villares 2019-03-25 09:48:57 -03:00
rodzic b88d846e09
commit bec6a460b6
6 zmienionych plików z 521 dodań i 20 usunięć

Wyświetl plik

@ -74,27 +74,33 @@ 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:
ri = r1 - r2
if d > abs(ri):
rid = (r1 - r2) / d
if rid > 1:
rid = 1
if rid < -1:
rid = -1
beta = asin(rid) + HALF_PI
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()
#print((d, beta, ri, x1, y1, x2, y2))
with pushStyle():
noStroke()
beginShape()
vertex(-x1, -y1)
vertex(d - x2, -y2)
vertex(d, 0)
vertex(d - x2, +y2)
vertex(-x1, +y1)
vertex(0, 0)
endShape(CLOSE)
line(-x1, -y1, d - x2, -y2)
line(-x1, +y1, d - x2, +y2)
arc(0, 0, r1 * 2, r1 * 2,
@ -103,4 +109,4 @@ def var_bar(p1x, p1y, p2x, p2y, r1, r2=None):
beta - PI, PI - beta)
else:
ellipse(p1x, p1y, r1 * 2, r1 * 2)
ellipse(p2y, p2x, r2 * 2, r2 * 2)
ellipse(p2x, p2y, r2 * 2, r2 * 2)

Wyświetl plik

@ -0,0 +1,229 @@
# -*- 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
#line(p1x, p1y, p2x, p2y)
d = dist(p1x, p1y, p2x, p2y)
ri = r1 - r2
if d > abs(ri):
rid = (r1 - r2) / d
if rid > 1:
rid = 1
if rid < -1:
rid = -1
beta = asin(rid) + HALF_PI
with pushMatrix():
translate(p1x, p1y)
angle = atan2(p1x - p2x, p2y - p1y)
rotate(angle + HALF_PI)
x1 = cos(beta) * r1
y1 = sin(beta) * r1
x2 = cos(beta) * r2
y2 = sin(beta) * r2
#print((d, beta, ri, x1, y1, x2, y2))
with pushStyle():
noStroke()
beginShape()
vertex(-x1, -y1)
vertex(d - x2, -y2)
vertex(d, 0)
vertex(d - x2, +y2)
vertex(-x1, +y1)
vertex(0, 0)
endShape(CLOSE)
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(p2x, p2y, r2 * 2, r2 * 2)
def poly_rounded2(p_list, r_list, open_poly=False):
"""
draws a 'filleted' polygon with variable radius
dependent on roundedCorner()
"""
if not open_poly:
with pushStyle():
noStroke()
beginShape()
for p0, p1 in zip(p_list, [p_list[-1]] + p_list[:-1]):
m = (PVector(p0.x, p0.y) + PVector(p1.x, p1.y)) / 2
vertex(m.x, m.y)
endShape(CLOSE)
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
roundedCorner(p1, m1, m2, r)
else:
for p0, p1, p2, r in zip(p_list[:-1],
[p_list[-1]] + p_list[:-2],
[p_list[-2]] + [p_list[-1]] + p_list[:-3],
[r_list[-1]] + r_list[:-2]
):
m1 = (PVector(p0.x, p0.y) + PVector(p1.x, p1.y)) / 2
m2 = (PVector(p2.x, p2.y) + PVector(p1.x, p1.y)) / 2
roundedCorner(p1, m1, m2, r)
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
"""
def GetProportionPoint(pt, segment, L, dx, dy):
factor = float(segment) / L if L != 0 else segment
return PVector((pt.x - dx * factor), (pt.y - dy * factor))
# 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 = sqrt(dx1 * dx1 + dy1 * dy1)
length2 = sqrt(dx2 * dx2 + dy2 * 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
# 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 = sqrt(dx * dx + dy * dy)
d = sqrt(segment * segment + max_r * 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
# noStroke()
with pushStyle():
noStroke()
beginShape()
vertex(p1.x, p1.y)
vertex(p1Cross.x, p1Cross.y)
vertex(p2Cross.x, p2Cross.y)
vertex(p2.x, p2.y)
endShape(CLOSE)
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, OPEN)

Wyświetl plik

@ -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=200, # 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: 51 KiB

Wyświetl plik

@ -0,0 +1,227 @@
# Alexandre B A Villares - https://abav.lugaralgum.com/sketch-a-day
SKETCH_NAME, OUTPUT = "sketch_190325a", ".gif"
"""
A retake of sketch 58 180228 + work from sketch_190321 :)
"""
from collections import namedtuple
import random as rnd
import copy as cp
add_library('GifAnimation')
from gif_exporter import gif_export
SPACING, MARGIN = 120, 120
X_LIST, Y_LIST = [], [] # listas de posições para elementos
desenho_atual, outro_desenho, desenho_inter, desenho_inicial = [], [], [], []
# NUM_NODES = 32 # número de elementos do desenho / number of nodes
Node = namedtuple(
'Node', 'x y t_size s_weight is_special points_to')
save_frames = False
def setup():
smooth(16)
size(600, 600, P2D)
rectMode(CENTER)
noFill()
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)
println("'s' to save, and 'n' for a new drawing")
def keyPressed():
global save_frames
if key == 's':
saveFrame("####.png")
#save_frames = not save_frames
print "Saving " + repr(save_frames)
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(new_node(x, y))
make_nodes_point(desenho)
outro_desenho[:] = cp.deepcopy(desenho)
make_nodes_point(outro_desenho)
def new_node(x=None, y=None):
if x == None or y == None:
x = rnd.choice(X_LIST)
y = rnd.choice(Y_LIST)
return Node( # elemento/"nó" uma namedtuple com:
x, # x
y, # y
rnd.choice([10, 20, 30]), # t_size (tail/circle size)
rnd.choice([1, 2, 3]), # s_weight (espessura da linha)
rnd.choice([True, False]), # is_special? (se é wave ou 'linha')
[] # points_to... (lista com ref. a outro elem.))
)
def make_nodes_point(desenho):
for node in desenho: # para cada elemento do desenho
node.points_to[:] = []
random_node = rnd.choice(desenho) # sorteia o,utro elemento
while not ((node.x, node.y) != (random_node.x, random_node.y)
and dist(node.x, node.y,
random_node.x, random_node.y) <= SPACING * 2):
random_node = rnd.choice(desenho)
# 'aponta' para este elemento, acrescenta na sub_lista
node.points_to.append(random_node)
def draw():
global desenho_atual, outro_desenho
background(200)
fc = frameCount % 300 - 150
if fc < 0:
desenho = desenho_atual
elif 0 <= fc < 149:
if frameCount % 10 == 0:
make_inter_nodes(map(fc, 0, 150, 0, 1))
desenho = desenho_inter
elif fc == 149:
desenho_atual, outro_desenho = outro_desenho, desenho_atual
desenho = desenho_atual
if not mousePressed:
make_nodes_point(outro_desenho)
print("will reset")
else:
outro_desenho[:] = cp.deepcopy(desenho_inicial)
# draws white 'lines', non-specials, first.
for node in (n for n in desenho if not n.is_special):
for other in node.points_to: # se estiver apontando para alguém
strokeWeight(node.s_weight)
stroke(255)
line(node.x, node.y, other.x, other.y)
# desenha o círculo (repare que só em nós que 'apontam')
ellipse(node.x, node.y, node.t_size, node.t_size)
# then draws 'lonely nodes' in red (nodes that do not point anywhere)
for node in (n for n in desenho if not n.points_to):
strokeWeight(node.s_weight)
stroke(100) # grey stroke for lonely nodes
if node.is_special:
ellipse(node.x, node.y, node.t_size * 2, node.t_size * 2)
else:
ellipse(node.x, node.y, node.t_size, node.t_size)
# then draws black specials
for node in (n for n in desenho if n.is_special):
for other in node.points_to: # se estiver apontando para alguém
strokeWeight(node.s_weight)
stroke(0)
wave(node.x, node.y, other.x, other.y,
node.t_size, node.s_weight * 5)
ellipse(node.x, node.y, node.t_size / 2, node.t_size / 2)
ellipse(other.x, other.y, node.s_weight, node.s_weight)
# if frameCount % 4 == 0:
# gif_export(GifMaker)
def make_inter_nodes(amt):
desenho_inter[:] = []
for n1, n2 in zip(desenho_atual, outro_desenho):
if n1.points_to:
p1x, p1y = n1.points_to[0].x, n1.points_to[0].y
else:
p1x, p1y = n1.x, n1.y
if n2.points_to:
p2x, p2y = n2.points_to[0].x, n2.points_to[0].y
else:
p2x, p2y = n2.x, n2.y
desenho_inter.append(Node( # elemento/"nó" uma namedtuple com:
n1.x, # x
n1.y, # y
n1.t_size, # t_size (tail/circle size)
n1.s_weight, # s_weight (espessura da linha)
n1.is_special, # is_special? (se é barra ou 'linha')
# cp.deepcopy(n1.points_to)
[PVector(lerp(p1x, p2x, amt), lerp(p1y, p2y, amt))]
))
def wave(x1, y1, x2, y2, s=None, n=2):
from arcs import roundedCorner
"""
dois pares (x, y), largura, número de ondas
"""
L = dist(x1, y1, x2, y2)
if not s:
s = int(max(L / 5, 10))
with pushMatrix():
translate(x1, y1)
angle = atan2(x1 - x2, y2 - y1)
rotate(angle)
offset = 0
dy = L / (n + 2.)
line(0, 0, 0, dy / 2.)
point_L = []
point_L.append(PVector(0, 0))
for i in range(1, n + 1):
point_L.append(PVector(0, i * dy))
if i % 2:
point_L.append(PVector(-s, i * dy + dy / 2.))
point_L.append(PVector(0, i * dy + dy))
else:
point_L.append(PVector(s, i * dy + dy / 2.))
point_L.append(PVector(0, i * dy + dy))
point_L.append(PVector(0, L))
for p0, p1, p2 in zip(point_L[:-2],
point_L[1:-1],
point_L[2:]
):
roundedCorner(p1, (p0 + p1) / 2., (p2 + p1) / 2., 10)
line(0, L - dy / 2., 0, L)
stroke(0)
def zigzag(x1, y1, x2, y2, s=None, n=2):
"""
dois pares (x, y), largura, número de ondas
"""
L = dist(x1, y1, x2, y2)
if not s:
s = int(max(L / 5, 10))
with pushMatrix():
translate(x1, y1)
angle = atan2(x1 - x2, y2 - y1)
rotate(angle)
offset = 0
dy = L / (n + 2)
beginShape()
stroke(0)
vertex(0, 0)
for i in range(1, n + 1):
vertex(0, i * dy)
if i % 2:
stroke(255, 0, 0)
vertex(-s, i * dy + dy / 2.)
else:
stroke(0, 0, 255)
vertex(s, i * dy + dy / 2.)
stroke(0)
vertex(0, L - dy)
vertex(0, L)
endShape()
# print text to add to the project's README.md
def settings():
println(
"""
![{0}](2019/{0}/{0}{1})
[{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)
)

Wyświetl plik

@ -23,6 +23,11 @@ Get updates from my sort-of-weekly newsletter: [[sketch-mail](https://villares.o
## 2019
---
![sketch_190325a](2019/sketch_190325a/sketch_190325a.gif)
[sketch_190325a](https://github.com/villares/sketch-a-day/tree/master/2019/sketch_190325a) [[Py.Processing](https://villares.github.io/como-instalar-o-processing-modo-python/index-EN)]
---
@ -99,12 +104,6 @@ selecting pointing nodes... (also present on 190323)
---
![sketch_190321b](2019/sketch_190321b/sketch_190321b.gif)
[sketch_190321b](https://github.com/villares/sketch-a-day/tree/master/2019/sketch_190321b)
---
![sketch_190313a](2019/sketch_190313a/sketch_190313a.gif)
[sketch_190313a](https://github.com/villares/sketch-a-day/tree/master/2019/sketch_190313a) [[Py.Processing](https://villares.github.io/como-instalar-o-processing-modo-python/index-EN)]