kopia lustrzana https://github.com/villares/sketch-a-day
main
rodzic
ce781ac67b
commit
0dc672c5c8
Plik binarny nie jest wyświetlany.
|
Po Szerokość: | Wysokość: | Rozmiar: 364 KiB |
|
|
@ -0,0 +1,91 @@
|
|||
from itertools import product
|
||||
|
||||
import py5
|
||||
from py5_tools import animated_gif
|
||||
from shapely import Polygon, MultiPolygon, LineString, MultiLineString
|
||||
|
||||
shapes = []
|
||||
grid = list(product(range(64, 512, 128 + 64), repeat=2))
|
||||
|
||||
|
||||
def setup():
|
||||
py5.size(512, 512)
|
||||
py5.no_smooth()
|
||||
animated_gif('out.gif', duration=1, frame_numbers=range(1, 21))
|
||||
setup_shapes()
|
||||
py5.no_loop()
|
||||
py5.stroke_join(py5.ROUND)
|
||||
|
||||
def setup_shapes(num_shapes=4, buffer=16):
|
||||
tri = Polygon()
|
||||
while not tri.area:
|
||||
shapes.clear()
|
||||
pts = py5.random_sample(grid, 3 * num_shapes)#, replace=False)
|
||||
for i in range(0, 3 * num_shapes, 3):
|
||||
tri = Polygon(pts[i:i+3])
|
||||
if tri.area == 0:
|
||||
break
|
||||
ang = i // 3 * py5.QUARTER_PI
|
||||
shapes.append((0, ang, tri.buffer(buffer)))
|
||||
else:
|
||||
continue
|
||||
|
||||
|
||||
def draw():
|
||||
py5.background(200)
|
||||
|
||||
py5.no_fill()
|
||||
for c, angle, shp in shapes:
|
||||
#py5.stroke(c)
|
||||
hatched_poly(
|
||||
shp,
|
||||
angle=angle,
|
||||
spacing=5
|
||||
)
|
||||
|
||||
def hatched_poly(poly, angle=0, holes=[], spacing=5):
|
||||
if not isinstance(poly, (Polygon, MultiPolygon)):
|
||||
poly = Polygon(poly, holes)
|
||||
py5.stroke_weight(3)
|
||||
py5.shape(py5.convert_shape(poly))
|
||||
py5.stroke_weight(1)
|
||||
hatch = generate_hatch(poly, angle=angle,spacing=spacing)
|
||||
py5.shape(py5.convert_shape(hatch))
|
||||
#hatch = generate_hatch(poly, angle=angle + py5.HALF_PI,spacing=spacing)
|
||||
#py5.shape(py5.convert_shape(hatch))
|
||||
|
||||
def draw_poly(poly, holes=[]):
|
||||
if not isinstance(poly, (Polygon, MultiPolygon)):
|
||||
poly = Polygon(poly, holes)
|
||||
py5.shape(py5.convert_cached_shape(poly))
|
||||
|
||||
def generate_hatch(poly, angle=0, spacing=5, holes=[]):
|
||||
#global p # for debugging
|
||||
#p = poly
|
||||
if not isinstance(poly, (Polygon, MultiPolygon)):
|
||||
poly = Polygon(poly, holes)
|
||||
if poly.area == 0:
|
||||
return MultiLineString()
|
||||
diagonal = py5.dist(*poly.bounds) # diagonal length
|
||||
#print(poly.bounds, diagonal, poly.area)
|
||||
num = int(diagonal / spacing)
|
||||
V = py5.Py5Vector
|
||||
centroid = V(poly.envelope.centroid.x, poly.envelope.centroid.y)
|
||||
a = V(-diagonal / 2, -diagonal / 2).rotate(angle) + centroid
|
||||
b = V(-diagonal / 2, +diagonal / 2).rotate(angle) + centroid
|
||||
c = V(+diagonal / 2, -diagonal / 2).rotate(angle) + centroid
|
||||
d = V(+diagonal / 2, +diagonal / 2).rotate(angle) + centroid
|
||||
# py5.stroke(255, 0, 0) # for debugging
|
||||
# with py5.begin_closed_shape():
|
||||
# py5.vertices((a, b, d, c))
|
||||
# py5.stroke(0)
|
||||
lines = [LineString((V.lerp(a, b, i / num), V.lerp(c, d, i / num)))
|
||||
for i in range(num + 1)]
|
||||
return MultiLineString(lines).intersection(poly)
|
||||
|
||||
def key_pressed():
|
||||
#py5.save_frame('###.png')
|
||||
setup_shapes()
|
||||
py5.redraw()
|
||||
|
||||
py5.run_sketch(block=False)
|
||||
|
|
@ -26,6 +26,16 @@ If you appreciate what I have been doing, you may also support my artistic work,
|
|||
|
||||
<!-- SKETCHES_START -->
|
||||
|
||||
### sketch_2025_08_24
|
||||
|
||||

|
||||
|
||||
[sketch_2025_08_24](https://github.com/villares/sketch-a-day/tree/main/2025/sketch_2025_08_24) [[py5](https://py5coding.org/)]
|
||||
|
||||
|
||||
|
||||
---
|
||||
|
||||
### sketch_2025_08_23
|
||||
|
||||

|
||||
|
|
|
|||
Ładowanie…
Reference in New Issue