/
sketch_190415a.pyde
113 lines (103 loc) · 2.97 KB
/
sketch_190415a.pyde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""
A minimal poly editor
"""
ORDER = 10
click = False
cell_size = 45.
pts = [(12.0, 10.0), (12.0, 0.0), (-2.0, -2.0),
(0.0, 12.0), (10.0, 12.0), (10.0, 10.0)]
holes = [(2.0, 2.0), (6.0, 2.0), (6.0, 6.0), (2.0, 7.0)]
def setup():
global xo, yo # offset de correção da borda na tela cheia
global cell_size, cell_half, grid_size
size(500, 500, P2D)
grid_size = ORDER * cell_size
cell_half = cell_size / 2
xo, yo = (width - grid_size) / 2, (height - grid_size) / 2
rectMode(CENTER)
strokeJoin(ROUND)
def draw():
background(230)
translate(xo, yo)
stroke(128)
for x in range(ORDER):
line(cell_half + x * cell_size, cell_half,
cell_half + x * cell_size, cell_size * ORDER - cell_half)
for y in range(ORDER):
line(cell_half, cell_half + y * cell_size,
cell_size * ORDER - cell_half, cell_half + y * cell_size)
# grade de cellulas
for i in range(ORDER):
x = cell_half + i * cell_size
for j in range(ORDER):
y = cell_half + j * cell_size
cell(x, y)
poly_draw()
fill(0)
text(str(pts), 0, grid_size)
def cell(mx, my):
global click
noStroke()
if dist(mouseX - xo, mouseY - yo, mx, my) < cell_half:
fill(209, 100)
if click:
x = mx / cell_size * 2 - ORDER / 2
y = my / cell_size * 2 - ORDER / 2
if (x, y) in pts:
pts.remove((x, y))
else:
pts.append((x, y))
click = False
else:
noFill()
ellipse(mx, my, cell_size, cell_size)
def poly_draw():
# polígono
pushStyle()
strokeWeight(3) # espessura do polígono
noFill()
if len(pts) >= 3:
fill(255)
beginShape()
for x, y in pts[::-1]:
stroke(0)
sx = (x + ORDER / 2) * cell_half
sy = (y + ORDER / 2) * cell_half
vertex(sx, sy)
beginContour()
for x, y in holes[::-1]:
sx = (x + ORDER / 2) * cell_half
sy = (y + ORDER / 2) * cell_half
vertex(sx, sy)
endContour()
endShape(CLOSE)
elif len(pts) == 2:
stroke(128)
beginShape(LINES)
for x, y in pts:
sx = (x + ORDER / 2) * cell_half
sy = (y + ORDER / 2) * cell_half
vertex(sx, sy)
endShape()
popStyle()
def mouseReleased():
global click
if mouseButton == LEFT:
click = True
def keyPressed():
global pts
if key == " ":
pts = [] # empty pts
if key == "p":
println(pts)
def settings():
from os import path
global SKETCH_NAME
SKETCH_NAME = path.basename(sketchPath())
OUTPUT = ".gif"
println(
"""
![{0}](2019/{0}/{0}{1})
[{0}](https://github.com/villares/sketch-a-day/tree/master/{0}) [[Py.Processing](https://villares.github.io/como-instalar-o-processing-modo-python/index-EN)]
""".format(SKETCH_NAME, OUTPUT)
)