Created
December 7, 2012 04:51
-
-
Save yangsu/4230845 to your computer and use it in GitHub Desktop.
Simple Processing Sketch to demo using handwriting input to create circles
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ArrayList<ArrayList<PVector>> shapes = new ArrayList<ArrayList<PVector>>(); | |
ArrayList<ArrayList<PVector>> paths = new ArrayList<ArrayList<PVector>>(); | |
ArrayList<PVector> current = new ArrayList<PVector>(); | |
ArrayList<Circle> circles = new ArrayList<Circle>(); | |
void setup() { | |
size(800, 800); | |
strokeWeight(2); | |
ellipseMode(RADIUS); | |
smooth(); | |
} | |
void drawShape(ArrayList<PVector> shape) { | |
beginShape(); | |
for (PVector p : shape) { | |
// ellipse(p.x, p.y, 5, 5); | |
curveVertex(p.x, p.y); | |
} | |
endShape(CLOSE); | |
} | |
void drawPath(ArrayList<PVector> path) { | |
pushStyle(); | |
noFill(); | |
beginShape(); | |
for (PVector p : path) { | |
// ellipse(p.x, p.y, 5, 5); | |
curveVertex(p.x, p.y); | |
} | |
endShape(); | |
popStyle(); | |
} | |
void draw() { | |
stroke(0); | |
background(150); | |
fill(255, 100); | |
stroke(255, 0 ,0); | |
drawPath(current); | |
for(ArrayList<PVector> p : shapes) | |
drawShape(p); | |
for(ArrayList<PVector> p : paths) | |
drawPath(p); | |
for (Circle c : circles) | |
c.show(); | |
} | |
void addpt(float x, float y) { | |
current.add(new PVector(x, y)); | |
} | |
void mousePressed() { | |
current = new ArrayList<PVector>(); | |
current.add(new PVector(mouseX, mouseY)); | |
} | |
void mouseDragged() { | |
addpt(mouseX, mouseY); | |
} | |
void mouseReleased() { | |
if (current.size() > 0 ) { | |
if (PVector.dist(current.get(0), current.get(current.size() - 1)) < 50) { | |
float x = 0, y = 0; | |
for (PVector p: current) { | |
x += p.x; | |
y += p.y; | |
} | |
x /= current.size(); | |
y /= current.size(); | |
PVector mid = new PVector(x, y, 0); | |
float r =0; | |
for (PVector p: current) { | |
r += PVector.dist(p, mid); | |
} | |
r /= current.size(); | |
circles.add(new Circle(x, y , r)); | |
shapes.add(current); | |
} | |
else { | |
paths.add(current); | |
} | |
} | |
} | |
class Circle { | |
float x, y , r; | |
Circle(float x, float y, float r) { | |
this.x = x; | |
this.y = y; | |
this.r = r; | |
} | |
void show() { | |
pushStyle(); | |
fill(255, 100); | |
stroke(0); | |
ellipse(x, y, r, r); | |
popStyle(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment