def test_sides(self): expected = [ Segment(self.vertices[0], self.vertices[1]), Segment(self.vertices[1], self.vertices[2]), Segment(self.vertices[2], self.vertices[0]) ] actual = self.polygon.sides() self.assertEqual(expected, actual)
def __init__(self, p0, p1): Segment.__init__(self, p0, p1) if self.dx != 0.0: Segment.x_min.__set__(self, -INF) Segment.x_max.__set__(self, INF) if self.dy != 0.0: Segment.y_min.__set__(self, -INF) Segment.y_max.__set__(self, INF)
def __init__(self, p0, p1): Segment.__init__(self, p0, p1) if self.dx > 0.0: Segment.x_max.__set__(self, INF) elif self.dx < 0.0: Segment.x_min.__set__(self, -INF) if self.dy > 0.0: Segment.y_max.__set__(self, INF) elif self.dy < 0.0: Segment.y_min.__set__(self, -INF)
def test_parallel_intersection(): s0 = Segment((0, 0), (0, 1)) for _ in xrange(100): # collinear, single intersection s1 = Segment((0, 1), (0, 2+random())) assert s0.intersection(s1) == (0, 1) # collinear, infinitely many intersections s2 = Segment((0, 0.5), (0, 2+random())) assert s0.intersection(s2) is None # parallel, not collinear s3 = deepcopy(s0) s3.move(random(), random()) assert s0.intersection(s3) is None
def make_circle_from_points(a: Point, b: Point, c: Point): """ Creates a circle that passes through three points: `a`, `b` and `c`. :param a: `Point` :param b: `Point` :param c: `Point` :return: `Circle` """ chord_one_bisec = Segment(a, b).bisector chord_two_bisec = Segment(b, c).bisector center = chord_one_bisec.intersection_with(chord_two_bisec) radius = center.distance_to(a) return Circle(center, radius)
def sides(self): """ Returns a list of the sides of the polygon. A side is a segment connecting two consecutive vertices. :return: list of sides """ vertex_pairs = make_round_pairs(self.vertices) return [Segment(pair[0], pair[1]) for pair in vertex_pairs]
class TestSegment(unittest.TestCase): start = Point(400, 0) end = Point(0, 400) segment = Segment(start, end) def test_length(self): expected = 400 * math.sqrt(2) actual = self.segment.length self.assertAlmostEqual(expected, actual) # --- POINT AT --- # def test_point_at_wrong_t(self): self.assertRaises(tparam.TParamError, self.segment.point_at, 56.7) def test_point_at(self): t = tparam.make(0.25) expected = Point(300, 100) actual = self.segment.point_at(t) self.assertEqual(expected, actual) def test_middle_point(self): expected = Point(200, 200) actual = self.segment.middle self.assertEqual(expected, actual) # --- CLOSEST POINT --- # def test_closest_point_is_start(self): p = Point(500, 20) expected = self.start actual = self.segment.closest_point_to(p) self.assertEqual(expected, actual) def test_closest_point_is_end(self): p = Point(20, 500) expected = self.end actual = self.segment.closest_point_to(p) self.assertEqual(expected, actual) def test_closest_point_is_middle(self): p = Point(250, 250) expected = Point(200, 200) actual = self.segment.closest_point_to(p) self.assertEqual(expected, actual) # --- INTERSECTIONS --- # def test_parallel_segments_no_intersection(self): other = Segment(Point(200, 0), Point(0, 200)) actual = self.segment.intersection_with(other) self.assertIsNone(actual) def test_segments_intersection(self): other = Segment(Point(0, 0), Point(400, 400)) expected = Point(200, 200) actual = self.segment.intersection_with(other) self.assertEqual(expected, actual)
def apply_to_segment(self, segment: Segment): """ Computes a new `Segment` result of applying this affine transformation to the given `segment`. :param segment: source `Segment` :return: transformed `Segment` """ return Segment( self.apply_to_point(segment.start), self.apply_to_point(segment.end) )
def test_intersection(): for _ in xrange(10000): s0 = Segment((random(), random()), (random(), random())) s1 = Segment((random(), random()), (random(), random())) p = s0.intersection(s1) if p is not None: x = Approx(p[0]) y = Approx(p[1]) assert s0.x_min <= x <= s0.x_max assert s0.y_min <= y <= s0.y_max assert s1.x_min <= x <= s1.x_max assert s1.y_min <= y <= s1.y_max assert x == s0.x_at(y) assert y == s0.y_at(x) assert x == s1.x_at(y) assert y == s1.y_at(x)
def test_segments_intersection(self): other = Segment(Point(0, 0), Point(400, 400)) expected = Point(200, 200) actual = self.segment.intersection_with(other) self.assertEqual(expected, actual)
def test_parallel_segments_no_intersection(self): other = Segment(Point(200, 0), Point(0, 200)) actual = self.segment.intersection_with(other) self.assertIsNone(actual)