villares 2019-06-01 23:58:38 -03:00
rodzic 6de3088f64
commit 548414927a
3 zmienionych plików z 14 dodań i 176 usunięć

Wyświetl plik

@ -1,128 +0,0 @@
def poly(p_list, closed=True):
beginShape()
for p in p_list:
if len(p) == 2 or p[2] == 0:
vertex(p[0], p[1])
else:
vertex(*p)
if closed:
endShape(CLOSE)
else:
endShape()
def poly_filleted(p_list, r_list=None, open_poly=False):
"""
draws a 'filleted' polygon with variable radius
dependent on roundedCorner()
"""
if not r_list:
r_list = [0] * len(p_list)
if not open_poly:
with pushStyle():
noStroke()
beginShape()
for p0, p1 in zip(p_list, [p_list[-1]] + p_list[:-1]):
m = (PVector(p0[0], p0[1]) + PVector(p1[0], p1[1])) / 2
vertex(m[0], m[1])
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[0], p0[1]) + PVector(p1[0], p1[1])) / 2
m2 = (PVector(p2[0], p2[1]) + PVector(p1[0], p1[1])) / 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[0], p0[1]) + PVector(p1[0], p1[1])) / 2
m2 = (PVector(p2[0], p2[1]) + PVector(p1[0], p1[1])) / 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[0] - dx * factor), (pt[1] - dy * factor))
# Vector 1
dx1 = pc[0] - p1[0]
dy1 = pc[1] - p1[1]
# Vector 2
dx2 = pc[0] - p2[0]
dy2 = pc[1] - p2[1]
# 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[0] * 2 - p1Cross[0] - p2Cross[0]
dy = pc[1] * 2 - p1Cross[1] - p2Cross[1]
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[1] - circlePoint[1], p1Cross[0] - circlePoint[0])
endAngle = atan2(p2Cross[1] - circlePoint[1], p2Cross[0] - circlePoint[0])
# 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
with pushStyle():
noStroke()
beginShape()
vertex(p1[0], p1[1])
vertex(p1Cross[0], p1Cross[1])
vertex(p2Cross[0], p2Cross[1])
vertex(p2[0], p2[1])
endShape(CLOSE)
line(p1[0], p1[1], p1Cross[0], p1Cross[1])
line(p2[0], p2[1], p2Cross[0], p2Cross[1])
arc(circlePoint[0], circlePoint[1], 2 * max_r, 2 * max_r,
startAngle, startAngle + sweepAngle)

Plik binarny nie jest wyświetlany.

Przed

Szerokość:  |  Wysokość:  |  Rozmiar: 124 KiB

Po

Szerokość:  |  Wysokość:  |  Rozmiar: 389 KiB

Wyświetl plik

@ -5,7 +5,6 @@ from random import shuffle
from itertools import product, combinations, permutations, combinations_with_replacement
from gif_exporter import gif_export
add_library('GifAnimation')
from polys import *
space, border = 20, 20
position = 0 # initial position
@ -54,8 +53,7 @@ def draw():
popMatrix()
i += 1
if i < len(line_combos):
# gif_export(GifMaker, SKETCH_NAME)
# gif_export(GifMaker, SKETCH_NAME[:-1] + "b") # B option
gif_export(GifMaker, SKETCH_NAME)
position += H * W
else:
gif_export(GifMaker, finish=True)
@ -77,6 +75,19 @@ def keyPressed():
if key == "s":
saveFrame("####.png")
def poly(p_list, closed=True):
beginShape()
for p in p_list:
if len(p) == 2 or p[2] == 0:
vertex(p[0], p[1])
else:
vertex(*p)
if closed:
endShape(CLOSE)
else:
endShape()
def settings():
from os import path
global SKETCH_NAME
@ -89,48 +100,3 @@ def settings():
[{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())
)
def var_bar(p1x, p1y, p2x, p2y, r1, r2=None):
"""
Tangent/tangent shape on 2 circles of arbitrary radius
"""
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)