kopia lustrzana https://github.com/villares/sketch-a-day
				
				
				
			2020_03_01
							rodzic
							
								
									6cb9673c47
								
							
						
					
					
						commit
						66c31d4bcb
					
				|  | @ -0,0 +1,149 @@ | |||
| # -*- coding: utf-8 -*- | ||||
| from __future__ import division | ||||
| 
 | ||||
| ROTATION = {0: 0, | ||||
|             BOTTOM: 0, | ||||
|             DOWN: 0, | ||||
|             1: HALF_PI, | ||||
|             LEFT: HALF_PI, | ||||
|             2: PI, | ||||
|             TOP: PI, | ||||
|             UP: PI, | ||||
|             3: PI + HALF_PI, | ||||
|             RIGHT: PI + HALF_PI, | ||||
|             BOTTOM + RIGHT: 0, | ||||
|             DOWN + RIGHT: 0, | ||||
|             DOWN + LEFT: HALF_PI, | ||||
|             BOTTOM + LEFT: HALF_PI, | ||||
|             TOP + LEFT: PI, | ||||
|             UP + LEFT: PI, | ||||
|             TOP + RIGHT: PI + HALF_PI, | ||||
|             UP + RIGHT: PI + HALF_PI, | ||||
|             } | ||||
| 
 | ||||
| def quarter_circle(x, y, radius, quadrant): | ||||
|     circle_arc(x, y, radius, ROTATION[quadrant], HALF_PI) | ||||
| 
 | ||||
| def half_circle(x, y, radius, quadrant): | ||||
|     circle_arc(x, y, radius, ROTATION[quadrant], PI) | ||||
| 
 | ||||
| def circle_arc(x, y, radius, start_ang, sweep_ang): | ||||
|     arc(x, y, radius * 2, radius * 2, start_ang, start_ang + sweep_ang) | ||||
| 
 | ||||
| def b_circle_arc(x, y, radius, start_ang, sweep_ang, mode=0): | ||||
|     """ | ||||
|     Alternative interface for b_arc | ||||
|     """ | ||||
|     b_arc(x, y, radius * 2, radius * 2, start_ang, start_ang + sweep_ang, | ||||
|           mode=mode) | ||||
| 
 | ||||
| def b_arc(cx, cy, w, h, start_angle, end_angle, mode=0): | ||||
|     """ | ||||
|     A bezier approximation of an arc | ||||
|     using the same signature as the original Processing arc() | ||||
|     mode: 0 "normal" or stand-alone arc, using beginShape() and endShape() | ||||
|           1 "middle" used in recursive call of smaller arcs | ||||
|           2 "naked" like normal, but without beginShape() and endShape() | ||||
|              for use inside a larger PShape | ||||
|     """ | ||||
|     theta = end_angle - start_angle | ||||
|     # Compute raw Bezier coordinates. | ||||
|     if mode != 1 or abs(theta) <= QUARTER_PI: | ||||
|         x0 = cos(theta / 2.0) | ||||
|         y0 = sin(theta / 2.0) | ||||
|         x3 = x0 | ||||
|         y3 = 0 - y0 | ||||
|         x1 = (4.0 - x0) / 3.0 | ||||
|         if y0 != 0: | ||||
|             y1 = ((1.0 - x0) * (3.0 - x0)) / (3.0 * y0)  # y0 != 0... | ||||
|         else: | ||||
|             y1 = 0 | ||||
|         x2 = x1 | ||||
|         y2 = 0 - y1 | ||||
|         # Compute rotationally-offset Bezier coordinates, using: | ||||
|         # x' = cos(angle) * x - sin(angle) * y | ||||
|         # y' = sin(angle) * x + cos(angle) * y | ||||
|         bezAng = start_angle + theta / 2.0 | ||||
|         cBezAng = cos(bezAng) | ||||
|         sBezAng = sin(bezAng) | ||||
|         rx0 = cBezAng * x0 - sBezAng * y0 | ||||
|         ry0 = sBezAng * x0 + cBezAng * y0 | ||||
|         rx1 = cBezAng * x1 - sBezAng * y1 | ||||
|         ry1 = sBezAng * x1 + cBezAng * y1 | ||||
|         rx2 = cBezAng * x2 - sBezAng * y2 | ||||
|         ry2 = sBezAng * x2 + cBezAng * y2 | ||||
|         rx3 = cBezAng * x3 - sBezAng * y3 | ||||
|         ry3 = sBezAng * x3 + cBezAng * y3 | ||||
|         # Compute scaled and translated Bezier coordinates. | ||||
|         rx, ry = w / 2.0, h / 2.0 | ||||
|         px0 = cx + rx * rx0 | ||||
|         py0 = cy + ry * ry0 | ||||
|         px1 = cx + rx * rx1 | ||||
|         py1 = cy + ry * ry1 | ||||
|         px2 = cx + rx * rx2 | ||||
|         py2 = cy + ry * ry2 | ||||
|         px3 = cx + rx * rx3 | ||||
|         py3 = cy + ry * ry3 | ||||
|         # Debug points... comment this out! | ||||
|         # stroke(0) | ||||
|         # ellipse(px3, py3, 15, 15) | ||||
|         # ellipse(px0, py0, 5, 5) | ||||
|     # Drawing | ||||
|     if mode == 0: # 'normal' arc (not 'middle' nor 'naked') | ||||
|         beginShape() | ||||
|     if mode != 1: # if not 'middle' | ||||
|         vertex(px3, py3) | ||||
|     if abs(theta) <= QUARTER_PI: | ||||
|         bezierVertex(px2, py2, px1, py1, px0, py0) | ||||
|     else: | ||||
|         # to avoid distortion, break into 2 smaller arcs | ||||
|         b_arc(cx, cy, w, h, start_angle, end_angle - theta / 2.0, mode=1) | ||||
|         b_arc(cx, cy, w, h, start_angle + theta / 2.0, end_angle, mode=1) | ||||
|     if mode == 0: # end of a 'normal' arc | ||||
|         endShape() | ||||
| 
 | ||||
| def p_circle_arc(x, y, radius, start_ang, sweep_ang, mode=0, num_points=None): | ||||
|     """ | ||||
|     Alternative interface for b_arc | ||||
|     """ | ||||
|     p_arc(x, y, radius * 2, radius * 2, start_ang, start_ang + sweep_ang, | ||||
|           mode=mode, num_points=num_points) | ||||
|                                  | ||||
| def p_arc(cx, cy, w, h, start_angle, end_angle, mode=0, num_points=None): | ||||
|     """ | ||||
|     A poly approximation of an arc | ||||
|     using the same signature as the original Processing arc() | ||||
|     mode: 0 "normal" arc, using beginShape() and endShape() | ||||
|               2 "naked" like normal, but without beginShape() and endShape() | ||||
|                  for use inside a larger PShape | ||||
|     """ | ||||
|     if not num_points: | ||||
|         num_points = 36   | ||||
|     # start_angle = start_angle if start_angle < end_angle else start_angle - TWO_PI | ||||
|     sweep_angle = end_angle - start_angle   | ||||
|     if mode == 0: | ||||
|             beginShape() | ||||
|     if sweep_angle < 0: | ||||
|         start_angle, end_angle = end_angle, start_angle | ||||
|         sweep_angle = -sweep_angle  | ||||
|         angle = sweep_angle / int(num_points) | ||||
|         a = end_angle | ||||
|         while a >= start_angle: | ||||
|                 sx = cx + cos(a) * w / 2. | ||||
|                 sy = cy + sin(a) * h / 2. | ||||
|                 vertex(sx, sy) | ||||
|                 a -= angle    | ||||
|     elif sweep_angle > 0: | ||||
|         angle = sweep_angle / int(num_points) | ||||
|         a = start_angle | ||||
|         while a <= end_angle: | ||||
|                 sx = cx + cos(a) * w / 2. | ||||
|                 sy = cy + sin(a) * h / 2. | ||||
|                 vertex(sx, sy) | ||||
|                 a += angle | ||||
|     else: | ||||
|         sx = cx + cos(start_angle) * w / 2. | ||||
|         sy = cy + sin(start_angle) * h / 2. | ||||
|         vertex(sx, sy) | ||||
|     if mode == 0: | ||||
|         endShape() | ||||
|  | @ -0,0 +1,44 @@ | |||
| """ | ||||
| Alexandre B A Villares http://abav.lugaralgum.com - GPL v3  | ||||
| 
 | ||||
| A helper for the Processing gifAnimation library https://github.com/extrapixel/gif-animation/tree/3.0 | ||||
| Download from https://github.com/villares/processing-play/blob/master/export_GIF/unzip_and_move_to_libraries_GifAnimation.zip | ||||
| This helper was inspired by an example by Art Simon https://github.com/APCSPrinciples/AnimatedGIF/ | ||||
| 
 | ||||
| v2019_09_23 | ||||
| 
 | ||||
| # add at the start of your sketch: | ||||
|   add_library('gifAnimation') | ||||
|   from gif_animation_helper import gif_export | ||||
| # add at the end of draw(): | ||||
|   gif_export(GifMaker, "filename") | ||||
| """ | ||||
| 
 | ||||
| def gif_export(GifMaker,             # gets a reference to the library | ||||
|                filename="exported",  # .gif will be added | ||||
|                repeat=0,             # 0 makes it an "endless" animation | ||||
|                quality=100,          # quality range 0 - 255 | ||||
|                delay=170,            # this is quick | ||||
|                frames=0,             # 0 will stop on keyPressed or frameCount >= 100000 | ||||
|                finish=False):        # force stop | ||||
|     global gifExporter | ||||
|     try: | ||||
|         gifExporter | ||||
|     except NameError: | ||||
|         gifExporter = GifMaker(this, filename + ".gif") | ||||
|         gifExporter.setRepeat(repeat) | ||||
|         gifExporter.setQuality(quality) | ||||
|         gifExporter.setDelay(delay) | ||||
|         print("gif recording started") | ||||
| 
 | ||||
| 
 | ||||
|     gifExporter.addFrame() | ||||
| 
 | ||||
|     if (frames == 0 and keyPressed and key == "e" or | ||||
|         frames != 0 and frameCount >= frames): | ||||
|         finish = True | ||||
|                  | ||||
|     if finish: | ||||
|         gifExporter.finish() | ||||
|         print("gif saved, exit") | ||||
|         exit() | ||||
										
											Plik binarny nie jest wyświetlany.
										
									
								
							| Po Szerokość: | Wysokość: | Rozmiar: 3.5 MiB | 
|  | @ -0,0 +1,152 @@ | |||
| # add_library('gifAnimation') | ||||
| # from gif_animation_helper import gif_export | ||||
| 
 | ||||
| from arcs import * | ||||
| 
 | ||||
| many_arrows = [] | ||||
| num_arrows, num_transitions = 30, 3 | ||||
| i, t = 0, 0 | ||||
| 
 | ||||
| def setup(): | ||||
|     global player_arrow | ||||
|     size(400, 400, P3D) | ||||
|     colorMode(HSB) | ||||
|     strokeJoin(ROUND) | ||||
|     frameRate(30) | ||||
|     background(200) | ||||
|     for _ in range(num_transitions): | ||||
|         many_arrows.append(create_arrows()) | ||||
|     player_arrow = list(random_arrow()) | ||||
| 
 | ||||
| def create_arrows(): | ||||
|     arrows = [] | ||||
|     for _ in range(num_arrows): | ||||
|         arrows.append(random_arrow()) | ||||
|     return arrows | ||||
| 
 | ||||
| def draw(): | ||||
|     background(200) | ||||
|     translate(width / 2, height / 2) | ||||
|     rotateX(HALF_PI * .5) | ||||
|     rotateY(HALF_PI * .66) | ||||
|     global t, i | ||||
| 
 | ||||
|     if t <= width: | ||||
|         tt = map(t, 0, width, 0, 1) | ||||
|     else: | ||||
|         tt = 1 | ||||
| 
 | ||||
|     ini_arrows, fin_arrows = many_arrows[i], many_arrows[i - 1] | ||||
|     for a, b in zip(ini_arrows, fin_arrows): | ||||
|         rad, start, sweep, thick, h = lerp_arrow(b, a, tt) | ||||
|         fill(h, 255, 200, 200) | ||||
|         # noStroke() | ||||
|         stroke(0) | ||||
|         player_arrow[0] = int(dist(mouseX, mouseY, width / 2, height / 2)) | ||||
| 
 | ||||
|         if compare_arrows(a, player_arrow): | ||||
|             fill(255) | ||||
|         display_arrow(rad, start, sweep, thick) | ||||
| 
 | ||||
|     print t | ||||
|     if t <= width: | ||||
|         t = lerp(t, width + 1, .05) | ||||
|     elif t > width: | ||||
|         t += 2 | ||||
|     if t > 1.1 * width: | ||||
|         t = 0 | ||||
|         i = (i + 1) % num_transitions | ||||
|     #     if i == 0: | ||||
|     #         gif_export(GifMaker, finish=True) | ||||
|     # if frameCount % 2 == 0: | ||||
|     #     gif_export(GifMaker, filename="sketch") | ||||
| 
 | ||||
| def lerp_arrow(a, b, t): | ||||
|     c = [] | ||||
|     for c_a, c_b in zip(a, b): | ||||
|         c.append(lerp(c_a, c_b, t)) | ||||
|     return c | ||||
| 
 | ||||
| def display_arrow(rad, start, sweep, thick, rot=True): | ||||
|     if rot: | ||||
|         start += thick * radians(frameCount % 361) / 10. | ||||
|     pushMatrix() | ||||
|     translate(0, 0, rad * thick / 100) | ||||
|     arc_arrow(0, 0, rad, start, sweep, thick) | ||||
|     popMatrix() | ||||
| 
 | ||||
| def random_arrow(): | ||||
|     d = -1 if random(100) >= 50 else 1 | ||||
|     return [int(random(2, height / 11)) * 5, | ||||
|             0,  # start | ||||
|             int(6 * random(QUARTER_PI, PI) / 6),  # sweep | ||||
|             int(random(2, height / 100)) * 10 * d,  # thickness | ||||
|             random(255),  # hue | ||||
|             ] | ||||
| 
 | ||||
| def keyPressed(): | ||||
|     if key == ' ': | ||||
|         many_arrows[:] = [] | ||||
|         for _ in range(3): | ||||
|             many_arrows.append(create_arrows()) | ||||
|         player_arrow[:] = list(random_arrow()) | ||||
| 
 | ||||
|     if key == 's': | ||||
|         saveFrame('#####.png') | ||||
| 
 | ||||
| def arc_arrow(x, y, radius, start_ang, sweep_ang, | ||||
|               thickness=None, correction=1): | ||||
|     """ | ||||
|     Draws an arrow in a circular arc shape | ||||
|     """ | ||||
|     if thickness is None: | ||||
|         thickness = radius / 2 | ||||
|     if abs(radius) * .75 < abs(thickness / 2): | ||||
|         thickness = radius * 1.5 if thickness > 0 else -radius * 1.5 | ||||
|     if radius < 0: | ||||
|         thickness = -thickness | ||||
| 
 | ||||
|     beginShape() | ||||
|     b_circle_arc(x, y, radius + thickness / 2, | ||||
|                  start_ang, sweep_ang, mode=2) | ||||
|     mid_ending(x, y, radius, start_ang + sweep_ang, thickness, correction) | ||||
|     b_circle_arc(x, y, radius - thickness / 2, | ||||
|                  start_ang + sweep_ang, -sweep_ang, mode=2) | ||||
|     mid_ending(x, y, radius, start_ang, thickness, correction) | ||||
|     endShape(CLOSE) | ||||
| 
 | ||||
| def mid_ending(x, y, radius, angle, thickness, correction): | ||||
|     if radius == 0: | ||||
|         radius = 1 | ||||
|     half_thick = thickness / 2. | ||||
|     pa = point_on_arc(x, y, radius + half_thick, angle) | ||||
|     pb = point_on_arc(x, y, radius - half_thick, angle) | ||||
|     mp = mid_point(pa, pb) | ||||
|     tangent_ep = point_on_arc(mp[0], mp[1], half_thick, angle + HALF_PI) | ||||
|     offset = half_thick / radius | ||||
|     midline_ep = point_on_arc(x, y, radius, angle + offset) | ||||
|     if correction == 0:  # tangent | ||||
|         vertex(*tangent_ep) | ||||
|     elif correction == 2:  # on mid-line | ||||
|         vertex(*midline_ep) | ||||
|     else:  # half-way | ||||
|         vertex(*mid_point(tangent_ep, midline_ep)) | ||||
| 
 | ||||
| def mid_point(pa, pb): | ||||
|     if len(pa) == 3 and len(pb) == 3: | ||||
|         return ((pa[0] + pb[0]) / 2, | ||||
|                 (pa[1] + pb[1]) / 2, | ||||
|                 (pa[2] + pb[2]) / 2) | ||||
|     else: | ||||
|         return ((pa[0] + pb[0]) / 2, | ||||
|                 (pa[1] + pb[1]) / 2) | ||||
| 
 | ||||
| def point_on_arc(cx, cy, radius, angle): | ||||
|     x = cx + radius * cos(angle) | ||||
|     y = cy + radius * sin(angle) | ||||
|     return (x, y) | ||||
| 
 | ||||
| def compare_arrows(a, b, t=10): | ||||
|     rad, start, sweep, thick, _ = a | ||||
|     brad, bstart, bsweep, bthick, _ = b | ||||
|     return (abs(rad - brad) < t) | ||||
		Ładowanie…
	
		Reference in New Issue
	
	 villares
						villares