# === CLASSES ===
# 2D Point
class Point2d(object):
def __init__(self, u, v):
""" Create a Point2d object using UV coords
Example: p = Point2d(1,-2) """
self.u = u
self.v = v
# 2D Line
class Line2d(object):
def __init__(self, pointA, pointB):
""" Create a Line2d object using two Point2d objects
Example: l = Line2d(pointA, pointB) """
self.pointA = pointA
self.pointB = pointB
# 2D polygon
class Polygon2d(object):
def __init__(self, lineList):
""" Create a Polygon2D object using a list of Line2d objects
Example: l = Polygon2D(list) """
self.lineList = lineList
self.pos = len(lineList)
# === CODE ===
incList = [(0.0, 0.1), (0.1, 0.0), (0.5, 0.6), (0.4, 0.6)]
# Create list of Point2d -points
pointList = []
for pair in incList :
pointList.append(Point2d(pair[0], pair[1]))
# print("pointList constructed, it is:")
# print pointList
# Create list of Line2d -lines
lineList = []
counter = 0
while counter <= len(pointList)-1:
if counter != len(pointList)-1:
# print("pointList[counter] is")
# print pointList[counter]
line = Line2d(pointList[counter], pointList[counter+1])
else: # Final line (last to first point)
line = Line2d(pointList[counter], pointList[0])
# print("created line %s")%line ## Gives (0.0, 0.1), (0.1, 0.0) on the first run
lineList.append(line)
# print("lineList is now") ## Gives [Line2d(Point2d(0.0, 0.0), Point2d(0.1, 0.0))] on the first run
counter += 1
# Create polygon object (Polygon2d)
print("lineList is %s")%lineList
return Polygon2d(lineList) ## ERROR: NOT CREATING POLYS
I´ve been trying to solve this issue where I pass a set off 2d points (incList) to a function that creates a convex hull (as a Polygon2d -object), but end up with a scrambled result.
Weird stuff 1: When printing the pointList (commented out), I get an array of Point2d objects, but if I print pointList[counter] I get just the coordinates!!!
Weird stuff 2: When I print a line, I get two tuples instead of a Line2d -object.
Weird stuff 3: When printing the lineList at the end, the array suddenly contains Line2d -objects, one which has a point in 0.0 0.0
What is wrong here?