Skip to content

Instantly share code, notes, and snippets.

@companje
Last active March 17, 2024 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save companje/9ec42d3b3e1f671a1201ee78bfb82f37 to your computer and use it in GitHub Desktop.
Save companje/9ec42d3b3e1f671a1201ee78bfb82f37 to your computer and use it in GitHub Desktop.
Gestures - patterns in lines - WIP
import java.util.ArrayList;
ArrayList<Line> lines = new ArrayList();
float downX, downY;
String title = "";
void setup() {
size(400, 400);
loadLines("vmove.txt");
}
void mousePressed() {
downX = mouseX;
downY = mouseY;
}
void mouseReleased() {
lines.add(new Line(downX, downY, mouseX, mouseY));
}
void draw() {
background(0);
stroke(255);
fill(255);
if (mousePressed) {
line(downX, downY, mouseX, mouseY);
}
PVector c = calculateCentroid(lines);
for (Line line : lines) {
line.draw();
}
for (Line l : lines) {
circle(l.x1, l.y1, 5);
}
fill(255, 0, 0);
ellipse(c.x, c.y, 10, 10);
PVector centroid = calculateCentroid(lines);
PVector avgDir = calculateAverageDirection(lines, centroid);
Line avgLine = new Line(centroid, centroid.copy().add(avgDir));
float sumDist = calculateCombinedDistance(lines);
println(avgLine.getLength(), sumDist/lines.size());
stroke(0,255,0);
avgLine.draw();
fill(255);
textSize(20);
text(title.replace(".txt",""), 20,30);
}
void keyPressed() {
if (key=='c') lines.clear();
if (key=='s') {
PrintWriter out = createWriter("hmove.txt");
for (Line l : lines) {
out.println(l.x1 + " " + l.y1 + " " + l.x2 + " " + l.y2);
}
out.close();
}
if (key=='1') loadLines("vmove.txt");
if (key=='2') loadLines("rotate.txt");
if (key=='3') loadLines("hmove.txt");
if (key=='4') loadLines("zoom-in.txt");
if (key=='5') loadLines("zoom-out.txt");
}
PVector calculateCentroid(ArrayList<Line> lines) {
float totalX = 0;
float totalY = 0;
for (Line line : lines) {
totalX += (line.x1 + line.x2) / 2;
totalY += (line.y1 + line.y2) / 2;
}
return new PVector(totalX / lines.size(), totalY / lines.size());
}
PVector calculateAverageDirection(ArrayList<Line> lines, PVector centroid) {
PVector sum = new PVector();
for (Line line : lines) {
PVector direction = PVector.sub(new PVector(line.x2, line.y2), new PVector(line.x1, line.y1));
sum.add(direction);
}
sum.div(lines.size());
return sum;
}
float calculateCombinedDistance(ArrayList<Line> lines) {
float d = 0;
for (Line line : lines) {
d += line.getLength();
}
return d;
}
class Line {
float x1, y1, x2, y2;
Line(float x1, float y1, float x2, float y2) {
set(x1,y1,x2,y2);
}
Line(PVector a, PVector b) {
set(a.x,a.y,b.x,b.y);
}
void set(float x1, float y1, float x2, float y2) {
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
void draw() {
line(x1, y1, x2, y2);
}
float getLength() {
return dist(x1,y1,x2,y2);
}
}
void loadLines(String filename) {
title = filename;
lines.clear();
String row[] = loadStrings(filename);
for (String s : row) {
float p[] = float(split(s, ' '));
lines.add(new Line(p[0], p[1], p[2], p[3]));
}
}
191.0 102.0 244.0 102.0
221.0 141.0 263.0 143.0
239.0 163.0 259.0 173.0
237.0 227.0 262.0 238.0
252.0 195.0 266.0 209.0
263.0 183.0 284.0 188.0
267.0 136.0 290.0 142.0
257.0 107.0 280.0 113.0
237.0 81.0 257.0 82.0
226.0 69.0 248.0 65.0
110.0 172.0 137.0 167.0
137.0 211.0 188.0 212.0
179.0 245.0 208.0 248.0
88.0 158.0 98.0 140.0
157.0 124.0 178.0 120.0
232.0 133.0 232.0 133.0
244.0 142.0 268.0 165.0
240.0 183.0 240.0 183.0
250.0 196.0 272.0 218.0
266.0 250.0 266.0 250.0
266.0 250.0 264.0 282.0
236.0 280.0 201.0 308.0
177.0 284.0 153.0 304.0
135.0 276.0 132.0 276.0
132.0 276.0 115.0 259.0
130.0 228.0 117.0 221.0
87.0 206.0 78.0 182.0
109.0 157.0 141.0 114.0
179.0 114.0 179.0 114.0
179.0 114.0 200.0 106.0
203.0 132.0 210.0 132.0
223.0 116.0 236.0 115.0
254.0 229.0 254.0 257.0
119.0 211.0 125.0 200.0
257.0 207.0 236.0 174.0
153.0 258.0 153.0 225.0
234.0 271.0 225.0 239.0
185.0 163.0 185.0 134.0
269.0 114.0 269.0 99.0
302.0 178.0 302.0 165.0
291.0 256.0 291.0 238.0
150.0 133.0 155.0 112.0
208.0 86.0 208.0 68.0
233.0 112.0 233.0 97.0
113.0 178.0 109.0 152.0
194.0 224.0 173.0 199.0
279.0 178.0 306.0 147.0
266.0 161.0 253.0 133.0
201.0 311.0 201.0 282.0
202.0 171.0 215.0 160.0
231.0 188.0 251.0 178.0
253.0 211.0 284.0 211.0
271.0 250.0 282.0 253.0
250.0 282.0 250.0 305.0
183.0 272.0 158.0 297.0
158.0 225.0 139.0 225.0
184.0 209.0 131.0 189.0
197.0 235.0 206.0 217.0
208.0 194.0 206.0 192.0
201.0 204.0 190.0 197.0
193.0 253.0 179.0 249.0
161.0 249.0 157.0 249.0
257.0 233.0 278.0 230.0
251.0 124.0 243.0 129.0
189.0 112.0 201.0 140.0
172.0 144.0 172.0 144.0
171.0 169.0 185.0 178.0
186.0 179.0 206.0 190.0
178.0 211.0 195.0 214.0
206.0 250.0 208.0 227.0
254.0 217.0 237.0 203.0
295.0 182.0 244.0 182.0
307.0 151.0 278.0 152.0
277.0 139.0 273.0 142.0
273.0 135.0 273.0 127.0
250.0 118.0 234.0 110.0
230.0 112.0 218.0 132.0
266.0 169.0 238.0 167.0
229.0 217.0 227.0 209.0
@companje
Copy link
Author

Screenshot 2024-03-17 at 15 55 28 Screenshot 2024-03-17 at 15 55 25 Screenshot 2024-03-17 at 15 55 22 Screenshot 2024-03-17 at 15 55 19 Screenshot 2024-03-17 at 15 55 15 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment