sketch-a-day/2019/sketch_190418a/sketch_190418a.pyde

144 wiersze
4.4 KiB
Plaintext
Czysty Zwykły widok Historia

2019-04-19 00:23:34 +00:00
# Alexandre B A Villares - https://abav.lugaralgum.com/sketch-a-day
2019-04-18 12:48:08 +00:00
"""
A minimal poly editor
2019-04-19 00:23:34 +00:00
- big refactor
- Drag poly and poly-hole vertices, add and remove vertices.
2019-04-18 12:48:08 +00:00
"""
cell_size = 25
2019-04-19 00:23:34 +00:00
outer_pts = [(-8, 8), (8, 8), (8, -8), (-8, -8)]
2019-04-18 14:08:41 +00:00
inner_pts = [(2.0, 2.0), (6.0, 2.0), (6.0, 6.0), (2.0, 7.0)]
2019-04-19 00:23:34 +00:00
outer_drag, inner_drag = -1, -1
2019-04-18 12:48:08 +00:00
def setup():
2019-04-18 14:08:41 +00:00
global cell_size, cell_size, order, grid_size
2019-04-19 00:23:34 +00:00
global x_offset, y_offset
2019-04-18 12:48:08 +00:00
size(500, 500, P2D)
order = width / cell_size
2019-04-19 00:23:34 +00:00
x_offset = y_offset = int(order / 2)
2019-04-18 12:48:08 +00:00
strokeJoin(ROUND)
def draw():
background(230)
2019-04-18 14:08:41 +00:00
# grade de cellulas
2019-04-18 12:48:08 +00:00
stroke(128)
2019-04-18 14:08:41 +00:00
noFill()
2019-04-18 12:48:08 +00:00
for x in range(order):
2019-04-18 14:08:41 +00:00
for y in range(order):
rect(x * cell_size, y * cell_size,
cell_size, cell_size)
2019-04-18 12:48:08 +00:00
poly_draw()
fill(0)
2019-04-19 00:23:34 +00:00
text(str(outer_pts), 0, order * cell_size - 10)
text(str(inner_pts), 0, order * cell_size)
2019-04-18 12:48:08 +00:00
def poly_draw():
# polígono
pushStyle()
strokeWeight(3) # espessura do polígono
noFill()
2019-04-18 14:08:41 +00:00
if len(outer_pts) >= 3:
2019-04-18 12:48:08 +00:00
fill(255)
beginShape()
2019-04-18 14:08:41 +00:00
for x, y in outer_pts[::-1]:
2019-04-18 12:48:08 +00:00
stroke(0)
2019-04-19 00:23:34 +00:00
sx = (x + x_offset) * cell_size
sy = (y + y_offset) * cell_size
2019-04-18 14:08:41 +00:00
vertex(sx, sy)
2019-04-18 12:48:08 +00:00
beginContour()
2019-04-18 14:08:41 +00:00
for x, y in inner_pts[::-1]:
2019-04-19 00:23:34 +00:00
sx = (x + x_offset) * cell_size
sy = (y + y_offset) * cell_size
2019-04-18 14:08:41 +00:00
vertex(sx, sy)
endContour()
2019-04-18 12:48:08 +00:00
endShape(CLOSE)
2019-04-18 14:08:41 +00:00
elif len(outer_pts) == 2:
2019-04-18 12:48:08 +00:00
stroke(128)
beginShape(LINES)
2019-04-18 14:08:41 +00:00
for x, y in outer_pts:
2019-04-19 00:23:34 +00:00
vertex((x + x_offset) * cell_size,
(y + y_offset) * cell_size)
2019-04-18 12:48:08 +00:00
endShape()
2019-04-18 14:08:41 +00:00
for x, y in inner_pts:
fill(0)
2019-04-19 00:23:34 +00:00
ellipse((x + x_offset) * cell_size,
(y + y_offset) * cell_size, 5, 5)
2019-04-18 14:08:41 +00:00
else:
for x, y in outer_pts:
fill(255)
2019-04-19 00:23:34 +00:00
ellipse((x + x_offset) * cell_size,
(y + y_offset) * cell_size, 5, 5)
2019-04-18 14:08:41 +00:00
for x, y in inner_pts:
fill(0)
2019-04-19 00:23:34 +00:00
ellipse((x + x_offset) * cell_size,
(y + y_offset) * cell_size, 5, 5)
2019-04-18 12:48:08 +00:00
popStyle()
def keyPressed():
2019-04-18 14:08:41 +00:00
global outer_pts
2019-04-18 12:48:08 +00:00
if key == " ":
2019-04-18 14:08:41 +00:00
outer_pts = [] # empty outer_pts
2019-04-18 12:48:08 +00:00
if key == "p":
2019-04-18 14:08:41 +00:00
println(outer_pts)
2019-04-18 12:48:08 +00:00
def mousePressed():
2019-04-18 14:08:41 +00:00
global outer_drag, inner_drag
if keyPressed and keyCode == SHIFT:
for i in range(order):
x = i * cell_size
for j in range(order):
y = j * cell_size
2019-04-19 00:23:34 +00:00
io, jo = i - x_offset, j - y_offset # grid origin correction
2019-04-18 14:08:41 +00:00
if dist(mouseX, mouseY, x, y) < 10 and mouseButton == LEFT:
2019-04-19 00:23:34 +00:00
if (io, jo) in outer_pts:
outer_pts.remove((io, jo))
2019-04-18 14:08:41 +00:00
else:
2019-04-19 00:23:34 +00:00
outer_pts.append((io, jo))
2019-04-18 14:08:41 +00:00
if dist(mouseX, mouseY, x, y) < 10 and mouseButton == RIGHT:
2019-04-19 00:23:34 +00:00
if (io, jo) in inner_pts:
inner_pts.remove((io, jo))
2019-04-18 14:08:41 +00:00
else:
2019-04-19 00:23:34 +00:00
inner_pts.append((io, jo))
2019-04-18 14:08:41 +00:00
else:
for i_op, op in enumerate(outer_pts):
2019-04-19 00:23:34 +00:00
ox = (op[0] + x_offset) * cell_size
oy = (op[1] + y_offset) * cell_size
2019-04-18 14:08:41 +00:00
if dist(mouseX, mouseY, ox, oy) < cell_size / 2:
outer_drag = i_op
return
for i_ip, ip in enumerate(inner_pts):
2019-04-19 00:23:34 +00:00
ix = (ip[0] + x_offset) * cell_size
iy = (ip[1] + y_offset) * cell_size
2019-04-18 14:08:41 +00:00
if dist(mouseX, mouseY, ix, iy) < cell_size / 2:
inner_drag = i_ip
return
2019-04-18 12:48:08 +00:00
def mouseDragged():
2019-04-18 14:08:41 +00:00
if outer_drag >= 0:
2019-04-19 00:23:34 +00:00
outer_pts[outer_drag] = (int(mouseX / cell_size) - x_offset,
int(mouseY / cell_size) - y_offset)
2019-04-18 14:08:41 +00:00
if inner_drag >= 0:
2019-04-19 00:23:34 +00:00
inner_pts[inner_drag] = (int(mouseX / cell_size) - x_offset,
int(mouseY / cell_size) - y_offset)
2019-04-18 12:48:08 +00:00
def mouseReleased():
2019-04-18 14:08:41 +00:00
global outer_drag, inner_drag
outer_drag = -1
inner_drag = -1
2019-04-18 12:48:08 +00:00
def settings():
from os import path
global SKETCH_NAME
SKETCH_NAME = path.basename(sketchPath())
2019-04-19 00:23:34 +00:00
OUTPUT = ".png"
2019-04-18 12:48:08 +00:00
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)
)