kopia lustrzana https://github.com/villares/sketch-a-day
main
rodzic
7a1679eb25
commit
46094bfb1b
|
@ -0,0 +1,259 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
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 poly_arc(x, y, radius, start_ang, sweep_ang, num_points=2):
|
||||
angle = sweep_ang / int(num_points)
|
||||
a = start_ang
|
||||
with beginShape():
|
||||
while a <= start_ang + sweep_ang:
|
||||
sx = x + cos(a) * radius
|
||||
sy = y + sin(a) * radius
|
||||
vertex(sx, sy)
|
||||
a += angle
|
||||
|
||||
def arc_poly(x, y, d, _, start_ang, end_ang, num_points=5):
|
||||
sweep_ang = end_ang - start_ang
|
||||
angle = sweep_ang / int(num_points)
|
||||
a = start_ang
|
||||
with beginShape():
|
||||
while a <= end_ang:
|
||||
sx = x + cos(a) * d / 2
|
||||
sy = y + sin(a) * d / 2
|
||||
vertex(sx, sy)
|
||||
a += angle
|
||||
|
||||
def bar(x1, y1, x2, y2, thickness=None, shorter=0, ends=(1, 1)):
|
||||
"""
|
||||
O código para fazer as barras, dois pares (x, y),
|
||||
um parâmetro de encurtamento: shorter
|
||||
"""
|
||||
L = dist(x1, y1, x2, y2)
|
||||
if not thickness:
|
||||
thickness = 10
|
||||
with pushMatrix():
|
||||
translate(x1, y1)
|
||||
angle = atan2(x1 - x2, y2 - y1)
|
||||
rotate(angle)
|
||||
offset = shorter / 2
|
||||
line(thickness / 2, offset, thickness / 2, L - offset)
|
||||
line(-thickness / 2, offset, -thickness / 2, L - offset)
|
||||
if ends[0]:
|
||||
half_circle(0, offset, thickness / 2, UP)
|
||||
if ends[1]:
|
||||
half_circle(0, L - offset, thickness / 2, DOWN)
|
||||
|
||||
def var_bar(p1x, p1y, p2x, p2y, r1, r2=None):
|
||||
if r2 is None:
|
||||
r2 = r1
|
||||
d = dist(p1x, p1y, p2x, p2y)
|
||||
if d > 0:
|
||||
with pushMatrix():
|
||||
translate(p1x, p1y)
|
||||
angle = atan2(p1x - p2x, p2y - p1y)
|
||||
rotate(angle + HALF_PI)
|
||||
ri = r1 - r2
|
||||
beta = asin(ri / d) + HALF_PI
|
||||
x1 = cos(beta) * r1
|
||||
y1 = sin(beta) * r1
|
||||
x2 = cos(beta) * r2
|
||||
y2 = sin(beta) * r2
|
||||
# with pushStyle():
|
||||
# noStroke()
|
||||
# beginShape()
|
||||
# vertex(-x1, -y1)
|
||||
# vertex(d - x2, -y2)
|
||||
# vertex(d, 0)
|
||||
# vertex(d - x2, +y2, 0)
|
||||
# vertex(-x1, +y1, 0)
|
||||
# vertex(0, 0, 0)
|
||||
# endShape()
|
||||
line(-x1, -y1, d - x2, -y2)
|
||||
line(-x1, +y1, d - x2, +y2)
|
||||
arc(0, 0, r1 * 2, r1 * 2,
|
||||
-beta - PI, beta - PI)
|
||||
arc(d, 0, r2 * 2, r2 * 2,
|
||||
beta - PI, PI - beta)
|
||||
else:
|
||||
ellipse(p1x, p1y, r1 * 2, r1 * 2)
|
||||
ellipse(p2y, p2x, r2 * 2, r2 * 2)
|
||||
|
||||
|
||||
def poly_rounded(P, r0, r1=None, r2=None):
|
||||
""" based on code by Introscopia"""
|
||||
r1 = r0 if not r1 else r1
|
||||
r2 = r0 if not r2 else r2
|
||||
a = [0] * 3
|
||||
d, d1, d2 = 2 * r0, 2 * r1, 2 * r2
|
||||
|
||||
a[0] = atan2(P[1].y - P[0].y, P[1].x - P[0].x) - HALF_PI
|
||||
a[1] = atan2(P[2].y - P[1].y, P[2].x - P[1].x) - HALF_PI
|
||||
a[2] = atan2(P[0].y - P[2].y, P[0].x - P[2].x) - HALF_PI
|
||||
|
||||
start = a[2] if a[2] < a[0] else a[2] - TWO_PI
|
||||
arc(P[0].x, P[0].y, d, d, start, a[0])
|
||||
start = a[0] if a[0] < a[1] else a[0] - TWO_PI
|
||||
arc(P[1].x, P[1].y, d1, d1, start, a[1])
|
||||
start = a[1] if a[1] < a[2] else a[1] - TWO_PI
|
||||
arc(P[2].x, P[2].y, d2, d2, start, a[2])
|
||||
|
||||
p01 = PVector(P[0].x + r0 * cos(a[0]), P[0].y + r0 * sin(a[0]))
|
||||
p10 = PVector(P[1].x + r1 * cos(a[0]), P[1].y + r1 * sin(a[0]))
|
||||
p12 = PVector(P[1].x + r1 * cos(a[1]), P[1].y + r1 * sin(a[1]))
|
||||
p21 = PVector(P[2].x + r2 * cos(a[1]), P[2].y + r2 * sin(a[1]))
|
||||
p20 = PVector(P[2].x + r2 * cos(a[2]), P[2].y + r2 * sin(a[2]))
|
||||
p02 = PVector(P[0].x + r0 * cos(a[2]), P[0].y + r0 * sin(a[2]))
|
||||
|
||||
with pushStyle():
|
||||
noStroke()
|
||||
with beginClosedShape():
|
||||
vertex(P[0].x, P[0].y)
|
||||
vertex(p02.x, p02.y)
|
||||
vertex(p20.x, p20.y)
|
||||
vertex(P[2].x, P[2].y)
|
||||
vertex(p21.x, p21.y)
|
||||
vertex(p12.x, p12.y)
|
||||
vertex(P[1].x, P[1].y)
|
||||
vertex(p10.x, p10.y)
|
||||
vertex(p01.x, p01.y)
|
||||
|
||||
line(p01.x, p01.y, p10.x, p10.y)
|
||||
line(p12.x, p12.y, p21.x, p21.y)
|
||||
line(p20.x, p20.y, p02.x, p02.y)
|
||||
|
||||
|
||||
def poly_rounded2(p_list, r_list):
|
||||
for p0, p1, p2, r in zip(p_list,
|
||||
[p_list[-1]] + p_list[:-1],
|
||||
[p_list[-2]] + [p_list[-1]] + p_list[:-2],
|
||||
[r_list[-1]] + r_list[:-1]
|
||||
):
|
||||
m1 = (PVector(p0.x, p0.y) + PVector(p1.x, p1.y)) / 2
|
||||
m2 = (PVector(p2.x, p2.y) + PVector(p1.x, p1.y)) / 2
|
||||
# strokeWeight(1)
|
||||
# stroke(0)
|
||||
# line(p1.x, p1.y, m1.x, m1.y)
|
||||
# line(p1.x, p1.y, m2.x, m2.y)
|
||||
# stroke(255)
|
||||
# strokeWeight(3)
|
||||
noFill()
|
||||
roundedCorner(p1, m1, m2, r)
|
||||
fill(255, 51)
|
||||
beginShape()
|
||||
vertex(m1.x, m1.y)
|
||||
vertex(p1.x, p1.y)
|
||||
vertex(m2.x, m2.y)
|
||||
endShape(CLOSE)
|
||||
|
||||
def roundedCorner(pc, p1, p2, r):
|
||||
"""
|
||||
Based on Stackoverflow C# rounded corner post
|
||||
https://stackoverflow.com/questions/24771828/algorithm-for-creating-rounded-corners-in-a-polygon
|
||||
"""
|
||||
# Vector 1
|
||||
dx1 = pc.x - p1.x
|
||||
dy1 = pc.y - p1.y
|
||||
|
||||
# Vector 2
|
||||
dx2 = pc.x - p2.x
|
||||
dy2 = pc.y - p2.y
|
||||
|
||||
# Angle between vector 1 and vector 2 divided by 2
|
||||
angle = (atan2(dy1, dx1) - atan2(dy2, dx2)) / 2
|
||||
|
||||
# The length of segment between angular point and the
|
||||
# points of intersection with the circle of a given radius
|
||||
tng = abs(tan(angle))
|
||||
segment = r / tng if tng != 0 else r
|
||||
|
||||
# Check the segment
|
||||
length1 = GetLength(dx1, dy1)
|
||||
length2 = GetLength(dx2, dy2)
|
||||
|
||||
min_len = min(length1, length2)
|
||||
|
||||
if segment > min_len:
|
||||
segment = min_len
|
||||
max_r = min_len * abs(tan(angle))
|
||||
else:
|
||||
max_r = r
|
||||
|
||||
# Points of intersection are calculated by the proportion between
|
||||
# the coordinates of the vector, length of vector and the length of the
|
||||
# segment.
|
||||
p1Cross = GetProportionPoint(pc, segment, length1, dx1, dy1)
|
||||
p2Cross = GetProportionPoint(pc, segment, length2, dx2, dy2)
|
||||
|
||||
# Calculation of the coordinates of the circle
|
||||
# center by the addition of angular vectors.
|
||||
dx = pc.x * 2 - p1Cross.x - p2Cross.x
|
||||
dy = pc.y * 2 - p1Cross.y - p2Cross.y
|
||||
|
||||
L = GetLength(dx, dy)
|
||||
d = GetLength(segment, max_r)
|
||||
|
||||
circlePoint = GetProportionPoint(pc, d, L, dx, dy)
|
||||
|
||||
# StartAngle and EndAngle of arc
|
||||
startAngle = atan2(p1Cross.y - circlePoint.y, p1Cross.x - circlePoint.x)
|
||||
endAngle = atan2(p2Cross.y - circlePoint.y, p2Cross.x - circlePoint.x)
|
||||
|
||||
# Sweep angle
|
||||
sweepAngle = endAngle - startAngle
|
||||
|
||||
# Some additional checks
|
||||
if sweepAngle < 0:
|
||||
startAngle, endAngle = endAngle, startAngle
|
||||
sweepAngle = -sweepAngle
|
||||
|
||||
if sweepAngle > PI:
|
||||
startAngle, endAngle = endAngle, startAngle
|
||||
sweepAngle = TWO_PI - sweepAngle
|
||||
|
||||
# Draw result using graphics
|
||||
line(p1.x, p1.y, p1Cross.x, p1Cross.y)
|
||||
line(p2.x, p2.y, p2Cross.x, p2Cross.y)
|
||||
arc(circlePoint.x, circlePoint.y, 2 * max_r, 2 * max_r,
|
||||
startAngle, startAngle + sweepAngle)
|
||||
# fill(0, 0, 100)
|
||||
# text(str(int(r)) + " " + str(int(max_r)),
|
||||
# circlePoint.x, circlePoint.y)
|
||||
|
||||
def GetLength(dx, dy):
|
||||
return sqrt(dx * dx + dy * dy)
|
||||
|
||||
|
||||
def GetProportionPoint(pt, segment, L, dx, dy):
|
||||
# factor = segment / L if L != 0 else 0
|
||||
factor = float(segment) / L if L != 0 else segment
|
||||
return PVector(
|
||||
(pt.x - dx * factor),
|
||||
|
||||
(pt.y - dy * factor))
|
|
@ -0,0 +1,40 @@
|
|||
"""
|
||||
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/
|
||||
|
||||
# add at the start of your sketch:
|
||||
add_library('gifAnimation')
|
||||
from gif_exporter import gif_export
|
||||
# add at the end of draw():
|
||||
gif_export(GifMaker)
|
||||
"""
|
||||
|
||||
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=255, # quality range 0 - 255
|
||||
delay=1200, # 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)
|
||||
|
||||
gifExporter.addFrame()
|
||||
|
||||
if frames == 0:
|
||||
if keyPressed and key == "e":
|
||||
finish = True
|
||||
|
||||
if finish:
|
||||
gifExporter.finish()
|
||||
print("gif saved")
|
||||
exit()
|
Plik binarny nie jest wyświetlany.
Po Szerokość: | Wysokość: | Rozmiar: 4.1 MiB |
|
@ -0,0 +1,165 @@
|
|||
# Alexandre B A Villares - https://abav.lugaralgum.com/sketch-a-day
|
||||
SKETCH_NAME, OUTPUT = "sketch_190224a", ".gif"
|
||||
"""
|
||||
Fixed the transition thing
|
||||
"""
|
||||
from collections import namedtuple
|
||||
import random as rnd
|
||||
import copy as cp
|
||||
from gif_exporter import gif_export
|
||||
from arcs import var_bar, poly_rounded2
|
||||
add_library('peasycam')
|
||||
add_library('GifAnimation')
|
||||
|
||||
X_LIST, Y_LIST = [], [] # listas de posições para elementos
|
||||
desenho_atual, outro_desenho, desenho_inter, desenho_inicial = [], [], [], []
|
||||
|
||||
SPACING, MARGIN = 100, 0
|
||||
MAX_S = 2
|
||||
LEVELS = 5
|
||||
NUM_NODES = 20 # número de elementos do desenho / number of nodes
|
||||
Node = namedtuple('Node', 'x y radius_size points_to')
|
||||
radius_modifier = 2
|
||||
end_mode = False
|
||||
|
||||
def setup():
|
||||
smooth(16)
|
||||
size(600, 600, P3D)
|
||||
# hint(DISABLE_DEPTH_TEST)
|
||||
noFill()
|
||||
cam = PeasyCam(this, 660)
|
||||
X_LIST[:] = [x for x in range(MARGIN, 1 + width - MARGIN, SPACING)]
|
||||
Y_LIST[:] = [y for y in range(MARGIN, 1 + height - MARGIN, SPACING)]
|
||||
novo_desenho(desenho_atual)
|
||||
desenho_inicial[:] = cp.deepcopy(desenho_atual)
|
||||
|
||||
def draw():
|
||||
global radius_modifier, desenho_atual, outro_desenho, end_mode
|
||||
#ortho()
|
||||
translate(-width / 2, -height / 2) #, - 120) # + 150)
|
||||
#rotateX(HALF_PI / 4)
|
||||
background(0)
|
||||
fc = frameCount % 100 - 50
|
||||
if fc < 0:
|
||||
desenho = desenho_atual
|
||||
elif 0 <= fc < 49:
|
||||
make_inter_nodes(map(fc, 0, 50, 0, 1))
|
||||
desenho = desenho_inter
|
||||
elif fc == 49:
|
||||
desenho_atual, outro_desenho = outro_desenho, desenho_atual
|
||||
desenho = desenho_atual
|
||||
if end_mode: exit()
|
||||
if not mousePressed:
|
||||
make_nodes_point(outro_desenho)
|
||||
else:
|
||||
print("will reset")
|
||||
end_mode = True
|
||||
outro_desenho[:] = cp.deepcopy(desenho_inicial)
|
||||
|
||||
desenho_plot(desenho)
|
||||
# if frameCount % 4 == 0:
|
||||
# gif_export(GifMaker, filename=SKETCH_NAME, delay=200)
|
||||
|
||||
if keyCode == UP and radius_modifier < 30:
|
||||
radius_modifier += 1
|
||||
println(radius_modifier)
|
||||
if keyCode == DOWN and radius_modifier > 0:
|
||||
radius_modifier -= 1
|
||||
println(radius_modifier)
|
||||
|
||||
|
||||
def keyPressed():
|
||||
global save_frames, radius_modifier
|
||||
if key == 'g':
|
||||
gif_export(GifMaker, filename=SKETCH_NAME)
|
||||
if key == 'r':
|
||||
make_nodes_point(desenho_atual)
|
||||
if key == 'n':
|
||||
novo_desenho(desenho_atual)
|
||||
if key == ' ':
|
||||
background(200)
|
||||
|
||||
|
||||
def novo_desenho(desenho):
|
||||
"""
|
||||
esvazia a lista elementos (setas e linhas) do desenho anterior
|
||||
clears the list of nodes and creates a a new drawing appending desenho_atual,
|
||||
a list of nodes/drawing elements: specials, connecting lines and lonely nodes
|
||||
"""
|
||||
desenho[:] = []
|
||||
for X in X_LIST:
|
||||
for Y in Y_LIST:
|
||||
desenho.append(Node(X, Y, rnd.choice([2, 4, 6]), []))
|
||||
make_nodes_point(desenho)
|
||||
outro_desenho[:] = cp.deepcopy(desenho)
|
||||
make_nodes_point(outro_desenho)
|
||||
|
||||
|
||||
def new_node(base=None):
|
||||
if not base:
|
||||
return Node( # elemento/"nó" uma namedtuple com:
|
||||
rnd.choice(X_LIST), # x
|
||||
rnd.choice(Y_LIST), # y
|
||||
rnd.choice([2, 4, 6]), # radius_size
|
||||
[] # points_to... (lista com ref. a outro elem.))
|
||||
)
|
||||
else:
|
||||
n = new_node()
|
||||
while dist(n.x, n.y, base.x, base.y) > MAX_S * SPACING:
|
||||
n = new_node()
|
||||
return n
|
||||
|
||||
def make_nodes_point(desenho):
|
||||
# AREA = (x1y2 + x2y3 + x3y1 – x1y3 – x2y1 – x3y2)/2.
|
||||
# x₁ (y₂ - y₃) + x₂ (y₃ - y₁) + x₃ (y₁ - y₂) == 0
|
||||
for n0 in desenho: # para cada elemento do desenho
|
||||
n1, n2 = new_node(n0), new_node(n0)
|
||||
while (n1.x * (n2.y - n0.y) +
|
||||
n2.x * (n0.y - n1.y) +
|
||||
n0.x * (n1.y - n2.y) == 0):
|
||||
# if the points are colinear, choose new nodes
|
||||
n1, n2 = new_node(n0), new_node(n0)
|
||||
n0.points_to[:] = []
|
||||
n0.points_to.append(n1)
|
||||
n0.points_to.append(n2)
|
||||
|
||||
def desenho_plot(d):
|
||||
for node in d:
|
||||
p1, p2 = node.points_to # se estiver apontando para alguém
|
||||
with pushMatrix():
|
||||
for i in range(LEVELS):
|
||||
strokeWeight(2)
|
||||
translate(0, 0, 15)
|
||||
rs = [(node.radius_size + i) * radius_modifier,
|
||||
(p1.radius_size + i) * radius_modifier,
|
||||
(p2.radius_size + i) * radius_modifier, ]
|
||||
poly_rounded2([node, p1, p2], rs)
|
||||
|
||||
# def mouseWheel(E):
|
||||
# global radius_modifier
|
||||
# radius_modifier += int(E.getAmount())
|
||||
# print(radius_modifier)
|
||||
|
||||
def make_inter_nodes(amt):
|
||||
desenho_inter[:] = []
|
||||
for n1, n2 in zip(desenho_atual, outro_desenho):
|
||||
desenho_inter.append(Node( # elemento/"nó" uma namedtuple com:
|
||||
n1.x, # x
|
||||
n1.y, # y
|
||||
n1.radius_size, # radius_size
|
||||
# cp.deepcopy(n1.points_to)
|
||||
[Node(lerp(p1.x, p2.x, amt),
|
||||
lerp(p1.y, p2.y, amt),
|
||||
lerp(p1.radius_size, p2.radius_size, amt), [])
|
||||
for p1, p2 in zip(n1.points_to, n2.points_to)]
|
||||
))
|
||||
|
||||
# print text to add to the project's README.md
|
||||
def settings():
|
||||
println(
|
||||
"""
|
||||

|
||||
|
||||
[{0}](https://github.com/villares/sketch-a-day/tree/master/2019/{0}) [[Py.Processing](https://villares.github.io/como-instalar-o-processing-modo-python/index-EN)]
|
||||
""".format(SKETCH_NAME, OUTPUT)
|
||||
)
|
Ładowanie…
Reference in New Issue