def test_distance_point(): print('plane-pt') pt = Point(0, 0, 0) pt1 = Point(0, 0, 1) pt2 = Point(1, 0, 1) pt3 = Point(1, 1, 1) plane = Plane(pt_1=pt1, pt_2=pt2, pt_3=pt3) print(plane.distance_to_point(pt))
def test_distance_line(): pt0 = Point(0, 0, 0) pt1 = Point(0, 0, 1) pt2 = Point(1, 0, 0) pt3 = Point(1, 0, 1) line1 = Line(pt_1=pt0, pt_2=pt1) line2 = Line(pt_1=pt2, pt_2=pt3) print(line1.distance_to_line(line2)) # should be 1
def test_distance_segment(): pt0 = Point(0, 0, 0) pt1 = Point(0, 0, 1) pt2 = Point(1, 0, 0) pt3 = Point(1, 0, 1) line = Line(pt_1=pt0, pt_2=pt1) seg = Segment(pt_1=pt2, pt_2=pt3) print(line.distance_to_segment(seg)) # should be 1
def test_distance_point(): print('poly-pt') pt = Point(0, 0, 0) pt1 = Point(0, 1, 1) pt2 = Point(0, 0, 1) pt3 = Point(1, 0, 1) pt4 = Point(1, 1, 1) poly = PolygonRegular(vertices=[pt1, pt2, pt3, pt4]) print(poly.distance_to_point(pt)) # should be 1
def test_distance_line(): pt0 = Point(0, 0, 0) pt1 = Point(1, 0, 0) pt2 = Point(1, 1, 0) pt3 = Point(0, 0, 4) line = Line(pt_1=pt1, pt_2=pt2) print('pt-line') print(pt0.distance_to_line(line)) seg = Segment(pt1, pt2) print(pt0.distance_to_segment(seg)) plane = Plane(pt_1=pt0, pt_2=pt1, pt_3=pt2) print(pt3.distance_to_plane(plane))
def test_distance_plane(): print('plane-plane') pt1 = Point(0, 0, 1) pt2 = Point(1, 0, 1) pt3 = Point(1, 1, 1) pt4 = Point(0, 0, 3) pt5 = Point(1, 0, 3) pt6 = Point(1, 1, 3) pt7 = Point(0, 0, 2) pt8 = Point(1, 0, 3) pt9 = Point(1, 1, 3) plane1 = Plane(pt_1=pt1, pt_2=pt2, pt_3=pt3) plane2 = Plane(pt_1=pt4, pt_2=pt5, pt_3=pt6) plane3 = Plane(pt_1=pt7, pt_2=pt8, pt_3=pt9) print(plane1.distance_to_plane(plane2)) # should be 2 print(plane1.distance_to_plane(plane3)) # they intersect
def test_distance_polygon(): print('plane-poly') pt1 = Point(0, 0, 1) pt2 = Point(1, 0, 1) pt3 = Point(1, 1, 1) pt4 = Point(0, 0, 3) pt5 = Point(1, 0, 3) pt6 = Point(1, 1, 3) pt7 = Point(0, 0, -3) pt8 = Point(1, 0, 3) pt9 = Point(1, 1, 3) plane = Plane(pt_1=pt1, pt_2=pt2, pt_3=pt3) poly1 = PolygonRegular(vertices=[pt4, pt5, pt6]) poly2 = PolygonRegular(vertices=[pt7, pt8, pt9]) print(plane.distance_to_polygon(poly1)) # should be 2 print(plane.distance_to_polygon(poly2)) # they intersect
def test_distance_polygon(): print('poly-poly') pt1 = Point(-1, 1, 1) pt2 = Point(-1, 0, 1) pt3 = Point(1, 0, 1) pt4 = Point(0, -1, 1) pt5 = Point(0, -1, 0) pt6 = Point(0, 1, 0) pt7 = Point(2, -1, 1) pt8 = Point(2, -1, 0) pt9 = Point(2, 1, 0) poly1 = PolygonRegular(vertices=[pt1, pt2, pt3]) poly2 = PolygonRegular(vertices=[pt4, pt5, pt6]) poly3 = PolygonRegular(vertices=[pt7, pt8, pt9]) print(poly1.distance_to_polygon(poly2)) # they intersect print(poly1.distance_to_polygon(poly3)) # should be 1
def test_distance_polygon(): print('pt-poly') pt0 = Point(0, 10, 0) pt1 = Point(-1, 1, 0) pt2 = Point(1, 1, 0) pt3 = Point(1, -1, 0) pt4 = Point(-1, -1, 0) poly = PolygonRegular(vertices=[pt1, pt2, pt3, pt4]) print(pt0.distance_to_polygon_regular(poly))
def test_distance_polygon(): pt0 = Point(0, 0, 0) pt1 = Point(1, 0, 0) pt6 = Point(0, 0, 5) pt7 = Point(1, 0, 5) pt8 = Point(0, 4, 5) pt9 = Point(1, 4, 5) pt2 = Point(0, 0, 1) pt3 = Point(1, 0, 1) pt4 = Point(1, 1, 1) pt5 = Point(0, 1, 1) line1 = Line(pt_1=pt0, pt_2=pt2) line2 = Line(pt_1=pt0, pt_2=pt1) line3 = Line(pt_1=pt6, pt_2=pt7) line4 = Line(pt_1=pt8, pt_2=pt9) poly1 = PolygonRegular(vertices=[pt2, pt3, pt4, pt5]) print(line1.distance_to_polygon(poly1)) # they intersect print(line2.distance_to_polygon(poly1)) # should be 1 print(line3.distance_to_polygon(poly1)) # should be 4 print(line4.distance_to_polygon(poly1)) # should be 5
def test_distance_plane(): print('pt-plane') pt0 = Point(1, 1, 1) pt1 = Point(-1, 1, 0) pt2 = Point(1, 1, 0) pt3 = Point(0, 0, 0) print(pt0.distance_to_plane(Plane(pt_1=pt1, pt_2=pt2, pt_3=pt3)))
def test_distance_polygon(): pt1 = Point(0, 0, -1) pt2 = Point(0, 0, 1) pt3 = Point(0, 1, 1) pt4 = Point(0, 1, -1) poly = PolygonRegular(vertices=[pt1, pt2, pt3, pt4]) pt5 = Point(1, 0, 0) pt6 = Point(1, 1, 0) pt7 = Point(-1, 0, 0) pt8 = Point(-1, 0, 100) seg1 = Segment(pt_1=pt5, pt_2=pt6) seg2 = Segment(pt_1=pt5, pt_2=pt7) seg3 = Segment(pt_1=pt5, pt_2=pt8) print(seg1.distance_to_polygon(poly)) # should be 1 print(seg3.distance_to_polygon(poly)) # should be a little bit less than 1 print(seg2.distance_to_polygon(poly)) # they cross
def test_distance_plane(): print('poly-plane') pt1 = Point(-1, 1, 1) pt2 = Point(-1, 0, 1) pt3 = Point(1, 0, 1) pt4 = Point(0, 1, 2) pt5 = Point(0, 0, 2) pt6 = Point(1, 0, 2) pt7 = Point(-1, 0, -2) poly = PolygonRegular(vertices=[pt1, pt2, pt3]) plane1 = Plane(pt_1=pt4, pt_2=pt5, pt_3=pt6) plane2 = Plane(pt_1=pt4, pt_2=pt5, pt_3=pt7) print(poly.distance_to_plane(plane1)) # should be 1 print(poly.distance_to_plane(plane2)) # they intersect
def test_distance_line(): print('poly-line') pt1 = Point(0, 1, 1) pt2 = Point(0, 0, 1) pt3 = Point(1, 0, 1) pt4 = Point(1, 1, 1) pt5 = Point(0, 0, 2) pt6 = Point(0, 1, 2) pt7 = Point(0, 0, 3) poly = PolygonRegular(vertices=[pt1, pt2, pt3, pt4]) line1 = Line(pt_1=pt5, pt_2=pt6) line2 = Line(pt_1=pt5, pt_2=pt7) print(poly.distance_to_line(line1)) # should be 1 print(poly.distance_to_line(line2)) # they intersect
def test_distance_segment(): print('poly-seg') pt1 = Point(0, 1, 1) pt2 = Point(0, 0, 1) pt3 = Point(1, 0, 1) pt4 = Point(1, 1, 1) pt5 = Point(0, 0, 2) pt6 = Point(0, 1, 2) pt7 = Point(0, 0, -2) poly = PolygonRegular(vertices=[pt1, pt2, pt3, pt4]) seg1 = Segment(pt_1=pt5, pt_2=pt6) seg2 = Segment(pt_1=pt5, pt_2=pt7) print(poly.distance_to_segment(seg1)) # should be 1 print(poly.distance_to_segment(seg2)) # they intersect
def test_distance_plane(): pt1 = Point(0, 0, 1) pt2 = Point(0, 0, -1) pt3 = Point(0, 1, 1) pt4 = Point(-1, 0, 0) pt5 = Point(1, 0, 0) pt6 = Point(-1, 0, 1) plane = Plane(pt_1=pt1, pt_2=pt2, pt_3=pt3) seg1 = Segment(pt_1=pt4, pt_2=pt5) seg2 = Segment(pt_1=pt4, pt_2=pt6) print(seg1.distance_to_plane(plane)) # they cross print(seg2.distance_to_plane(plane)) # distance should be 1
def test_distance_line(): pt1 = Point(1, 0, 1) pt2 = Point(-1, 0, 1) pt3 = Point(0, -1, 0) pt4 = Point(0, 1, 0) pt5 = Point(0, -1, 1) pt6 = Point(0, 1, 1) seg = Segment(pt_1=pt1, pt_2=pt2) line1 = Line(pt_1=pt3, pt_2=pt4) line2 = Line(pt_1=pt5, pt_2=pt6) print(seg.distance_to_line(line1)) # distance should be 1 print(seg.distance_to_line(line2)) # they cross
def test_distance_segment(): pt1 = Point(1, 0, 1) pt2 = Point(-1, 0, 1) pt3 = Point(0, -1, 0) pt4 = Point(0, 1, 0) pt5 = Point(0, -1, 1) pt6 = Point(0, 1, 1) seg = Segment(pt_1=pt1, pt_2=pt2) seg1 = Segment(pt_1=pt3, pt_2=pt4) seg2 = Segment(pt_1=pt5, pt_2=pt6) print(seg.distance_to_segment(seg1)) # distance should be 1 print(seg.distance_to_segment(seg2)) # they cross
def test_distance_plane(): pt0 = Point(0, 0, 0) pt1 = Point(0, 0, 1) pt2 = Point(0, 1, 0) pt3 = Point(1, 0, 0) pt4 = Point(1, 0, 1) pt5 = Point(-1, 0, 10) plane = Plane(pt_1=pt0, pt_2=pt1, pt_3=pt2) line1 = Line(pt_1=pt3, pt_2=pt4) line2 = Line(pt_1=pt3, pt_2=pt5) print(line1.distance_to_plane(plane)) # should be 1 print(line2.distance_to_plane(plane)) # they intersect
def test_distance_segment(): print('plane-seg') pt1 = Point(0, 0, 1) pt2 = Point(1, 0, 1) pt3 = Point(1, 1, 1) pt4 = Point(0, 0, 0) pt5 = Point(1, 1, 0) pt6 = Point(0, 0, 2) seg1 = Segment(pt_1=pt4, pt_2=pt5) seg2 = Segment(pt_1=pt4, pt_2=pt6) plane = Plane(pt_1=pt1, pt_2=pt2, pt_3=pt3) print(plane.distance_to_segment(seg1)) # should be 1 print(plane.distance_to_segment(seg2)) # they intersect
def test_distance_line(): print('plane-line') pt1 = Point(0, 0, 1) pt2 = Point(1, 0, 1) pt3 = Point(1, 1, 1) pt4 = Point(0, 0, 0) pt5 = Point(1, 1, 0) pt6 = Point(0, 1, 1) line1 = Line(pt_1=pt4, pt_2=pt5) line2 = Line(pt_1=pt4, pt_2=pt6) plane = Plane(pt_1=pt1, pt_2=pt2, pt_3=pt3) print(plane.distance_to_line(line1)) # should be 1 print(plane.distance_to_line(line2)) # they intersect
def init_vertices(self, vertices): if len(vertices) < 3: print('error in poly init_vertices:', 'not enough vertices number') return None self.vertices = vertices x = 0 y = 0 z = 0 for pt in self.vertices: x += pt.x y += pt.y z += pt.z self.N = len(self.vertices) self.central_angle = math.pi / self.N # TODO: center is useful, but imports are recursive if uncomment next line self.center = Point(x / self.N, y / self.N, z / self.N) self.edge_length = self.vertices[-1].distance_to_point( self.vertices[0]) self.outer_radius = self.edge_length / 2 / math.sin(self.central_angle) self.inner_radius = (self.outer_radius**2 - self.edge_length**2 / 4)**0.5 self.containing_plane = Plane(pt_1=self.vertices[0], pt_2=self.vertices[1], pt_3=self.vertices[2])
def test_distance_point(): pt1 = Point(0, 0, 0) pt2 = Point(0, 0, 0) print('pt-pt') print(pt1.distance_to_point(pt1)) print(pt1.distance_to_point(pt2))
def test_distance_segment(): print('pt-seg') pt0 = Point(0, 0, 0) pt1 = Point(-1, 1, 1) pt2 = Point(1, 1, 1) print(pt0.distance_to_segment(Segment(pt_1=pt1, pt_2=pt2)))
def test_distance_prism(): print('pt-prism') pt0 = Point(0, 0, 11) pt10 = Point(-1, 1, 0) pt20 = Point(1, 1, 0) pt30 = Point(1, -1, 0) pt40 = Point(-1, -1, 0) top = PolygonRegular(vertices=[pt10, pt20, pt30, pt40]) pt11 = Point(-1, 1, 1) pt21 = Point(1, 1, 1) pt31 = Point(1, -1, 1) pt41 = Point(-1, -1, 1) bot = PolygonRegular(vertices=[pt11, pt21, pt31, pt41]) prism = PrismRegular(top_facet=top, bot_facet=bot) print(pt0.distance_to_prism_regular(prism))
def test_distance_point(): pt0 = Point(0, 0, 0) pt1 = Point(0, 0, 1) pt2 = Point(1, 0, 0) line = Line(pt_1=pt0, pt_2=pt1) print(line.distance_to_point(pt2)) # should be 1
def test_distance_prism(): print('plane-prism') pt1 = Point(0, 0, 1) pt2 = Point(1, 0, 1) pt3 = Point(1, 1, 1) pt4 = Point(0, 0, 3) pt5 = Point(1, 0, 3) pt6 = Point(1, 1, 3) pt7 = Point(0, 0, -3) pt8 = Point(1, 0, -3) pt9 = Point(1, 1, -3) pt10 = Point(0, 0, 5) pt11 = Point(0, 1, 5) pt12 = Point(1, 1, 5) plane = Plane(pt_1=pt1, pt_2=pt2, pt_3=pt3) top_facet = PolygonRegular(vertices=[pt4, pt5, pt6]) bot_facet = PolygonRegular(vertices=[pt7, pt8, pt9]) bot_facet2 = PolygonRegular(vertices=[pt10, pt11, pt12]) prism1 = PrismRegular(top_facet, bot_facet) prism2 = PrismRegular(top_facet, bot_facet2) print(plane.distance_to_prism(prism1)) # they intersect print(plane.distance_to_prism(prism2)) # should be 2
def test_distance_prism(): pt10 = Point(0, 0, -1) pt20 = Point(0, 0, 1) pt30 = Point(0, 1, 1) pt40 = Point(0, 1, -1) pt11 = Point(1, 0, -1) pt21 = Point(1, 0, 1) pt31 = Point(1, 1, 1) pt41 = Point(1, 1, -1) prism = PrismRegular( top_facet=PolygonRegular(vertices=[pt10, pt20, pt30, pt40]), bot_facet=PolygonRegular(vertices=[pt11, pt21, pt31, pt41])) pt1 = Point(0, 0, 0) pt2 = Point(2, 0, 0) pt3 = Point(3, 0, 0) seg1 = Segment(pt1, pt2) seg2 = Segment(pt2, pt3) print(seg1.distance_to_prism(prism)) # they cross print(seg2.distance_to_prism(prism)) # should be 1
def test_distance_prism(): pt0 = Point(0, 0, -1) pt1 = Point(1, 0, -1) pt2 = Point(0, 0, 0) pt3 = Point(1, 0, 0) pt4 = Point(1, 1, 0) pt5 = Point(0, 1, 0) pt6 = Point(0, 0, 1) pt7 = Point(1, 0, 1) pt8 = Point(1, 1, 1) pt9 = Point(0, 1, 1) pt10 = Point(-4, 0, -3) pt11 = Point(-4, 1, -3) line1 = Line(pt_1=pt0, pt_2=pt1) line2 = Line(pt_1=pt10, pt_2=pt11) prism = PrismRegular( top_facet=PolygonRegular(vertices=[pt2, pt3, pt4, pt5]), bot_facet=PolygonRegular(vertices=[pt6, pt7, pt8, pt9])) print(line1.distance_to_prism(prism)) # should be 1 print(line2.distance_to_prism(prism)) # should be 5
def test_distance_point(): pt = Point(0, 0, 0) pt1 = Point(1, 0, 0) pt2 = Point(-1, 0, 0) seg = Segment(pt_1=pt1, pt_2=pt2) print(seg.distance_to_point(pt))