class ALine(object): """3D Line work with APoint and support in draw in `AutoCAD` Usage:: >>> l1 = ALine([10, 10], [20, 20]) Aline(APoint(10.00, 10.00, 0.00), APoint(20.00, 20.00, 0.00)) """ def __init__(self, start_point, end_point): if isinstance(start_point, APoint): self.start = start_point else: self.start = APoint(*start_point) if isinstance(end_point, APoint): self.end = end_point else: self.end = APoint(*end_point) @property def length(self): """The length of 3D line""" return self.start.distance_to(self.end) @property def middle(self): """The middle point of 3D line""" return APoint((self.start.x + self.end.x) / 2, (self.start.y + self.end.y) / 2, (self.start.z + self.end.z) / 2) @staticmethod def create_from_vector(v, pnt): """ """ if v is None or not isinstance(v, Vector) or pnt is None: return None v = v.normalized() # TODO: Change into APoint + Vector return ALine(pnt, pnt + v) def __str__(self): return 'Aline(%s, %s)' % (self.start, self.end) def __eq__(self, v): return isinstance(v, ALine) and self.start == v.start and self.end == v.end
def test_distance(self): p1 = APoint(10, 10, 10) p2 = APoint(15, 15, 15) self.assertAlmostEqual(p1.distance_to(p2), 8.660254037844387) self.assertEqual(distance(p1, p2), distance(p2, p1))