访问Python对象数据

时间:2013-01-03 18:58:39

标签: python oop

我是python的新手(并且对Python OO来说是全新的),但是我正在使用一个相当复杂的类,我正在努力理解,但我需要编辑。为了帮助理解正在发生的事情,我试图从对象中提取数据,但我所做的只是返回“地址处的classname对象”。

有人可以向我解释如何打印这个对象中的顶点字段:

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))

我知道这很简单,但感谢您的帮助。

编辑:我正在使用的整个课程可以在以下网址找到:

https://bitbucket.org/mozman/geoalg/src/5bbd46fa2270/geoalg/voronoi.py

2 个答案:

答案 0 :(得分:1)

假设您有一个Halfedge实例 - 让我们称之为my_halfedge - 您可以使用点表示法访问其任何属性。因此,对于vertex属性,您需要执行my_halfedge.vertex

答案 1 :(得分:1)

要制作Halfedge的实例,例如,

he2 = Halfedge()

并打印此实例的顶点值

print he2.vertex

这就是你要追求的吗?