コード例 #1
0
ファイル: show.py プロジェクト: pashazz/pyVoronoi
    def read_output(self):
        self.painter.drawDisplay.ClearCanvas()
        filename, _ = QtWidgets.QFileDialog.getOpenFileName()
        if filename:
            f = open(filename, 'r')
            lines = f.readlines()
            points = []
            _points = []
            for l in lines:
                t = l.split()
                if t[0] == 'P':
                    #need to create new attribute to save original point set which include duplicates
                    t1 = int(t[1])
                    t2 = int(t[2])
                    p = Point(t1, t2)
                    _points.append(p)
                    if p not in self.painter.points:
                        points.append(p)
                        self.painter.points.add((Point(t1, t2)))
                elif t[0] == 'E':
                    self.painter.lines.append(
                        Line(Point(int(t[1]), int(t[2])),
                             Point(int(t[3]), int(t[4]))))

            self.painter.points = points
            self.painter._points = _points
            for p in _points:
                self.painter.drawDisplay.display_points(p)
            self.painter.drawDisplay.display_output()
            self.painter.update()
            f.close()
コード例 #2
0
class Pin:
    """ Pins are the parts of Bodies (/symbols/components) that connect
    to nets. Basically a line segment, with a null end and a connect end
    """

    def __init__(self, pin_number, p1, p2, label=None):
        self.label = label # is a Label
        self.p1 = Point(p1) # null end
        self.p2 = Point(p2) # connect end
        self.pin_number = pin_number


    def bounds(self):
        """ Return the min and max points of a pin """
        xs = [self.p1.x, self.p2.x]
        ys = [self.p2.y, self.p2.y]
        if self.label is not None:
            xs += self.label.bounds()[0::2]
            ys += self.label.bounds()[1::2]
        return [Point(min(xs), min(ys)), Point(max(xs), max(ys))]


    def json(self):
        """ Return a pin as JSON """
        d = {
            "pin_number":self.pin_number,
            "p1":self.p1.json(),
            "p2":self.p2.json()
            }
        if self.label is not None:
            d["label"] = self.label.json()
        return d
コード例 #3
0
def prepare(size, polygons_info, start, end):
    '''
        something neccesary such as construct the whole picture
    '''
    polygons = generate_polygons(*polygons_info)
    for each in polygons:
        add_polygon_to_plot(each)
    start = Point(start)
    end = Point(end)
    points = [start]
    for polygon in polygons:
        points += polygon.vertices
    points += [end]

    #get successor_and_costs for every points
    for i in xrange(len(points)-1):
        # -1 for end point does not need to get successor
        point_i = points[i]
        siblings = [point_i]
        belonging = check_belonging(point_i, polygons)
        if belonging[0]:
            for each in belonging[1]:
                point_i.add_successor(each)
            siblings = belonging[2].vertices
        for other_point in points:
            if other_point in siblings:
                continue
            flag = True
            for polygon in polygons:
                if polygon.whether_intersect_line(Line((point_i, other_point))):
                    flag = False
                    break
            if flag:
                point_i.add_successor(other_point)
    return points
コード例 #4
0
 def test_intersection(self):
     p1 = Point(0, 0)
     p2 = Point(4, 0)
     p3 = Point(0, 2)
     p4 = Point(4, 2)
     p5 = Point(2, 1)
     self.assertEqual(Segment(p1, p4).intersection(Segment(p2, p3)), p5)
コード例 #5
0
 def bounds(self):
     """ Return the min and max points of a pin """
     xs = [self.p1.x, self.p2.x]
     ys = [self.p2.y, self.p2.y]
     if self.label is not None:
         xs += self.label.bounds()[0::2]
         ys += self.label.bounds()[1::2]
     return [Point(min(xs), min(ys)), Point(max(xs), max(ys))]
コード例 #6
0
def main() -> None:
    c1x, c1y, c1r = [int(x) for x in input().split()]
    c1 = Circle(Point(c1x, c1y), c1r)
    c2x, c2y, c2r = [int(x) for x in input().split()]
    c2 = Circle(Point(c2x, c2y), c2r)
    left, right = c1.cross_point_circle(c2)
    print("{:.8f} {:.8f} {:.8f} {:.8f}".format(left.x, left.y, right.x,
                                               right.y))
コード例 #7
0
 def test_intersects(self):
     p1 = Point(0, 0)
     p2 = Point(4, 0)
     p3 = Point(0, 2)
     p4 = Point(4, 2)
     p5 = Point(2, 0)
     self.assertTrue(Segment(p1, p4).intersects(Segment(p2, p3)))
     self.assertFalse(Segment(p1, p5).intersects(Segment(p2, p3)))
コード例 #8
0
def main() -> None:
    x0, y0, x1, y1 = [int(x) for x in input().split()]
    s = Segment(Point(x0, y0), Point(x1, y1))
    q = int(input())

    for _ in range(q):
        x2, y2 = [int(x) for x in input().split()]
        print(Point(x2, y2).location(s).name)
コード例 #9
0
 def test_init_default(self):
     s1 = Segment()
     self.assertEqual(s1.p1, Point(0, 0))
     self.assertEqual(s1.p2, Point(0, 0))
     s2 = Segment()
     s1.p1.x = 1
     self.assertEqual(s2.p1, Point(0, 0))
     self.assertEqual(s2.p2, Point(0, 0))
コード例 #10
0
 def test_init_default(self):
     p1 = Point()
     self.assertEqual(p1.x, 0)
     self.assertEqual(p1.y, 0)
     p2 = Point()
     p1.x = 1
     self.assertEqual(p2.x, 0)
     self.assertEqual(p2.y, 0)
コード例 #11
0
def main() -> None:
    n = int(input())

    for _ in range(n):
        x1, y1, x2, y2, x3, y3, x4, y4 = [int(x) for x in input().split()]
        s1 = Segment(Point(x1, y1), Point(x2, y2))
        s2 = Segment(Point(x3, y3), Point(x4, y4))
        print(s1.distance_with_segment(s2))
コード例 #12
0
 def test_cross_point_line(self):
     self.assertEqual(
         Circle(Point(2, 3),
                2).cross_point_line(Line(Point(0, 3), Point(1, 3))),
         [Point(0, 3), Point(4, 3)])
     self.assertEqual(
         Circle(Point(2, 3),
                2).cross_point_line(Line(Point(4, 0), Point(4, 6))),
         [Point(4, 3), Point(4, 3)])
コード例 #13
0
ファイル: CGL_2_B.py プロジェクト: koba925/PCAD
def main() -> None:
    q = int(input())

    for _ in range(q):
        x0, y0, x1, y1, x2, y2, x3, y3 = \
            [int(x) for x in input().split()]
        s1 = Segment(Point(x0, y0), Point(x1, y1))
        s2 = Segment(Point(x2, y2), Point(x3, y3))
        print(1 if s1.intersects(s2) else 0)
コード例 #14
0
    def test_in_width_of(self):
        s1 = Segment(Point(0, -1), Point(0, 1))
        self.assertFalse(Point(2, -2).in_width_of(s1))
        self.assertTrue(Point(2, -1).in_width_of(s1))
        self.assertTrue(Point(2, 0).in_width_of(s1))
        self.assertTrue(Point(2, 1).in_width_of(s1))
        self.assertFalse(Point(2, 2).in_width_of(s1))

        s1 = Segment(Point(-1, 0), Point(1, 0))
        self.assertFalse(Point(5, 3).in_width_of(s1))
コード例 #15
0
 def test_sides(self):
     p = Polygon([Point(0, 0), Point(1, 0), Point(0, 1)])
     self.assertEqual(list(p.sides()), [
         Segment(Point(0, 0), Point(1, 0)),
         Segment(Point(1, 0), Point(0, 1)),
         Segment(Point(0, 1), Point(0, 0))
     ])
コード例 #16
0
def main() -> None:
    n = int(input())
    points = []
    for _ in range(n):
        x, y = [int(x) for x in input().split()]
        points.append(Point(x, y))
    polygon = Polygon(points)

    q = int(input())
    for _ in range(q):
        x, y = [int(x) for x in input().split()]
        print(int(polygon.contains(Point(x, y))))
コード例 #17
0
ファイル: CGL_1_B.py プロジェクト: koba925/PCAD
def main() -> None:
    x1, y1, x2, y2 = [int(x) for x in input().split()]
    p1 = Point(x1, y1)
    p2 = Point(x2, y2)
    l = Line(p1, p2)

    q = int(input())

    for _ in range(q):
        x, y = [int(x) for x in input().split()]
        p = Point(x, y)
        a = l.reflection(p)
        print(a.x, a.y)
コード例 #18
0
 def test_cross_point_circle(self):
     self.assertTrue(
         Circle(Point(0, 0), 2).cross_point_circle(Circle(Point(2, 0), 2))
         == [Point(1.0, -1.7320508075),
             Point(1.0, 1.7320508075)])
     self.assertTrue(
         Circle(Point(0, 0), 2).cross_point_circle(Circle(Point(0, 3), 1))
         == [Point(0, 2), Point(0, 2)])
コード例 #19
0
 def mousePressEvent(self,event):
     p = Point(event.x(),event.y())
     self._points.append(p)
     if p not in self.points:
         self.points.append(p)
     self.drawDisplay.display_points(p)
     self.update()
コード例 #20
0
    def generate_struct(self):
        for x in range(0, self.num_shapes[0]):
            for y in range(0, self.num_shapes[1]):
                coordinates = [
                    ((x) * self.size_shapes[0], (y) * self.size_shapes[1]),
                    ((x + 1) * self.size_shapes[0], (y) * self.size_shapes[1]),
                    ((x + 1) * self.size_shapes[0],
                     (y + 1) * self.size_shapes[1]),
                    ((x) * self.size_shapes[0], (y + 1) * self.size_shapes[1])
                ]

                new_points = []

                for coord in coordinates:
                    if coord in self.points:
                        new_points.append(self.points[coord])
                    else:
                        new_point = Point(coord)
                        self.points[coord] = new_point
                        new_points.append(new_point)

                self.shapes.append(Shape(new_points))

        for point in self.points.values():
            if self.in_bounds(point):
                point.evolve()
コード例 #21
0
 def bounds(self):
     """ Return the min and max point of a design """
     bounds = [net.bounds() for net in self.nets]
     offset_bounds = lambda (x1, y1, x2, y2), (xo, yo): (x1 + xo, y1 + yo,
                                                         x2 + xo, y2 + yo)
     for comp in self.component_instances:
         offsets = [(att.x, att.y) for att in comp.symbol_attributes]
         lib_comp = self.components.components[comp.library_id]
         bbounds = [
             b.bounds() for b in lib_comp.symbols[comp.symbol_index].bodies
         ]
         bounds.extend(
             [offset_bounds(b, o) for b, o in zip(bbounds, offsets)])
         xs = sum([list(b[0::2]) for b in bounds], [])
         ys = sum([list(b[1::2]) for b in bounds], [])
     return [Point(min(xs), min(ys)), Point(max(xs), max(ys))]
コード例 #22
0
 def test_lt(self):
     self.assertTrue(self.p1 < Point(2, 2))
     self.assertFalse(self.p1 < Point(0, 2))
     self.assertTrue(self.p1 < Point(1, 3))
     self.assertTrue(self.p1 < Point(1.00000000001, 3))
     self.assertFalse(self.p1 < Point(1, 1))
     self.assertFalse(self.p1 < Point(1.00000000001, 1))
     self.assertFalse(self.p1 < Point(1, 2))
コード例 #23
0
ファイル: show.py プロジェクト: pashazz/pyVoronoi
    def ReadFile(self, filename=None):
        self.painter.drawDisplay.ClearCanvas()
        filename, _ = QtWidgets.QFileDialog.getOpenFileName()
        if filename:
            f = open(filename, 'r')
            self.readPoints = [[], 0]
            self._readPoints = [[], 0]
            self.painter.points = []
            self.painter.parent.next_data.setEnabled(True)
            while True:
                num = f.readline()
                if len(num) == 0:
                    continue
                elif num[0] == '#' or num[0] == '\n':
                    continue
                elif int(num) == 0:
                    break
                s = set()
                _data = []
                num = int(num)
                for i in range(0, num):
                    #to avoid the comment between input point
                    while True:
                        line = f.readline()
                        if len(line) == 0:
                            continue
                        elif line[0] == '#' or line[0] == '\n':
                            continue
                        else:
                            break

                    x, y = map(int, line.split())
                    p = Point(x, y)
                    _data.append(p)
                    if p not in s:
                        s.add(p)
                data = []
                for p in s:
                    data.append(p)

                self.readPoints[0].append(data)
                self._readPoints[0].append(_data)

            t = self.readPoints[1]
            self.painter.points.extend(self.readPoints[0][t])
            #for duplicated input data
            self.painter._points.extend(self._readPoints[0][t])
            #need to show original data,not filterd data
            for p in self.painter._points:
                self.painter.drawDisplay.display_points(p)

            self.readPoints[1] = self.readPoints[1] + 1
            self._readPoints[1] = self._readPoints[1] + 1
            f.close()
            self.painter.update()
コード例 #24
0
class Pin:
    """ Pins are the parts of Bodies (/symbols/components) that connect
    to nets. Basically a line segment, with a null end and a connect end
    """

    def __init__(self, pin_number, p1, p2, label=None):
        self.label = label # is a Label
        self.p1 = Point(p1) # null end
        self.p2 = Point(p2) # connect end
        self.pin_number = pin_number


    def json(self):
        d = {
            "pin_number":self.pin_number,
            "p1":self.p1.json(),
            "p2":self.p2.json()
            }
        if self.label is not None:
            d["label"] = self.label.json()
        return d
コード例 #25
0
 def test_eq(self):
     self.assertTrue(self.p1 == Point(1, 2))
     self.assertTrue(self.p1 == Point(1.00000000001, 2))
     self.assertTrue(self.p1 == Point(1, 2.00000000001))
     self.assertFalse(self.p1 == Point(1, 3))
     self.assertFalse(self.p1 == Point(2, 2))
     self.assertFalse(self.p1 == Point(2, 3))
     self.assertFalse(self.p1 == 3)
コード例 #26
0
 def __init__(self, pin_number, p1, p2, label=None):
     self.label = label # is a Label
     self.p1 = Point(p1) # null end
     self.p2 = Point(p2) # connect end
     self.pin_number = pin_number
コード例 #27
0
ファイル: diagram.py プロジェクト: pashazz/pyVoronoi
    def merge(VDL, VDR, tangent):

        clip_lines = []
        #used to record ray which intersect with dividing chain
        #using hash table
        ray_list = set()

        def discard_edges(ray, circumcenter, side, SG_bisector):
            def recursive_discard_edge(ray, other_point, base_point, side):
                #want to delete left remaining line
                for candidate in ray.connected:
                    if candidate.avail == True and candidate not in ray_list:
                        next_base_point = None
                        next_other_point = None
                        #catch base point
                        if (candidate.p1 is base_point
                                or candidate.p2 is base_point):
                            if candidate.p1 is base_point:
                                next_base_point = candidate.p2
                                next_other_point = candidate.p1
                            else:
                                next_base_point = candidate.p1
                                next_other_point = candidate.p2

                            if side == 'right':
                                if ConvexHull.cross(base_point,
                                                    next_base_point,
                                                    other_point) > 0:
                                    candidate.avail = False
                                    recursive_discard_edge(
                                        candidate, next_other_point,
                                        next_base_point, 'right')
                            elif side == 'left':
                                if ConvexHull.cross(base_point,
                                                    next_base_point,
                                                    other_point) < 0:
                                    candidate.avail = False
                                    recursive_discard_edge(
                                        candidate, next_other_point,
                                        next_base_point, 'left')

            if side == 'right':
                #clear the edges extend to the left of HP
                #Line(hole,ray.p1) or Line(hole,ray.p2) must cw to Line(hole,bisector.p1)
                if ConvexHull.cross(circumcenter, ray.p1, SG_bisector.p1) > 0:
                    #this means p1 is left to circumcenter,so replace p1 with circumcenter
                    if ray.p1.iscircumcenter == True:
                        recursive_discard_edge(ray, circumcenter, ray.p1,
                                               'right')
                    ray.p1 = circumcenter
                else:
                    if ray.p2.iscircumcenter == True:
                        recursive_discard_edge(ray, circumcenter, ray.p2,
                                               'right')
                    ray.p2 = circumcenter
            elif side == "left":
                #clear the edges extend to the right of HP
                #Line(hole,ray.p1) or Line(hole,ray.p2) must ccw to Line(hole,bisector.p1)
                if ConvexHull.cross(circumcenter, ray.p1, SG_bisector.p1) < 0:
                    #this means p1 is right to circumcenter,so replace p1 with circumcenter
                    if ray.p1.iscircumcenter == True:
                        recursive_discard_edge(ray, circumcenter, ray.p1,
                                               'left')
                    ray.p1 = circumcenter
                else:

                    if ray.p2.iscircumcenter == True:
                        recursive_discard_edge(ray, circumcenter, ray.p2,
                                               'left')
                    ray.p2 = circumcenter
            else:
                #clear both side
                #clear the edges extend to the right of HP
                #Line(hole,ray.p1) or Line(hole,ray.p2) must ccw to Line(hole,bisector.p1)
                if ConvexHull.cross(circumcenter, ray[0].p1,
                                    SG_bisector.p1) < 0:
                    #this means p1 is right to circumcenter,so replace p1 with circumcenter
                    if ray[0].p1.iscircumcenter == True:
                        recursive_discard_edge(ray[0], circumcenter, ray[0].p1,
                                               'left')
                    ray[0].p1 = circumcenter
                else:
                    if ray[0].p2.iscircumcenter == True:
                        recursive_discard_edge(ray[0], circumcenter, ray[0].p2,
                                               'left')
                    ray[0].p2 = circumcenter

                #clear the edges extend to the left of HP
                if ConvexHull.cross(circumcenter, ray[1].p1,
                                    SG_bisector.p1) > 0:
                    #this means p1 is left to circumcenter,so replace p1 with circumcenter
                    if ray[1].p1.iscircumcenter == True:
                        recursive_discard_edge(ray[1], circumcenter, ray[1].p1,
                                               'right')
                    ray[1].p1 = circumcenter
                else:
                    if ray[1].p2.iscircumcenter == True:
                        recursive_discard_edge(ray[1], circumcenter, ray[1].p2,
                                               'right')
                    ray[1].p2 = circumcenter

        def nextPoint(pool, SG_bisector):
            ans = None
            first = True
            for candidate in pool:
                if candidate.line.avail == True and SG_bisector.p1 is not candidate.line.hole:
                    result = Line.intersect(candidate.line, SG_bisector)
                    if result is not None:
                        t = (result, candidate.point, candidate.line)
                        if first == True:
                            ans = t
                            first = False
                        else:
                            if t[0].y <= ans[0].y:
                                ans = t
            return ans

        upper_tangent, lower_tangent = VD.find_tangent(VDL, VDR)
        ul = (upper_tangent, lower_tangent)
        tangent[0].append(ul)

        HP = []
        SG = upper_tangent
        px = SG.p1
        py = SG.p2
        #p1 of upper_tangent belongs to VDL, and p2 belongs to VDR
        SG_bisector = Line.biSector(SG.p1, SG.p2)
        SG_bisector._p1 = SG.p1
        SG_bisector._p2 = SG.p2

        HP.append(SG_bisector)
        circumcenter = None

        firsttime = True
        newpl = defaultdict(list)

        while not (SG == lower_tangent):
            #step4 as textBook

            #p1 of SG_bisector is fixed to upper position than p2
            if SG_bisector.p1.y > SG_bisector.p2.y:
                SG_bisector.p1, SG_bisector.p2 = SG_bisector.p2, SG_bisector.p1
            elif abs((SG_bisector.p1.y) - (SG_bisector.p2.y)) <= 0.00005:
                if SG_bisector.p1.x < SG_bisector.p2.x:
                    SG_bisector.p1, SG_bisector.p2 = SG_bisector.p2, SG_bisector.p1

            newpl[SG.p1].append(pair(SG_bisector, SG.p2))
            newpl[SG.p2].append(pair(SG_bisector, SG.p1))

            #orginally p1 is very far from painter,so we need to fix p1 to previous circumcenter
            if firsttime == False and circumcenter is not None:
                SG_bisector.p1 = circumcenter

            pll = px.related
            prl = py.related

            result_l = nextPoint(pll, SG_bisector)
            result_r = nextPoint(prl, SG_bisector)

            side = None
            ray = None
            #with biSector of pyz2 first,that is,VDR first
            if result_l is not None and result_r is not None:
                if abs(result_l[0].x - result_r[0].x) <= 0.05 and abs(
                        result_l[0].y - result_r[0].y) <= 0.05:
                    #VDL.parent.msg = VDL.parent.msg+'both cd'+'\n'
                    SG = Line(result_l[1], result_r[1])
                    circumcenter = result_l[0]
                    ray = (result_l[2], result_r[2])
                    side = 'both'
                elif result_l[0].y > result_r[0].y:
                    #VDL.parent.msg = VDL.parent.msg+'cd VDR'+'\n'
                    SG = Line(px, result_r[1])
                    circumcenter = result_r[0]
                    ray = result_r[2]
                    side = 'right'
                elif result_l[0].y < result_r[0].y:
                    #VDL.parent.msg = VDL.parent.msg+'cd VDL'+'\n'
                    SG = Line(result_l[1], py)
                    circumcenter = result_l[0]
                    ray = result_l[2]
                    side = 'left'
                else:
                    print('confused...')
                    #print result_l,result_r
            else:
                if result_l is not None and result_r is None:
                    #VDL.parent.msg = VDL.parent.msg+'VDR is None,cd VDL'+'\n'
                    SG = Line(result_l[1], py)
                    circumcenter = result_l[0]
                    ray = result_l[2]
                    side = 'left'
                elif result_r is not None and result_l is None:
                    #VDL.parent.msg = VDL.parent.msg+'VDL is None,cd VDR'+'\n'
                    SG = Line(px, result_r[1])
                    circumcenter = result_r[0]
                    #print 'circumcenter',(circumcenter.x,circumcenter.y)
                    ray = result_r[2]
                    side = 'right'
                else:
                    #VDL.parent.msg = VDL.parent.msg+'both are None'+'\n'
                    #let SG be lower_tangent
                    SG = lower_tangent
                    SG_bisector = Line.biSector(SG.p1, SG.p2)
                    SG_bisector._p1 = SG.p1
                    SG_bisector._p2 = SG.p2
                    HP.append(SG_bisector)
                    continue

            if ray is not None:
                if not isinstance(ray, tuple):
                    ray.hole = circumcenter
                    t = (ray, SG_bisector, side, circumcenter)
                    if ray not in ray_list:
                        ray_list.add(ray)
                    clip_lines.append(t)
                else:
                    for r in ray:
                        r.hole = circumcenter
                        ray_list.add(r)
                    t = (ray, SG_bisector, side, circumcenter)
                    clip_lines.append(t)

            if circumcenter is not None:
                circumcenter.iscircumcenter = True
                #lower point is replaced by circumcenter
                SG_bisector.p2 = circumcenter

            #new SG
            px = SG.p1
            py = SG.p2
            SG_bisector = Line.biSector(SG.p1, SG.p2)
            SG_bisector._p1 = SG.p1
            SG_bisector._p2 = SG.p2

            HP.append(SG_bisector)
            firsttime = False
            #the end of while loop for HP

        if SG_bisector.p1.y > SG_bisector.p2.y:
            SG_bisector.p1, SG_bisector.p2 = SG_bisector.p2, SG_bisector.p1
        elif abs((SG_bisector.p1.y) - (SG_bisector.p2.y)) <= 0.00005:
            if SG_bisector.p1.x < SG_bisector.p2.x:
                SG_bisector.p1, SG_bisector.p2 = SG_bisector.p2, SG_bisector.p1

        newpl[SG.p1].append(pair(SG_bisector, SG.p2))
        newpl[SG.p2].append(pair(SG_bisector, SG.p1))

        for p in newpl.keys():
            for t in newpl[p]:
                p.related.append(t)

        if circumcenter is not None:
            SG_bisector.p1 = circumcenter

        #clip the unwanted lines
        VDL.parent.msg = VDL.parent.msg + 'clip lines' + '\n'
        for t in clip_lines:
            ray = t[0]
            circumcenter = t[3]
            SG_bisector = t[1]
            side = t[2]
            discard_edges(ray, circumcenter, side, SG_bisector)

        #add new connected line
        s = 0
        for t in range(0, len(HP) - 1):
            #need to add the intersected dividing chain
            HP[t].connected.append(HP[t + 1])
            HP[t + 1].connected.append(HP[t])
            #need to add the intersected ray
            if s != len(clip_lines):
                if not isinstance(clip_lines[s][0], tuple):
                    HP[t].connected.append(clip_lines[s][0])
                    clip_lines[s][0].connected.append(HP[t])
                    HP[t + 1].connected.append(clip_lines[s][0])
                    clip_lines[s][0].connected.append(HP[t + 1])
                else:
                    r = clip_lines[s][0]
                    HP[t].connected.append(r[0])
                    r[0].connected.append(HP[t])
                    HP[t + 1].connected.append(r[0])
                    r[0].connected.append(HP[t + 1])

                    HP[t].connected.append(r[1])
                    r[1].connected.append(HP[t])
                    HP[t + 1].connected.append(r[1])
                    r[1].connected.append(HP[t + 1])

                    r[1].connected.append(r[0])
                    r[0].connected.append(r[1])
            s = s + 1

        lines = []
        #lines = VDR.lines+VDL.lines+HP
        lines.append(VDR.lines)
        lines.append(VDL.lines)
        lines.append(HP)
        if VDL.parent.isstep_by_step == True:
            hp = []
            for h in HP:
                hp.append(Line(Point(h.p1.x, h.p1.y), Point(h.p2.x, h.p2.y)))
            VDR.parent.hp[0].append(hp)
        #VDR.parent.hp[0].append(copy.deepcopy(HP))
        range_points = (VDL.range_points[0], VDR.range_points[1])
        return VD(lines, range_points, VDR.parent)
コード例 #28
0
 def test_distance(self):
     self.assertEqual(Point(1, 1).distance(Point(4, 5)), 5)
コード例 #29
0
 def test_projection(self):
     self.assertEqual(
         Segment(Point(0, 0), Point(4, 0)).projection(Point(2, 2)),
         Point(2, 0))
コード例 #30
0
class TestPoint(unittest.TestCase):
    def setUp(self):
        self.p1 = Point(1, 2)
        self.p2 = Point(3, 5)

    def test_float_equal(self):
        self.assertTrue(float_equal(1, 1))
        self.assertTrue(float_equal(1.00000000001, 1))
        self.assertTrue(float_equal(1, 1.00000000001))
        self.assertFalse(float_equal(1.000000001, 1))
        self.assertFalse(float_equal(1, 1.000000001))

    def test_init_default(self):
        p1 = Point()
        self.assertEqual(p1.x, 0)
        self.assertEqual(p1.y, 0)
        p2 = Point()
        p1.x = 1
        self.assertEqual(p2.x, 0)
        self.assertEqual(p2.y, 0)

    def test_init(self):
        self.assertEqual(self.p1.x, 1)
        self.assertEqual(self.p1.y, 2)

    def test_repr(self):
        self.assertEqual(self.p1.__repr__(), 'Point(1, 2)')

    def test_eq(self):
        self.assertTrue(self.p1 == Point(1, 2))
        self.assertTrue(self.p1 == Point(1.00000000001, 2))
        self.assertTrue(self.p1 == Point(1, 2.00000000001))
        self.assertFalse(self.p1 == Point(1, 3))
        self.assertFalse(self.p1 == Point(2, 2))
        self.assertFalse(self.p1 == Point(2, 3))
        self.assertFalse(self.p1 == 3)

    def test_add(self):
        self.assertEqual(self.p1 + self.p2, Point(4, 7))

    def test_sub(self):
        self.assertEqual(self.p1 - self.p2, Point(-2, -3))

    def test_mul(self):
        self.assertEqual(self.p1 * 2, Point(2, 4))
        self.assertEqual(2 * self.p1, Point(2, 4))

    def test_div(self):
        self.assertEqual(self.p1 / 2, Point(0.5, 1))

    def test_lt(self):
        self.assertTrue(self.p1 < Point(2, 2))
        self.assertFalse(self.p1 < Point(0, 2))
        self.assertTrue(self.p1 < Point(1, 3))
        self.assertTrue(self.p1 < Point(1.00000000001, 3))
        self.assertFalse(self.p1 < Point(1, 1))
        self.assertFalse(self.p1 < Point(1.00000000001, 1))
        self.assertFalse(self.p1 < Point(1, 2))

    def test_norm(self):
        self.assertTrue(Point(3, 4).norm() == 25)
        self.assertTrue(Vector(3, 4).norm() == 25)

    def test_abs(self):
        self.assertTrue(Point(3, 4).abs() == 5)
        self.assertTrue(Vector(3, 4).abs() == 5)

    def test_dot(self):
        self.assertEqual(Point(1, 0).dot(Point(0, 1)), 0)
        self.assertEqual(self.p1.dot(self.p2), 13)

    def test_cross(self):
        self.assertEqual(Point(1, 0).cross(Point(0, 1)), 1)
        self.assertEqual(self.p1.cross(self.p2), -1)

    def test_is_orthogonal(self):
        self.assertTrue(Point(1, 0).is_orthogonal(Point(0, 1)))
        self.assertFalse(Point(0, 1).is_orthogonal(Point(1, 1)))

    def test_is_parallel(self):
        self.assertFalse(Point(1, 0).is_parallel(Point(0, 1)))
        self.assertTrue(Point(0, 1).is_parallel(Point(0, 1)))

    def test_projection(self):
        self.assertEqual(
            Segment(Point(0, 0), Point(4, 0)).projection(Point(2, 2)),
            Point(2, 0))

    def test_distance(self):
        self.assertEqual(Point(1, 1).distance(Point(4, 5)), 5)

    def test_in_side_of(self):
        s1 = Segment(Point(0, 0), Point(0, 1))
        self.assertFalse(Point(2, -1).in_side_of(s1))
        self.assertTrue(Point(2, 0).in_side_of(s1))
        self.assertTrue(Point(2, 1).in_side_of(s1))

    def test_in_width_of(self):
        s1 = Segment(Point(0, -1), Point(0, 1))
        self.assertFalse(Point(2, -2).in_width_of(s1))
        self.assertTrue(Point(2, -1).in_width_of(s1))
        self.assertTrue(Point(2, 0).in_width_of(s1))
        self.assertTrue(Point(2, 1).in_width_of(s1))
        self.assertFalse(Point(2, 2).in_width_of(s1))

        s1 = Segment(Point(-1, 0), Point(1, 0))
        self.assertFalse(Point(5, 3).in_width_of(s1))

    def test_distance_to_line(self):
        s1 = Segment(Point(-1, 0), Point(1, 0))
        self.assertEqual(Point(0, 3).distance_to_line(s1), 3)

    def test_distance_to_segment(self):
        s1 = Segment(Point(-1, 0), Point(1, 0))
        self.assertEqual(Point(-5, 3).distance_to_segment(s1), 5)
        self.assertEqual(Point(0, 3).distance_to_segment(s1), 3)
        self.assertEqual(Point(5, 3).distance_to_segment(s1), 5)
コード例 #31
0
 def test_is_parallel(self):
     self.assertFalse(Point(1, 0).is_parallel(Point(0, 1)))
     self.assertTrue(Point(0, 1).is_parallel(Point(0, 1)))
コード例 #32
0
 def test_is_orthogonal(self):
     self.assertTrue(Point(1, 0).is_orthogonal(Point(0, 1)))
     self.assertFalse(Point(0, 1).is_orthogonal(Point(1, 1)))