Reputation: 1813
I'm fairly new to python (and completely new to Python OO), but I'm working with a fairly complicated class that I'm struggling to understand but that I need to edit. To help understand what's going on I'm trying to pull data out of an object but everything I do is just returning "classname object at address".
Could someone explain to me how I'd go about printing, say, the vertex field in this object:
class Halfedge(object):
def __init__(self, edge=None, marker=Edge.LEFT):
self.left = None # left Halfedge in the edge list
self.right = None # right Halfedge in the edge list
self.qnext = None # priority queue linked list pointer
self.edge = edge # edge list Edge
self.marker = marker
self.vertex = None # Site()
self.ystar = BIG_FLOAT
def __eq__(self, other):
return self.ystar == other.ystar and \
self.vertex.x == other.vertex.x
def __lt__(self, other):
if self.ystar == other.ystar:
return self.vertex.x < other.vertex.x
else:
return self.ystar < other.ystar
def left_reg(self, default):
if not self.edge:
return default
elif self.marker == Edge.LEFT:
return self.edge.reg[Edge.LEFT]
else:
return self.edge.reg[Edge.RIGHT]
def right_reg(self, default):
if not self.edge:
return default
elif self.marker == Edge.LEFT:
return self.edge.reg[Edge.RIGHT]
else:
return self.edge.reg[Edge.LEFT]
def is_point_right_of(self, point):
"""Returns True if <point> is to right of halfedge.
"""
edge = self.edge
topsite = edge.reg[1]
right_of_site = point.x > topsite.x
if(right_of_site and self.marker == Edge.LEFT):
return True
if(not right_of_site and self.marker == Edge.RIGHT):
return False
if(edge.a == 1.0):
dyp = point.y - topsite.y
dxp = point.x - topsite.x
fast = 0
if ((not right_of_site and edge.b < 0.0) or \
(right_of_site and edge.b >= 0.0)):
above = dyp >= edge.b * dxp
fast = above
else:
above = point.x + point.y * edge.b > edge.c
if(edge.b < 0.0):
above = not above
if (not above):
fast = 1
if (not fast):
dxs = topsite.x - (edge.reg[0]).x
above = (edge.b * (dxp*dxp - dyp*dyp)) < \
(dxs*dyp*(1.0+2.0*dxp/dxs + edge.b*edge.b))
if(edge.b < 0.0):
above = not above
else: # edge.b == 1.0
yl = edge.c - edge.a * point.x
t1 = point.y - yl
t2 = point.x - topsite.x
t3 = yl - topsite.y
above = t1*t1 > t2*t2 + t3*t3
if(self.marker == Edge.LEFT):
return above
else:
return not above
def intersect(self, other):
"""Create a new site where the Halfedges edge1 and edge2 intersect.
"""
edge1 = self.edge
edge2 = other.edge
if (edge1 is None) or (edge2 is None):
return None
# if the two edges bisect the same parent return None
if edge1.reg[1] is edge2.reg[1]:
return None
d = edge1.a * edge2.b - edge1.b * edge2.a
if almost_equal(d, 0.0):
return None
intersect_x = (edge1.c * edge2.b - edge2.c * edge1.b) / d
intersect_y = (edge2.c * edge1.a - edge1.c * edge2.a) / d
if edge1.reg[1] < edge2.reg[1]:
halfedge = self
edge = edge1
else:
halfedge = other
edge = edge2
right_of_site = intersect_x >= edge.reg[1].x
if ((right_of_site and halfedge.marker == Edge.LEFT) or
(not right_of_site and halfedge.marker == Edge.RIGHT)):
return None
# create a new site at the point of intersection - this is a new
# vector event waiting to happen
return Site((intersect_x, intersect_y))
I know this is pretty simple stuff, but thanks for any help.
Edit: The entire class I'm using can be found at:
https://bitbucket.org/mozman/geoalg/src/5bbd46fa2270/geoalg/voronoi.py
Upvotes: 0
Views: 121
Reputation: 69182
To make an instance of Halfedge do, for example,
he2 = Halfedge()
and to print this instance's value of vertex do
print he2.vertex
Is this what you're after?
Upvotes: 1
Reputation: 599560
Assuming you have a Halfedge instance - let's call it my_halfedge
- you access any of its properties with the dot notation. So, for the vertex
property, you'd do my_halfedge.vertex
.
Upvotes: 1