def contains(self, point): test_point = self.min_point.copy() test_point.translate_XY(-1, -1) # -1 -1 should be outside the contour line = Line(test_point, point) next_list = self.vertex_list[1:] + self.vertex_list[0:1] # next point n = 0 for p1, p2 in zip(self.vertex_list, next_list): temp = Line(p1, p2) if line.fast_check_intersection(temp): n += 1 # if the line intersects at the point, add one if line.fast_on_line(p1): n -= 1 return n % 2 == 1
def test_is_parallel(self): line_list_from_points = self.get_line_list_from_points() line_list_from_functions = self.get_line_list_from_functions() self.assertTrue( Line.is_parallel(line_list_from_points[0], line_list_from_functions[0])) self.assertTrue( Line.is_parallel(line_list_from_points[1], line_list_from_functions[1])) self.assertFalse( Line.is_parallel(line_list_from_points[0], line_list_from_functions[1])) self.assertFalse( Line.is_parallel(line_list_from_points[1], line_list_from_functions[0]))
def vertex_close(self, point, distance): next_list = self.vertex_list[1:] + self.vertex_list[0:1] for p1, p2 in zip(self.vertex_list, next_list): if Line(p1, p2).distance(point) < distance: return True return False
def is_intersects(s1, s2): l1, l2 = LineBuilder.from_segment(s1), LineBuilder.from_segment(s2) intersect = Line.get_intersect(l1, l2) if not intersect: return False if s1.contain_point(intersect) and s2.contain_point(intersect): return True return False
def __init__(self, a: Vector, b: Vector, c: Vector): self.a = a # unused but who knows self.b = b # unused but who knows self.c = c # unused but who knows self.ab = Line(self.a, self.b) self.bc = Line(self.b, self.c) self.ca = Line(self.c, self.a)
def create_cube(size, dimensions, color="black", **kw): lines = [] for d in range(dimensions**2): point = Vector(*(size * (d & 2**i == 2**i) for i in range(dimensions))) for n in range(dimensions): if point.coordinates[n] == 0: lines.append( Line( point, Vector(*(point.coordinates[i] if i != n else size for i in range(dimensions))))) return Model(dimensions, [Primitive(l, color) for l in lines], **kw)
def self_vertex_close(self, point, distance): next_list = self.vertex_list[1:] + self.vertex_list[0:1] for p1, p2 in zip(self.vertex_list, next_list): dis = Line(p1, p2).distance_2(point) if dis == 0: continue if dis < distance: print("DIS", dis, point, "\tLINE:", p1, p2) return True return False
def create_arr_and_lines(): """ This function creates the lines for the animation. :return: list. """ numbers = list(range(line_gap - 2, lines_end + line_gap * 5, 5)) shuffle_arr(numbers) arr = [] for i in range(len(numbers)): start_point = Point(x_start + line_gap * i, y_start) end_point = Point(x_start + line_gap * i, y_start - numbers[i]) line = Line(start_point, end_point, VALUE_COLOR, line_width) arr.append(line) return arr
def get_vision(self, d): result = [] for p in sum([m.get_final_primitives() for m in self.models], []): result.append( Primitive(Line( p.line.vectors[0][:2] * reduce(lambda x, y: x * d / (y + d), p.line.vectors[0].coordinates[2:], 1), p.line.vectors[1][:2] * reduce(lambda x, y: x * d / (y + d), p.line.vectors[1].coordinates[2:], 1)), color=p.color, arrow=p.arrow)) return result
def generate_border_lines(image): contours, heirarchy = cv2.findContours(image, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE) contour_list = [] for contour, heirarchy in zip(contours, heirarchy[0]): line_list = [] pt0 = None for c in contour: # if this is the first point do not create a line if not pt0 is None: line_list.append(Line(pt0, p2=tuple(c[0]))) pt0 = tuple(c[0]) # add the last line from the last first point to the starting point line_list.append(Line(pt0, p2=tuple(contour[0][0]))) contour_list.append(Contour(line_list, heirarchy)) return contour_list
def create_axis(dimensions, size, color, **kw): return Model( dimensions, [ Primitive( Line( Vector.get_zero_vector(dimensions), Vector(*(size if i == d else 0 for i in range(dimensions))) ), color, arrow=LAST) for d in range(dimensions) ], **kw)
def intersection(self, line): points = [] next_p = self.vertex_list[1:] + self.vertex_list[0:1] for i, p in enumerate(self.vertex_list): temp = line.intersection(Line(p, next_p[i])) if temp is None: continue else: points.append(temp) return points
class Triangle: def __init__(self, a: Vector, b: Vector, c: Vector): self.a = a # unused but who knows self.b = b # unused but who knows self.c = c # unused but who knows self.ab = Line(self.a, self.b) self.bc = Line(self.b, self.c) self.ca = Line(self.c, self.a) def draw(self, canvas: Canvas): self.ab.draw(canvas) self.bc.draw(canvas) self.ca.draw(canvas)
def main(): r1 = Line((2, 0), angle=np.pi / 4) # for i in range(-2,3,1): # l1 = Line((i,2), (i+4,-2)) # print("\n:LINE", l1) # checkIntersection(r1, l1) # plt.plot(*l1.plot()) # plt.plot(*r1.plot()) # plt.show() # for i in range(-2,3,1): # l1 = Line((i,0.5), (i+4,-2)) # print("\n:LINE", l1) # checkIntersection(r1, l1) # plt.plot(*l1.plot()) # plt.plot(*r1.plot()) # plt.show() l1 = Line((1, 1), (1, -1)) for i in range(0, 12): angle = np.pi * i / 6 r1 = Line((0, 0), angle=angle) checkIntersection(r1, l1) plt.plot(*l1.plot()) plt.plot(*r1.plot()) plt.show()
def test_intersection(self): line = Line.from_points(Point(0, 0), Point(1, 1)) test_line = Line.from_points(Point(0, -1), Point(0, 1)) self.assertEquals(line.intersection(test_line), Point(0, 0)) test_line = Line.from_points(Point(-1, 1), Point(-1, 2)) self.assertEquals(line.intersection(test_line), Point(-1, -1)) test_line = Line.from_points(Point(2, 1), Point(0, 3)) self.assertEquals(line.intersection(test_line), Point(1.5, 1.5)) # Parallel lines test_line = Line.from_points(Point(1, 1), Point(2, 2)) self.assertEquals(line.intersection(test_line), None) # Same lines test_line = Line.from_points(Point(-1, -1), Point(0, 0)) self.assertEquals(line.intersection(test_line), None)
t0 = time.clock() p1_my = Point(1, 0) p2_my = Point(0, 1) p3_my = Point(1, 1) p4_my = Point(10, 1) p5_my = Point(1, 0) p6_my = Point(10, 0) # p7_my = Point(2, -2) # print(p7_my) # --------------test line -----------------# l1 = Line2D(p1_my, p2_my) l2 = Line(p3_my, p4_my) l3 = Line(p5_my, p6_my) # print(l3) # print(l2) # print(l1.args) # print(l1.direction) # print(l1.angle_between(l2)) # print(l1.is_parallel(l2)) # print(l3.is_parallel(l2)) # print(l3.is_parallel(l3)) # print(l3.unit) # print(l3.parallel_line(p3_my)) # print(l1.perpendicular_line(p3_my)) # print(l1.perpendicular_segment(p3_my)) # print(l1.projection(p3_my))
def exportAsLine(self, point): line = Line() line.constructor2(self.weights[0], self.weights[1], point) #line.constructor2(self.weights[1], self.weights[0], point); return line
def defining_line(self): return Line.from_points(self.center(), self.center() + self.heading_vector())
def test_is_intersect(self): lines = self.get_line_list_from_points() self.assertTrue(Line.get_intersect(lines[0], lines[1])) self.assertTrue(Line.get_intersect(lines[2], lines[1])) self.assertFalse(Line.get_intersect(lines[2], lines[3]))
def self_intersections(self): next_list = self.vertex_list[1:] + self.vertex_list[0:1] # next point new_list = [[]] intersection_list = [] current_contour = 0 print("START:", self.vertex_list[0]) # find the self intersections for p1, p2 in zip(self.vertex_list, next_list): line = Line(p1, p2) intersections = self.intersection(line) new_list[current_contour].append(p1) sort_intersections = [] # sort intersections for i in intersections: if not sort_intersections: sort_intersections.append(i) else: dis = p1.distance(i) # find the index and insert between for j, s in enumerate(sort_intersections): index = j if p1.distance(s) < dis: break # insert the index into the array sort_intersections = sort_intersections[0:index] + [ i ] + sort_intersections[index:] for i in sort_intersections: # if the intersection point exists, move to the previous contour if i in intersection_list: current_contour -= 1 # if the intersection point does not exist, create a new contour and save the point in each else: intersection_list.append(i) new_list[current_contour].append(i) current_contour += 1 new_list.append([i]) print(intersection_list) # return a list of all new contours return [Contour(x) for x in new_list]