def intersection(line_segment, point, slope): line_seg_direction = get_line_direction(line_segment.p2, line_segment.p1) step1 = ((point.x - line_segment.p1.x) * slope) step2 = (line_segment.p1.y - point.y) + step1 step3 = (line_seg_direction.x * slope) step4 = (step3 - line_seg_direction.y) if step4 != 0: t2 = step2 / step4 if (t2 >= 0) and (t2 <= 1): # Line intersect at single point x = (line_segment.p1.x + (t2 * line_seg_direction.x)) y = (line_segment.p1.y + (t2 * line_seg_direction.y)) return stitchcode.Point(x, y) else: p_direction = get_line_direction(line_segment.p1, point) if (p_direction.x == 0) and (p_direction.y == 0): # Lines entirely overlap return line_segment.p1 else: if straight_stitch.distance(line_segment.p1, point) < straight_stitch.distance( line_segment.p2, point): far = line_segment.p2 near = line_segment.p1 else: far = line_segment.p1 near = line_segment.p2 side1 = straight_stitch.distance( far, near) + straight_stitch.distance(near, point) side2 = straight_stitch.distance(far, point) if abs(side1 - side2) < .0001: #Line completely overlaps return line_segment.p1 #Lines do not intersect return None
def is_along(line_seg, point, tolerance): ab = straight_stitch.distance(line_seg.p1, line_seg.p2) ap = straight_stitch.distance(line_seg.p1, point) pb = straight_stitch.distance(line_seg.p2, point) if abs(ap + pb - ab) < tolerance: return True return False
def find_closest_points(points): p2 = 1 if len(points) == 1: return 0 close_dist = straight_stitch.distance(points[0], points[1]) for j in range(1, len(points)): if 0 is not p2: dist = straight_stitch.distance(points[0], points[j]) if close_dist > dist: close_dist = dist p2 = j return p2
def satin_line(point1, point2, density, width): points = [] line_points = straight_stitch.line(point1, point2, density) for i in range(0, len(line_points) - 1): points.append(line_points[i]) if (straight_stitch.distance(line_points[i], line_points[i + 1]) != 0): points.append( single_satin_stitch(line_points[i], line_points[i + 1], width)) points.append(line_points[len(line_points) - 1]) return points
def satin_corner(point1, point2, point3, penitration_length, width): if (are_points_too_close(point1, point2) or are_points_too_close(point2, point3) or are_points_too_close(point1, point3)): return p1 = point2 p2 = single_satin_stitch(point2, blah(point1, point2, width), width) p3 = single_satin_stitch(point2, point3, width) p12 = straight_stitch.distance(p1, p2) p13 = straight_stitch.distance(p1, p3) p23 = straight_stitch.distance(p2, p3) z1 = p2.x - p1.x w1 = p2.y - p1.y z2 = p3.x - p1.x w2 = p3.y - p1.y theta = math.acos(((z1 * z2) + (w1 * w2)) / (math.sqrt(z1**2 + w1**2) * math.sqrt(z2**2 + w2**2))) theta2 = math.acos((p12**2 + p13**2 - p23**2) / (2 * p12 * p13)) cross = (point2.x - point1.x) * (point3.y - point1.y) - ( point2.y - point1.y) * (point3.x - point1.x) if (cross > 0 or (abs(theta - math.pi) < 0.001)): return [] sub_division = (width * theta) / penitration_length if sub_division == 0: return partial_angle = (theta / sub_division) points = [] sub_division = int(math.ceil(sub_division)) for i in range(0, sub_division): x = p2.x - p1.x y = p2.y - p1.y magx = x * math.cos(partial_angle * i + math.pi) + y * math.sin( partial_angle * i + math.pi) magy = -1 * x * math.sin(partial_angle * i + math.pi) + y * math.cos( partial_angle * i + math.pi) points.append(stitchcode.Point(p1.x - magx, p1.y - magy)) points.append( stitchcode.Point(p1.x - (magx * 0.2), p1.y - (magy * 0.2))) return points
def single_satin_stitch(point1, point2, width): if point1.x < point2.x: outside_y = 1 else: outside_y = -1 if point1.y < point2.y: outside_x = -1 else: outside_x = 1 distance = straight_stitch.distance(point1, point2) x = ((((abs(point2.y - point1.y) * width) / distance) * outside_x) + point1.x) y = ((((abs(point2.x - point1.x) * width) / distance) * outside_y) + point1.y) return stitchcode.Point(x, y, False)
def get_char(character, size, x_offset, y_offset, font): g = glyph.Glyph(glyphquery.glyphName(font, character)) contours = g.calculateContours(font) seen = set() coord = [] points = [] for parts in contours: xy = (glyph.decomposeOutline(parts)) for p in xy: new_point = stitchcode.Point(x_offset + (p[0] / size), y_offset + (p[1] / size), False) if len(points) is 0 or straight_stitch.distance( points[-1], new_point) > 2: points.append(new_point) if len(points) is not 0: points[len(points) - 1].jump = True coord.append(points) points = [] if len(points) is not 0: points[len(points) - 1].jump = True coord.append(points) return coord
def blah(p1, p2, width): z = p2.x - p1.x w = p2.y - p1.y dist = straight_stitch.distance(p1, p2) m = (dist + width) / dist return stitchcode.Point(p1.x + z * m, p1.y + w * m)