def sort1(group): # dumb sort. find closest endpoint to current endpoint, append, rinse, repeat sortedgroup = Group() original = group[:] sortedgroup.append(original[0]) del original[0] while original: p1 = sortedgroup[-1].points.xy[-1] bestvalue = 99999999999999999999 # i know this is stupid. ugh. bestindex = 0 reverseflag = False for index in range(len(original)): distance = distance_between(p1, original[index].points.xy[0]) if distance < bestvalue: bestvalue = distance bestindex = index reverseflag = False distance = distance_between(p1, original[index].points.xy[-1]) if distance < bestvalue: bestvalue = distance bestindex = index reverseflag = True if reverseflag: original[bestindex].points.xy.reverse() sortedgroup.append(original[bestindex]) del original[bestindex] return sortedgroup
def pupd_to_paths(hpgl): # slurp in PUs and PDs, and convert to a group of simple paths # probably fragile, but works fine on the output of pstoedit -f hpgl results = Group() builder = [] for command in hpgl: if isinstance(command, PU): if builder: # must be starting a new path, so stash the last coords = get_all_coordinates(builder) results.append(Path(coords)) builder = [] builder.append(command) elif isinstance(command, PD): builder.append(command) return results
def grid(width, height, width_divisions, height_divisions): '''Rectangular grid. - `width` : ``int`` or ``float``, width of the rectangle. - `height` : ``int`` or ``float``, height of the rectangle. - `width_divisions` : ``int``, number of horizontal equidistant partitions. - `height_divisions` : ``int``, number of vertical equidistant partitions. ''' ul_x = width bl_x = ul_x ur_x = ul_x + width ul_y = height ur_y = ul_y bl_y = ul_y - height x_step_size = width / width_divisions y_step_size = height / height_divisions g = Group() ## add horizontal lines for i in range(height_divisions + 1): step_y = y_step_size * i l = line((ul_x, ul_y - step_y), (ur_x, ur_y - step_y)) g.append(l) ## add vertical lines for i in range(width_divisions + 1): step_x = x_step_size * i l = line((ul_x + step_x, ul_y), (bl_x + step_x, bl_y)) g.append(l) return g
def grid(width, height, width_divisions,height_divisions): '''Rectangular grid. - `width` : ``int`` or ``float``, width of the rectangle. - `height` : ``int`` or ``float``, height of the rectangle. - `width_divisions` : ``int``, number of horizontal equidistant partitions. - `height_divisions` : ``int``, number of vertical equidistant partitions. ''' ul_x = width bl_x = ul_x ur_x = ul_x + width ul_y = height ur_y = ul_y bl_y = ul_y - height x_step_size = width / width_divisions y_step_size = height / height_divisions g = Group() ## add horizontal lines for i in range(height_divisions + 1): step_y = y_step_size * i l = line((ul_x, ul_y - step_y), (ur_x, ur_y - step_y)) g.append(l) ## add vertical lines for i in range(width_divisions + 1): step_x = x_step_size * i l = line((ul_x + step_x, ul_y), (bl_x + step_x, bl_y)) g.append(l) return g
## DEMO if __name__ == '__main__': from chiplotle.geometry.shapes.line import line from chiplotle.geometry.core.group import Group from chiplotle.tools import io from random import randrange #draw a bunch of lines that do not intersect no_intersections = Group() line_1 = line([randrange(0, 4000), randrange(0, 4000)], [randrange(0, 4000), randrange(0, 4000)]) no_intersections.append(line_1) while len(no_intersections) < 300: new_line = line([randrange(0, 4000), randrange(0, 4000)], [randrange(0, 4000), randrange(0, 4000)]) intersection = False for l in no_intersections: if get_line_intersection(new_line, l) != None: intersection = True break if intersection == False: no_intersections.append(new_line) print "found %d lines..." % len(no_intersections)
rads_incr = segmentation_map[segmentation_mode.lower()]() rads = start_angle arc = [] while rads < end_angle: coord = Coordinate(math.cos(rads), math.sin(rads)) coord = coord * Coordinate(width / 2.0, height / 2.0) rads += rads_incr arc.append(coord) ## NOTE: this is better than using rads <= end_angle since ## the equality does not always work as expected with floats ## due to rounding. last = Coordinate(math.cos(end_angle), math.sin(end_angle)) last *= Coordinate(width / 2.0, height / 2.0) arc.append(last) return Path(arc) ## RUN DEMO CODE if __name__ == '__main__': from chiplotle.tools import io from chiplotle.geometry.core.group import Group gr = Group() for radius in range(100, 1000, 10): ae = arc_ellipse(radius, radius * 2, 0, math.pi / 2, 5, 'arc') gr.append(ae) io.view(gr)
point_order = [] for i in range(0, num_points): point_num = (i * jump_size) % num_points point_order.append(point_num) corners = [corners[i] for i in point_order] return Polygon(corners) ## RUN DEMO CODE if __name__ == '__main__': from chiplotle.tools import io from chiplotle.geometry.shapes.star_crisscross import star_crisscross from chiplotle.geometry.core.group import Group from chiplotle.geometry.transforms.offset import offset gr1 = Group() for points in range(5, 26): for i in range(1, points): s = star_crisscross(1000, 1000, num_points = points, jump_size = i, find_valid_jump_size = False) offset(s, ((i - 1) * 1000, -(points - 5) * 1000)) gr1.append(s) io.view(gr1)
point_order = [] for i in range(0, num_points): point_num = (i * jump_size) % num_points point_order.append(point_num) corners = [corners[i] for i in point_order] return Polygon(corners) ## RUN DEMO CODE if __name__ == "__main__": from chiplotle.tools import io from chiplotle.geometry.shapes.star_crisscross import star_crisscross from chiplotle.geometry.core.group import Group from chiplotle.geometry.transforms.offset import offset gr1 = Group() for points in range(5, 26): for i in range(1, points): s = star_crisscross(1000, 1000, num_points=points, jump_size=i, find_valid_jump_size=False) offset(s, ((i - 1) * 1000, -(points - 5) * 1000)) gr1.append(s) io.view(gr1)
else: w_multi = quarter_width h_multi = quarter_height even = True point_x = (w_multi * cos_alpha); point_y = (h_multi * sin_alpha); corners.append((point_x, point_y)) degrees += degrees_incr corners.append(corners[0]) return Polygon(corners) ## RUN DEMO CODE if __name__ == '__main__': from chiplotle.tools import io from chiplotle.geometry.shapes.star_outline import star_outline from chiplotle.geometry.core.group import Group gr1 = Group() for i in range(5, 10): st = star_outline(100 * (i * i * i), 100 * (i * i * i), num_points = i) gr1.append(st) io.view(gr1)
rads_incr = segmentation_map[segmentation_mode.lower()]() rads = start_angle arc = [] while rads < end_angle: coord = Coordinate(math.cos(rads), math.sin(rads)) coord = coord * Coordinate(width / 2.0, height / 2.0) rads += rads_incr arc.append(coord) ## NOTE: this is better than using rads <= end_angle since ## the equality does not always work as expected with floats ## due to rounding. last = Coordinate(math.cos(end_angle), math.sin(end_angle)) last *= Coordinate(width / 2.0, height / 2.0) arc.append(last) return Path(arc) ## RUN DEMO CODE if __name__ == '__main__': from chiplotle.tools import io from chiplotle.geometry.core.group import Group gr = Group() for radius in range(100, 1000, 10): ae = arc_ellipse(radius, radius * 2, 0, math.pi/2, 5, 'arc') gr.append(ae) io.view(gr)
def arc_circle(radius, start_angle, end_angle, segments=100, segmentation_mode="2PI"): """ Constructs an arc from a circle with the given radius, and number of segments. Arc goes from start_angle to end_angle, both of which are in radians. - `segmentation_mode` : '2PI' or 'arc'. The first segments the whole circle into the given number of segments, the second segments the arc. """ radius = radius * 2.0 return arc_ellipse( radius, radius, start_angle, end_angle, segments, segmentation_mode ) ## RUN DEMO CODE if __name__ == "__main__": from chiplotle.tools import io gr = Group() for radius in range(100, 1000, 100): ac = arc_circle(radius, 1.0, math.pi) assert isinstance(ac, Path) gr.append(ac) io.view(gr, "png")
even = False else: w_multi = quarter_width h_multi = quarter_height even = True point_x = (w_multi * cos_alpha) point_y = (h_multi * sin_alpha) corners.append((point_x, point_y)) degrees += degrees_incr corners.append(corners[0]) return Polygon(corners) ## RUN DEMO CODE if __name__ == '__main__': from chiplotle.tools import io from chiplotle.geometry.shapes.star_outline import star_outline from chiplotle.geometry.core.group import Group gr1 = Group() for i in range(5, 10): st = star_outline(100 * (i * i * i), 100 * (i * i * i), num_points=i) gr1.append(st) io.view(gr1)
''' Constructs an arc from a circle with the given radius, and number of segments. Arc goes from start_angle to end_angle, both of which are in radians. - `segmentation_mode` : '2PI' or 'arc'. The first segments the whole circle into the given number of segments, the second segments the arc. ''' radius = radius * 2.0 return arc_ellipse(radius, radius, start_angle, end_angle, segments, segmentation_mode) ## RUN DEMO CODE if __name__ == '__main__': from chiplotle.tools import io gr = Group() for radius in range(100, 1000, 100): ac = arc_circle(radius, 1.0, math.pi) assert isinstance(ac, Path) gr.append(ac) io.view(gr, 'png')