-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPatterner.pde
213 lines (167 loc) · 5.5 KB
/
Patterner.pde
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
///////////////////////////////////////////////////////////////////////////////////
// //
// //
// by Gerard Walsh 2015 //
// //
///////////////////////////////////////////////////////////////////////////////////
//********** Global Variables **********//
char state = 'A'; // A = Unpressed, B = Pressed - attempted checker pattern, buttons Q + E
boolean isAltPressed = false; // Checks if alt is pressed, for keybindings
PImage img;
float dotSize = 2; //default dot size = 2 px
float scale = 2; //default: 2x
int incr = 4; // distance (in px) between centers, default: 4 px
int e = 2; // constrain for top output radius
int Count, xCount, yCount; // for calculating, and displaying
int cCount = 0;
//For importing images
boolean fileSelected;
String path;
//********** Setup **********//
void setup() {
size(1200, 800, P3D); //start at image size
surface.setResizable(true);
img = loadImage("Mountain.jpg");
ellipseMode(CENTER);
noStroke();
}
//********** Draw **********//
void draw() {
background(255);
fill(0, 0, 225); //the circles are blue
if (fileSelected)
{
img = loadImage(path);
fileSelected = false;
}
if (img != null) {
surface.setSize(int(scale)*img.width, int(scale)*img.height);
for (int y = 0; y < img.height; y += incr) {
for (int x = 0; x < img.width; x += incr) {
float val = (red(img.get(x, y)) + green(img.get(x, y)) + blue(img.get(x, y)))/3;
float s = map(val, 0, 255, 1, e*scale); //'s' is the individual radii of each circle in the array, ranging from "1" to "e"
if (state == 'B') {
if (((x+y)/incr)%2==0) { //if checkered is on, and x + y is even
ellipse(x*scale, y*scale, s*dotSize, s*dotSize);
cCount += 1;
}
}
if (state == 'A') {
ellipse(x*scale, y*scale, s*dotSize, s*dotSize);
cCount += 1;
}
}
}
}
//********** Show Element Count **********//
xCount = ceil(float(img.width)/float(incr));
yCount = ceil(float(img.height)/float(incr));
textAlign(LEFT, TOP);
textSize(20);
fill(255, 200);
rect(0, 10, 2*img.width, 90);
fill(0);
text(cCount + " elements,\n" + xCount + " x Columns,\n" + yCount + " y Rows.", 10, 10);
noLoop();
}
//********** Key Bindings **********//
void keyPressed() {
cCount = 0;
loop();
switch(keyCode) {
case UP:
scale *= 1.0; //default 1.2
scale = constrain(scale, 0.5, 100000);
break;
case DOWN:
scale /= 1.0; //default 1.2
scale = constrain(scale, 0.5, 100000);
break;
case RIGHT:
incr+=1;
incr = constrain(incr, 1, 100000);
break;
case LEFT:
incr-=1;
incr = constrain(incr, 1, 100000);
break;
}
switch(key) {
case('l'): //"l" to load a new file!
//case('L'): //or "L" //only matters if holding shift actually
selectInput("Select a file to process:", "fileSelected");
break;
case('w'): //"w" increases radius of circles
dotSize+=0.3;
dotSize = constrain(dotSize, 0, 100000);
break;
case('s'): //"s" decreases radius of circles
dotSize-=0.3;
dotSize = constrain(dotSize, 0, 100000);
break;
case('d'): //"d" increases "e", max radius
e+=1;
e = constrain(e, 1, 100000);
break;
case('a'): //"a" decreases "e", max radius
e-=1;
e = constrain(e, 1, 100000);
break;
case('x'): //"x" to export dxf, "alt + x" to export svg
if (isAltPressed == false) export("dxf");
if (isAltPressed == true) export("svg");
delay(1500); //holds for 1.5 secs before closing
exit();
break;
case('e'):
state = 'A';
break;
case('q'):
state = 'B';
break;
}
if (keyCode == ALT && isAltPressed == false) isAltPressed = true;
}
void keyReleased() {
if (keyCode == ALT) isAltPressed = false;
}
//********** Functions **********//
void fileSelected(File selection)
{
if (selection != null)
{
path = selection.getAbsolutePath();
fileSelected = true;
println("User selected " + selection.getAbsolutePath());
}
}
void export(String kind) {
println("generating pattern ... ");
if (kind == "dxf") {
dxfHeader();
for (int y = 0; y < img.height; y += incr) {
for (int x = 0; x < img.width; x += incr) {
float val = (red(img.get(x, y))+green(img.get(x, y)) + blue(img.get(x, y)))/3;
float s = map(val, 0, 255, 1, e*scale);
dxfCircle(x*scale, y*scale, 1, 1, s*dotSize, "Layer 1");
}
}
dxfEnd();
println("exporting dxf ... ");
saveStrings("export2.dxf", expDxf);
}
if (kind == "svg") {
//svgHeader();
//loop this:
//svgEllipse(int cx, int cy, int rx, int ry)
//svgEnd();
println("exporting svg ... ");
//saveXML(expSVG, "export.svg");
}
println("... done exporting.");
}
void delay(int delay)
{
int time = millis();
while (millis() - time <= delay);
}