def near_bounds(contour): xMin, yMin, xMax, yMax = contour.bounds contour_start = (contour.first.x, contour.first.y) xMid = (xMin + xMax)/2 yMid = (yMin + yMax)/2 test_tuples = list(product((xMin, xMid, xMax), (yMin, yMid, yMax))) #test_tuples.pop(test_tuples.index((xMid, yMid))) test_lengths = [Line(node, contour_start).length for node in test_tuples] test_score = [item[0] for item in sorted(enumerate(test_lengths), key=lambda n: n[1])] return test_score
def quadrant(contour, quadrants): xMin, yMin, xMax, yMax = contour.bounds contour_center = ((xMin + xMax)/2, (yMin + yMax)/2) contour_start = (contour.first.x, contour.first.y) start_line = Line(contour_center, contour_start) start_angle = start_line.angle start_angle = start_angle if start_angle > 0 else 360 + start_angle for q in range(len(quadrants)): if int(start_angle) <= quadrants[q]: return q # return quadrant
def get_handle_length(self): '''Returns handle length and radii from base points.''' from typerig.core.func.geometry import line_intersect hl0 = Line(self.p0, self.p1) hl1 = Line(self.p2, self.p3) handle_intersection = Point(line_intersect(*self.tuple)) hl0i = Line(self.p0, handle_intersection) hl1i = Line(self.p3, handle_intersection) radius_0 = hl0i.length if not math.isnan(hl0i.length) else 0. radius_1 = hl1i.length if not math.isnan(hl1i.length) else 0. handle_0 = hl0.length if not math.isnan(hl0.length) else 0. handle_1 = hl0.length if not math.isnan(hl1.length) else 0. #ratio_0 = ratfrac(hl0.length, radius_0, 1.) #ratio_1 = ratfrac(hl1.length, radius_1, 1.) return (radius_0, handle_0), (radius_1, handle_1)
def solve_handle_distance_from_base(self, ratio=(.5, .5)): '''Finds new handle positions for given ratio from base points.''' from typerig.core.func.geometry import line_intersect handle_intersection = Point(line_intersect(*self.tuple)) hl0i = Line(self.p0, handle_intersection) hl1i = Line(self.p3, handle_intersection) new_p1 = hl0i.solve_point(ratio[0]) new_p2 = hl1i.solve_point(ratio[1]) return self.__class__(self.p0.tuple, new_p1.tuple, new_p2.tuple, self.p3.tuple)
def segments(self): obj_segments = [] for segment in self.point_segments: if len(segment) == 2: obj_segments.append(Line(*segment)) elif len(segment) == 3: # Placeholder for simple TT curves raise NotImplementedError elif len(segment) == 4: obj_segments.append(CubicBezier(*segment)) else: # Placeholder for complex TT curves raise NotImplementedError return obj_segments
def inter_layer_diff(glyph, layers): '''Return the maximum deviation of a node between given layers''' inter_layer_data = [] for layer_name in layers: inter_layer_data.append((layer_name, [n.asCoord() for n in glyph.nodes(layer_name, extend=eNode)])) inter_layer_distances = [] inter_layer_data.append(inter_layer_data[0]) for i in range(len(inter_layer_data) - 1): current_distances = [] layer_name_1, layer_nodes_1 = inter_layer_data[i] layer_name_2, layer_nodes_2 = inter_layer_data[i+1] for point_pair in zip(layer_nodes_1, layer_nodes_2): new_line = Line(*point_pair) current_distances.append(new_line.length) max_distance = max(current_distances) inter_layer_distances.append(((layer_name_1, layer_name_2), (current_distances.index(max_distance), max_distance))) return inter_layer_distances
def line(self): return Line(self.p0, self.p3)
def __init__(self, *argv): temp_line = Line(*argv) super(Vector, self).__init__(*temp_line.tuple) self.parent = argv
self.p3.tuple) def lerp_last(self, shift): diffBase = self.p0 - self.p3 diffP1 = self.p0 - self.p1 diffP2 = self.p0 - self.p2 if diffBase.x != 0: self.p1.x += (diffP1.x / diffBase.x) * shift.x self.p2.x += (diffP2.x / diffBase.x) * shift.x if diffBase.y != 0: self.p1.y += (diffP1.y / diffBase.y) * shift.y self.p2.y += (diffP2.y / diffBase.y) * shift.y self.p3 += shift return self.__class__(self.p0.tuple, self.p1.tuple, self.p2.tuple, self.p3.tuple) if __name__ == "__main__": a = Line(((113.73076629638672, 283.6538391113281), (357.96154022216797, 415.3846130371094))) b = CubicBezier((145.7924041748047, 367.8679504394531), (222.71548461914062, 405.3679504394531), (317.9077911376953, 376.5217971801758), (328.48471450805664, 229.40641021728516)) c = b.intersect_line(a) print(b.doSwap())