| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  | """ | 
					
						
							| 
									
										
										
										
											2019-05-03 03:34:49 +00:00
										 |  |  | Alexandre B A Villares - https://abav.lugaralgum.com/sketch-a-day | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | - Unfolding piramids | 
					
						
							|  |  |  | """ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # add_library('GifAnimation') | 
					
						
							|  |  |  | # from gif_exporter import gif_export | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-03 03:32:00 +00:00
										 |  |  | CUT_STROKE = color(255, 0, 0) | 
					
						
							|  |  |  | FOLD_STROKE = color(0, 0, 255) | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-03 03:32:00 +00:00
										 |  |  | outer_radius, inner_radius = 100, 50 | 
					
						
							|  |  |  | sides = 5 | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def setup(): | 
					
						
							|  |  |  |     size(400, 600, P3D) | 
					
						
							|  |  |  |     hint(ENABLE_DEPTH_TEST) | 
					
						
							|  |  |  |     hint(ENABLE_DEPTH_SORT) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def draw(): | 
					
						
							|  |  |  |     background(240) | 
					
						
							|  |  |  |     pushMatrix() | 
					
						
							| 
									
										
										
										
											2019-05-03 03:32:00 +00:00
										 |  |  |     translate(width / 2, height / 4 + 50) | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  |     rotateX(radians(45)) | 
					
						
							|  |  |  |     rotateZ(radians(frameCount / 3.)) | 
					
						
							|  |  |  |     fill(255, 200) | 
					
						
							|  |  |  |     stroke(0) | 
					
						
							|  |  |  |     strokeWeight(2) | 
					
						
							| 
									
										
										
										
											2019-05-03 03:32:00 +00:00
										 |  |  |     # draw 3D piramid and get points | 
					
						
							|  |  |  |     points = piramid_3D(sides, outer_radius, inner_radius) | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  |     popMatrix() | 
					
						
							| 
									
										
										
										
											2019-05-03 03:32:00 +00:00
										 |  |  |     # draw unfolded 2D | 
					
						
							|  |  |  |     translate(width / 2, height *  3 / 4 - 50) | 
					
						
							|  |  |  |     piramid_2D(points) | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-03 03:32:00 +00:00
										 |  |  | def piramid_3D(np, ext_r, base_r): | 
					
						
							|  |  |  |     # calculando os points | 
					
						
							|  |  |  |     points = [] | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  |     n = np * 2 | 
					
						
							|  |  |  |     for i in range(n): | 
					
						
							|  |  |  |         ang = radians(i * 360. / n) | 
					
						
							|  |  |  |         if i % 2 == 0: | 
					
						
							| 
									
										
										
										
											2019-05-03 03:32:00 +00:00
										 |  |  |             r = base_r | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2019-05-03 03:32:00 +00:00
										 |  |  |             r = ext_r | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  |         x = sin(ang) * r | 
					
						
							|  |  |  |         y = cos(ang) * r | 
					
						
							| 
									
										
										
										
											2019-05-03 03:32:00 +00:00
										 |  |  |         points.append((x, y)) | 
					
						
							|  |  |  |     # edges da base | 
					
						
							|  |  |  |     base_points = points[::2] | 
					
						
							|  |  |  |     o_base_points = base_points[1:] + [base_points[0]] | 
					
						
							|  |  |  |     base_edges = zip(base_points, o_base_points) | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  |     # calculo da altura | 
					
						
							| 
									
										
										
										
											2019-05-03 03:32:00 +00:00
										 |  |  |     (p0x, p0y), (p1x, p1y) = points[0], points[1] | 
					
						
							|  |  |  |     side = dist(p0x, p0y, p1x, p1y) | 
					
						
							|  |  |  |     h_squared = side * side - base_r * base_r | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  |     if h_squared > 0:  # se a altura viavel | 
					
						
							|  |  |  |         h = sqrt(h_squared) | 
					
						
							| 
									
										
										
										
											2019-05-03 03:32:00 +00:00
										 |  |  |         for edge in base_edges: | 
					
						
							|  |  |  |             p1, p2 = edge | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  |             beginShape() | 
					
						
							|  |  |  |             vertex(*p1) | 
					
						
							|  |  |  |             vertex(*p2) | 
					
						
							|  |  |  |             vertex(0, 0, h) | 
					
						
							|  |  |  |             endShape(CLOSE) | 
					
						
							| 
									
										
										
										
											2019-05-03 03:34:49 +00:00
										 |  |  |     # always draws base | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  |     beginShape() | 
					
						
							| 
									
										
										
										
											2019-05-03 03:32:00 +00:00
										 |  |  |     for pt in base_points: | 
					
						
							|  |  |  |         vertex(*pt) | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  |     endShape(CLOSE) | 
					
						
							| 
									
										
										
										
											2019-05-03 03:34:49 +00:00
										 |  |  |     # return points for 2D! | 
					
						
							| 
									
										
										
										
											2019-05-03 03:32:00 +00:00
										 |  |  |     return points | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-03 03:32:00 +00:00
										 |  |  | def piramid_2D(points): | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  |     noFill() | 
					
						
							| 
									
										
										
										
											2019-05-03 03:34:49 +00:00
										 |  |  |     # base fold lines | 
					
						
							| 
									
										
										
										
											2019-05-03 03:32:00 +00:00
										 |  |  |     stroke(FOLD_STROKE) | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  |     beginShape() | 
					
						
							| 
									
										
										
										
											2019-05-03 03:32:00 +00:00
										 |  |  |     for pt in points[::2]: | 
					
						
							|  |  |  |         vertex(*pt) | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  |     endShape(CLOSE) | 
					
						
							| 
									
										
										
										
											2019-05-03 03:34:49 +00:00
										 |  |  |     # lateral edges | 
					
						
							| 
									
										
										
										
											2019-05-03 03:32:00 +00:00
										 |  |  |     o_points = points[1:] + [points[0]] | 
					
						
							|  |  |  |     edges = zip(points, o_points) | 
					
						
							|  |  |  |     for i, edge in enumerate(edges): | 
					
						
							|  |  |  |         p1, p2 = edge | 
					
						
							|  |  |  |         stroke(CUT_STROKE) | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  |         if i % 2 == 0: | 
					
						
							|  |  |  |         # abas de cola | 
					
						
							|  |  |  |             glue_tab(p2, p1, 10, ) | 
					
						
							| 
									
										
										
										
											2019-05-03 03:32:00 +00:00
										 |  |  |             # FOLD_STROKE | 
					
						
							|  |  |  |             stroke(FOLD_STROKE) | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  |             line(p2[0], p2[1], p1[0], p1[1]) | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2019-05-03 03:32:00 +00:00
										 |  |  |             # outra edge cortada | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  |             line(p1[0], p1[1], p2[0], p2[1]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def glue_tab(p1, p2, tab_w, cut_ang=QUARTER_PI / 3): | 
					
						
							|  |  |  |     """ | 
					
						
							| 
									
										
										
										
											2019-05-03 03:36:46 +00:00
										 |  |  |     draws a trapezoidal or triangular glue tab along edge defined by p1 and p2, | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  |     with width tab_w and cut angle a | 
					
						
							|  |  |  |     """ | 
					
						
							|  |  |  |     al = atan2(p1[0] - p2[0], p1[1] - p2[1]) | 
					
						
							|  |  |  |     a1 = al + cut_ang + PI | 
					
						
							|  |  |  |     a2 = al - cut_ang | 
					
						
							| 
									
										
										
										
											2019-05-03 03:32:00 +00:00
										 |  |  |     # calculate cut_len to get the base_rght tab width | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  |     cut_len = tab_w / sin(cut_ang) | 
					
						
							|  |  |  |     f1 = (p1[0] + cut_len * sin(a1), | 
					
						
							|  |  |  |           p1[1] + cut_len * cos(a1)) | 
					
						
							|  |  |  |     f2 = (p2[0] + cut_len * sin(a2), | 
					
						
							|  |  |  |           p2[1] + cut_len * cos(a2)) | 
					
						
							|  |  |  |     edge_len = dist(p1[0], p1[1], p2[0], p2[1]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if edge_len > 2 * cut_len * cos(cut_ang):    # 'normal' trapezoidal tab | 
					
						
							|  |  |  |         beginShape() | 
					
						
							|  |  |  |         vertex(*p1)    # vertex(p1[0], p1[1]) | 
					
						
							|  |  |  |         vertex(*f1) | 
					
						
							|  |  |  |         vertex(*f2) | 
					
						
							|  |  |  |         vertex(*p2) | 
					
						
							|  |  |  |         endShape() | 
					
						
							| 
									
										
										
										
											2019-05-03 03:36:46 +00:00
										 |  |  |     else:    # short triangular tab | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  |         fm = ((f1[0] + f2[0]) / 2, (f1[1] + f2[1]) / 2) | 
					
						
							|  |  |  |         beginShape() | 
					
						
							|  |  |  |         vertex(*p1) | 
					
						
							|  |  |  |         vertex(*fm)    # middle way of f1 and f2 | 
					
						
							|  |  |  |         vertex(*p2) | 
					
						
							|  |  |  |         endShape() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def keyPressed(): | 
					
						
							| 
									
										
										
										
											2019-05-03 03:32:00 +00:00
										 |  |  |     global inner_radius, outer_radius, sides | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  |     if keyCode == UP: | 
					
						
							| 
									
										
										
										
											2019-05-03 03:32:00 +00:00
										 |  |  |         outer_radius += 5 | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  |     if keyCode == DOWN: | 
					
						
							| 
									
										
										
										
											2019-05-03 03:32:00 +00:00
										 |  |  |         outer_radius -= 5 | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  |     if keyCode == LEFT: | 
					
						
							| 
									
										
										
										
											2019-05-03 03:32:00 +00:00
										 |  |  |         inner_radius += 5 | 
					
						
							| 
									
										
										
										
											2019-05-04 01:17:48 +00:00
										 |  |  |     if keyCode == RIGHT: | 
					
						
							| 
									
										
										
										
											2019-05-03 03:32:00 +00:00
										 |  |  |         inner_radius -= 5 | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  |     if key == "+": | 
					
						
							| 
									
										
										
										
											2019-05-03 03:32:00 +00:00
										 |  |  |         sides += 1 | 
					
						
							|  |  |  |     if key == "-" and sides > 3: | 
					
						
							|  |  |  |         sides -= 1 | 
					
						
							| 
									
										
										
										
											2019-05-03 01:04:29 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     # if key == "g": | 
					
						
							|  |  |  |     #     gif_export(GifMaker, filename=SKETCH_NAME) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def settings(): | 
					
						
							|  |  |  |     from os import path | 
					
						
							|  |  |  |     global SKETCH_NAME | 
					
						
							|  |  |  |     SKETCH_NAME = path.basename(sketchPath()) | 
					
						
							|  |  |  |     OUTPUT = ".gif" | 
					
						
							|  |  |  |     println( | 
					
						
							|  |  |  |         """ | 
					
						
							|  |  |  |  | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | [{0}](https://github.com/villares/sketch-a-day/tree/master/{2}/{0}) [[Py.Processing](https://villares.github.io/como-instalar-o-processing-modo-python/index-EN)] | 
					
						
							|  |  |  | """.format(SKETCH_NAME, OUTPUT, year()) | 
					
						
							|  |  |  |     ) |