def get_box(x, y): p1 = stitchcode.Point(x, y) p2 = stitchcode.Point(x, y + box_side) p3 = stitchcode.Point(x + box_side, y + box_side) p4 = stitchcode.Point(x + box_side, y) p5 = stitchcode.Point(x, y) return [p1, p2, p3, p4, p5]
def find_intersections(points, x, y, slope): intersection_set = [] line_segments = [] for i in range(0, len(points) - 1): if points[i].jump: continue suggested_point = intersection(LineSegment(points[i], points[i + 1]), stitchcode.Point(x, y), slope) if suggested_point is not None: if suggested_point == points[i]: index = get_surrounding_point_indexes(points, i) is_significant_corner = intersection( LineSegment(points[index[0]], points[index[1]]), stitchcode.Point(x, y), slope) if is_significant_corner is not None: intersection_set.append(suggested_point) else: intersection_set.append(suggested_point) intersection_set.append( stitchcode.Point(suggested_point.x, suggested_point.y)) elif suggested_point != points[i + 1]: intersection_set.append(suggested_point) #for n in range(0, len(intersection_set)-1, 2): # line_segments.append(LineSegment(intersection_set[n], intersection_set[n + 1])) while len(intersection_set) > 0: p1 = find_closest_points(intersection_set) line_segments.append( LineSegment(intersection_set[0], straight_stitch.copy(intersection_set[p1]))) if (p1 != 0): intersection_set.pop(p1) intersection_set.pop(0) return line_segments
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 midway_point(point1, point2, ratio): point3 = stitchcode.Point(point1.x, point1.y) if (point2.x - point1.x) != 0: point3.x = (ratio * (point2.x - point1.x)) + point1.x if (point2.y - point1.y) != 0: point3.y = (ratio * (point2.y - point1.y)) + point1.y return point3
def forward(d): while d != 0: def clamp(v): if (v>stmax): v = stmax return v dx = clamp(d) turtle.forward(dx) emb.addStitch(stitchcode.Point(turtle.pos()[0],turtle.pos()[1])) d -= dx
def generate(self): graph1 = [] graph2 = [] center = [] base = [] for i in range(0, len(self.messages)): center.append(stitchcode.Point(2000, i * 10, False)) base.append(0) unknown1, unknown2, base = self.draw_section(base, self.unknown) anger1, anger2, base = self.draw_section(base, self.anger) disgust1, disgust2, base = self.draw_section(base, self.disgust) fear1, fear2, base = self.draw_section(base, self.fear) joy1, joy2, base = self.draw_section(base, self.joy) sadness1, sadness2, base = self.draw_section(base, self.sadness) graph1.extend(sadness1) graph1[-1].color = 1 graph2.extend(sadness2) graph2[-1].color = 1 #joy1.reverse() graph1.extend(joy1) graph1[-1].color = 2 #joy2.reverse() graph2.extend(joy2) graph2[-1].color = 2 graph1.extend(fear1) graph1[-1].color = 3 graph2.extend(fear2) graph2[-1].color = 3 #disgust1.reverse() graph1.extend(disgust1) graph1[-1].color = 4 #disgust2.reverse() graph2.extend(disgust2) graph2[-1].color = 4 graph1.extend(anger1) graph1[-1].color = 5 graph2.extend(anger2) graph2[-1].color = 5 # unknown1.reverse() graph1.extend(unknown1) graph1[-1].color = 1 #unknown2.reverse() graph2.extend(unknown2) graph2[-1].color = 1 return graph1, graph2
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 getFont(charater, size, x_offset, y_offset): font_file = "open-sans/OpenSans-Light.ttf" font = ttx.TTFont(font_file) g = glyph.Glyph(glyphquery.glyphName(font, charater)) contours = g.calculateContours(font) points = [] for letter in contours: xy = (glyph.decomposeOutline(letter)) for p in xy: points.append( stitchcode.Point(x_offset + (p[0] / size), y_offset + (p[1] / size), False)) 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 write_words(self, words, unit_height, type, x, y): points = [] height = 0 if unit_height <= 2: if type == "in": points.extend( straight_stitch.straight_stitch([ stitchcode.Point(x, y, False), stitchcode.Point(x - len(words), y, False), stitchcode.Point(x, y, False) ], 30)) else: points.extend( straight_stitch.straight_stitch([ stitchcode.Point(x, y, False), stitchcode.Point(x + len(words), y, False), stitchcode.Point(x, y, False) ], 30)) i = 1 else: font_file = "Consolas.ttf" my_font = ttx.TTFont(font_file) count, height, rows = self.how_many_lines( words, unit_height * self.SECTION_HEIGHT) if (unit_height * self.SECTION_HEIGHT / count < self.SECTION_HEIGHT): print("ERROR") for i in range(0, len(rows)): m_y = y - ((height) * (i)) points.append(stitchcode.Point(x, m_y, False)) #print "height: {} row: {}".format(height,rows[i]) letters, width = font.fill_stitch_string( rows[i], 1462 / (height * 0.8), x, m_y, my_font, 1, 10, 100) #letters, width =font.satin_stitch_string(rows[i], 1462 / (height), x, m_y, my_font) if type == "in": letters = [ stitchcode.Point(p.x - width, p.y) for p in letters ] #(message, size, x_offset, y_offset, font, slope, density, p_distance): points.extend(letters) return height, points
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 find_starting_point(points, slope): min_x = points[0].x min_y = points[0].y max_x = points[0].x max_y = points[0].y for i in range(1, len(points)): if points[i].x < min_x: min_x = points[i].x elif points[i].x > max_x: max_x = points[i].x if points[i].y < min_y: min_y = points[i].y elif points[i].y > max_y: max_y = points[i].y if slope == 0: return stitchcode.Point(min_x, min_y), stitchcode.Point(min_x, max_y) if slope < 0: finish_x = max_x - ((1 / slope) * abs(max_y - min_y)) return stitchcode.Point(min_x, min_y), stitchcode.Point(finish_x, min_y) if slope > 0: start_x = min_x - ((1 / slope) * abs(max_y - min_y)) return stitchcode.Point(start_x, min_y), stitchcode.Point(max_x, min_y)
if __name__ == "__main__": emb = stitchcode.Embroidery() (x, y) = (0, 0) for i in range(0, 4000): sx = numpy.random.normal(0, 1) * maxstep sy = numpy.random.normal(0, 1) * maxstep if (x + sx < 0): sx *= -1 if (x + sx > maxx): sx *= -1 x += sx if (y + sy < 0): sy *= -1 if (y + sy > maxy): sy *= -1 y += sy print x, y, "/", sx, sy emb.addStitch(stitchcode.Point(x, y)) emb.scale(1) emb.translate_to_origin() emb.save_as_exp("random-walk.exp") emb.save_as_png("random-walk.png", False)
def addPoint(self, x, y): if self.isDistanceOK(x, y): self.points.append((x, y, self.jump)) self.last_point = (x, y, self.jump) self.emb.addStitch(stitchcode.Point(x, y, self.jump)) self.jump = False
def draw_section(self, base, new_points): line1 = [] line2 = [] for i in range(0, len(self.messages)): if self.type[i] == 'in': x_top = self.middle - (( (base[i] + new_points[i]) * self.spacing)) x_bottom = self.middle - (base[i] * self.spacing) y_top = i * self.unit_height y_center = (i * self.unit_height) + (self.unit_height / 2) line1.extend( straight_stitch.straight_stitch([ stitchcode.Point(self.middle, y_center, False), stitchcode.Point(x_top, y_center, False) ], 30)) line1.extend( satin_stitch.satin_stitch([ stitchcode.Point(x_top, y_top, False), stitchcode.Point(x_bottom, y_top, False) ], 6, self.unit_height)) # satin_stitch(input_points, penetration_distance, width): line1.extend( straight_stitch.straight_stitch([ stitchcode.Point(x_bottom, y_center, False), stitchcode.Point(self.middle, y_center, False) ], 30)) else: x_top = self.middle + ( (base[i] + new_points[i]) * self.spacing) x_bottom = self.middle + (base[i] * self.spacing) y_top = (i + 1) * self.unit_height y_center = (i * self.unit_height) + (self.unit_height / 2) line2.extend( straight_stitch.straight_stitch([ stitchcode.Point(self.middle, y_center, False), stitchcode.Point(x_top, y_center, False) ], 30)) line2.extend( satin_stitch.satin_stitch([ stitchcode.Point(x_top, y_top, False), stitchcode.Point(x_bottom, y_top, False) ], 6, self.unit_height)) # satin_stitch(input_points, penetration_distance, width): line2.extend( straight_stitch.straight_stitch([ stitchcode.Point(x_bottom, y_center, False), stitchcode.Point(self.middle, y_center, False) ], 30)) base[i] = base[i] + new_points[i] return line1, line2, base
emb.flatten() emb.save("Output/LineExample.exp") emb.save("Output/LineExample.png") emb.save("Output/LineExample.dst") if __name__ == "__main__": points = [] for i in range(0, 8, 2): for j in range(0, 8): box_points = [] for l in range(0, (i + 1)): box = (get_box(j * box_offset, i * box_offset)) box_points = straight_stitch(box, (j + 7) * 2) points.extend(box_points) p1 = stitchcode.Point(box_points[0].x, box_points[0].y, True) p2 = stitchcode.Point(box_points[len(box_points) - 1].x, box_points[len(box_points) - 1].y, True) points.append(p2) points.append( stitchcode.Point(8 * box_offset - 20, i * box_offset, True)) points.append( stitchcode.Point(8 * box_offset - 20, (i + 1) * box_offset, True)) for j in range(0, 8): for l in range(0, (i + 2)): box = get_box(((7 * box_offset) - (j * box_offset)), ((i + 1) * box_offset)) box_points = straight_stitch(box, 25 - (j * 2)) points.extend(box_points) p1 = stitchcode.Point(box_points[0].x, box_points[0].y, True)
def copy(p): return stitchcode.Point(p.x, p.y)
from scipy import interpolate stepsize_x = 2 stepsize_y = 30 def getDistance(x1, y1, x2, y2): dx = x1 - x2 dy = y1 - y2 dist = math.sqrt(dx * dx + dy * dy) return dist if __name__ == "__main__": emb = stitchcode.Embroidery() emb.addStitch(stitchcode.Point(0, 0)) last_y = 0 x = 0 for j in range(0, 512): if j % 1 == 0: y = numpy.random.normal(0, 0.5) * 127 else: y = 0 d = getDistance(0, last_y, stepsize_y, y) steps = max(1, int(round(d / stepsize_y))) yarr = (last_y, y) xarr = (0, steps + 1) xnew = range(0, steps + 1)
import stitchcode from straight_stitch import straight_stitch from straight_stitch import line from straight_stitch import copy def write_to_file(emb): emb.translate_to_origin() #emb.scale(1) emb.flatten() emb.save("Output/colorChange.exp") emb.save("Output/colorChange.png") if __name__ == "__main__": points = line(stitchcode.Point(0, 0, True), stitchcode.Point(0, 500, True), 20) emb = stitchcode.Embroidery() print("HERE") i = 0 for p in points: p.color = i emb.addStitch(p) i = i + 1 points.reverse() i = 0 for p in points: emb.addStitch(stitchcode.Point(20, p.y, False, i)) i = i + 1 write_to_file(emb)
import fill_stitch import stitchcode def write_to_file(emb): emb.translate_to_origin() #emb.scale(1) emb.flatten() emb.save("Output/reTest1.exp") emb.save("Output/reTest1.png") emb.save("Output/reTest1.dst") if __name__ == "__main__": ps = [] ps.append(stitchcode.Point(0, 0)) ps.append(stitchcode.Point(0, 50)) ps.append(stitchcode.Point(50, 50)) ps.append(stitchcode.Point(50, 0)) ps.append(stitchcode.Point(0, 0)) points = fill_stitch.fill_stitch(ps, 1, 3, 5) emb = stitchcode.Embroidery() for p in points: emb.addStitch(p) write_to_file(emb)
def get_line_direction(a, b): return stitchcode.Point((a.x - b.x), (a.y - b.y))
def clamp(v): if (v > stmax): v = stmax return v dx = clamp(d) turtle.forward(dx) emb.addStitch(stitchcode.Point(turtle.pos()[0], turtle.pos()[1])) d -= dx def circle(d, steps): for i in range(steps): forward(d) turtle.right(360 / steps) if __name__ == "__main__": emb = stitchcode.Embroidery() turtle.speed(2000) steps = 100 s = 50 emb.addStitch(stitchcode.Point(0, 0)) for i in range(30): circle(s, 40) turtle.right(12) #s += 1 emb.translate_to_origin() emb.save_as_exp("circles.exp") emb.save_as_png("circles.png", True)
#emb.scale(1) emb.flatten() emb.save("Output/SatinExample.exp") emb.save("Output/SatinExample.png") emb.save("Output/SatinExample.dst") if __name__ == "__main__": points = [] for i in range(0, 8, 2): for j in range(0, 8): box_points = [] box = (get_box(j * box_offset, i * box_offset)) box_points = satin_stitch(box, 3 + j, (i * 3) + 10) points.extend(box_points) p2 = stitchcode.Point(box_points[len(box_points) - 1].x, box_points[len(box_points) - 1].y) points.append(p2) points.append(stitchcode.Point(8 * box_offset - 20, i * box_offset)) points.append( stitchcode.Point(8 * box_offset - 20, (i + 1) * box_offset)) for j in range(0, 8): box = get_box(((7 * box_offset) - (j * box_offset)), ((i + 1) * box_offset)) box_points = satin_stitch(box, 10 - j, (i * 3) + 10) points.extend(box_points) p2 = stitchcode.Point(box_points[len(box_points) - 1].x, box_points[len(box_points) - 1].y) points.append(p2) emb = stitchcode.Embroidery() for p in points:
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)
(0, 2)] def heart(center, size): points= [] for p in heartPoints: points.append(stitchcode.Point(center.x + (p[0] * size), center.y + (p[1] * size))) return points; def copy(p): return stitchcode.Point(p.x, p.y) if __name__ == "__main__": points = [] radius = 200 center = stitchcode.Point(radius, radius) top = stitchcode.Point(radius, radius*2) bottom = stitchcode.Point(radius, 0) left = stitchcode.Point(0, radius) for j in range(radius, 40, -10): for i in range(0, 61): angle = (2 * math.pi * i)/60 points.append(stitchcode.Point(center.x + (math.cos(angle)*j), center.y + (math.sin(angle)*j))) points.extend(straight_stitch([copy(center), copy(top), copy(center), copy(left), copy(center), copy(bottom), copy(center)], 10, 0)) data = [] for i in range(0, len(heart_rate)): angle = ((2 * math.pi * i) / 60 - (math.pi/2)) * -1 data.append(stitchcode.Point(center.x + (math.cos(angle) * heart_rate[i]), center.y + (math.sin(angle) * heart_rate[i]))) points.extend(satin_stitch(data, 2, 15)) points.append(copy(center))
emb.translate_to_origin() #emb.scale(1) emb.flatten() emb.save("Output/FillExample.exp") emb.save("Output/FillExample.png") emb.save("Output/FillExample.dst") if __name__ == "__main__": points = [] for i in range(0, 8, 2): for j in range(0, 8): box_points = [] box = (get_box(j * box_offset, i * box_offset)) box_points = fill_stitch(box, 1, 1, j + 3, (i * 5) + 10) p1a = stitchcode.Point(box[0].x, box[0].y) points.append(p1a) p1b = stitchcode.Point(box[1].x, box[1].y) points.append(p1b) points.extend(box_points) points.append(stitchcode.Point(box[3].x, box[3].y)) points.append(stitchcode.Point(8 * box_offset - 40, i * box_offset)) points.append( stitchcode.Point(8 * box_offset - 40, (i + 1) * box_offset)) for j in range(0, 8): box = get_box(((7 * box_offset) - (j * box_offset)), ((i + 1) * box_offset)) box_points = fill_stitch(box, 1, 1, 10 - j, (i * 5) + 10) p1a = stitchcode.Point(box[0].x, box[0].y) points.append(p1a)
def heart(center, size): points= [] for p in heartPoints: points.append(stitchcode.Point(center.x + (p[0] * size), center.y + (p[1] * size))) return points;
circle.append(circle[0]) return circle def write_to_file(emb, file_name): emb.translate_to_origin() # emb.scale(1) emb.flatten() emb.save("Output/" + file_name + ".exp") emb.save("Output/" + file_name + ".png") if __name__ == "__main__": logo = ILABLogo() count = 0 stitchpoints = [] shape = [] for i in logo.shapes: for j in i: shape.append(stitchcode.Point(j[0] * 30, j[1] * 30)) stitchpoints.extend(fill_stitch(shape, 1, 1, 2, 100)) shape = [] stitchpoints[-1].color = count count = count + 1 emb = stitchcode.Embroidery() for p in stitchpoints: emb.addStitch(copy(p)) write_to_file(emb, "ilab")
def write_to_file(emb, file_name): emb.translate_to_origin() # emb.scale(1) emb.flatten() emb.save("Output/" + file_name + ".exp") emb.save("Output/" + file_name + ".png") if __name__ == "__main__": potato = Potato(Potato.SQUARE) stitchpoints = [] scale = 60 stitchpoints.extend( fill_stitch([ stitchcode.Point(x * scale, y * scale) for (x, y) in potato.circle ], 1, 1, 2, 100)) stitchpoints[-1].color = 1 stitchpoints.extend( fill_stitch( [stitchcode.Point(x * scale, y * scale) for (x, y) in potato.body], 1, 1, 2, 100)) stitchpoints[-1].color = 2 stitchpoints.extend( fill_stitch([ stitchcode.Point(x * scale, y * scale) for (x, y) in potato.shape ], 1, 1, 2, 100)) stitchpoints[-1].color = 3 it = 4 for spot in potato.spots: stitchpoints.extend(