-
-
Save jkwok91/c718053a3e0a59bd6aa5 to your computer and use it in GitHub Desktop.
nothing fancy, just testing out my rotate() / translate() chops; working on something larger
This file contains 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
class Circle { | |
int r; | |
PVector center, location; | |
Circle(float x, float y, int rad) { | |
center = new PVector(x, y); | |
location = new PVector(); | |
move(); | |
r = rad; | |
} | |
void move() { | |
location.x = 2*r*cos(theta); | |
location.y = 2*r*sin(theta); | |
} | |
void display() { | |
pushMatrix(); | |
translate(center.x, center.y); | |
stroke(color(255, 255, 0)); | |
ellipse(location.x, location.y, r*2, r*2); | |
popMatrix(); | |
} | |
} |
This file contains 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
/* | |
two circles rotating around each other | |
*/ | |
float theta, d; | |
void setup() { | |
size(300, 300); | |
background(0); | |
stroke(255); | |
noFill(); | |
theta = 0; | |
d = 80; | |
} | |
void draw() { | |
background(0); | |
translate(width/2, height/2); | |
float ex = (d/2)*cos(theta); | |
float ey = (d/2)*sin(theta); | |
makeCircle(ex, ey, d); | |
makeCircle(-ex, -ey, d); | |
theta += 0.05; | |
} | |
void makeCircle(float centerx, float centery, float diameter) { | |
ellipse(centerx, centery, diameter, diameter); | |
float radius = diameter/2; | |
pushMatrix(); | |
translate(centerx, centery); | |
rotate(theta*1.5); | |
rect(-radius/sqrt(2), -radius/sqrt(2), diameter/sqrt(2), diameter/sqrt(2)); | |
popMatrix(); | |
} |
This file contains 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
/* | |
circles congregating in a larger circle | |
learning to implement separation[, alignment, and cohesion] | |
april 20, 2014 | |
*/ | |
int randNum; | |
ArrayList<Dot> cluster; | |
float maxSep, theta; | |
Circle c1; | |
void setup() { | |
size(500, 300); | |
stroke(255); | |
noFill(); | |
c1 = new Circle(width/2, height/2, 50); | |
maxSep = 50; | |
randNum = (int)random(width); | |
cluster = new ArrayList<Dot>(); | |
for (int i = 0; i < randNum; i++) { | |
cluster.add(new Dot(random(width), random(height), c1.r)); | |
} | |
} | |
void draw() { | |
background(0); | |
c1.move(); | |
c1.display(); | |
for (Dot d : cluster) { | |
PVector target = PVector.add(c1.location, c1.center); | |
d.applyBehaviors(target, cluster); | |
d.update(); | |
d.display(); | |
} | |
theta = (theta + 0.05)%TWO_PI; | |
} | |
void keyPressed() { | |
if (keyCode == UP) { | |
maxSep += 0.5; | |
} | |
else if (keyCode == DOWN) { | |
maxSep += 0.5; | |
} | |
} |
This file contains 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
class Dot { | |
float radius; | |
PVector location, velocity, acceleration; | |
float maxforce, maxspeed, r; | |
Dot(float x, float y, int rad) { | |
radius = random(1, 5); | |
location = new PVector(x, y); | |
velocity = new PVector(random(-0.5, 0.5), random(-1, 1)); | |
acceleration = new PVector(0, 0); | |
maxforce = 0.1; | |
maxspeed = 5; | |
r = rad; | |
} | |
void update() { | |
velocity.add(acceleration); | |
velocity.limit(maxspeed); | |
location.add(velocity); | |
acceleration.mult(0); //reset acc | |
} | |
void applyForce(PVector force) { | |
acceleration.add(force); | |
} | |
void applyBehaviors(PVector p, ArrayList<Dot> ds) { | |
PVector seekF = seek(p); | |
PVector separateF = separate(ds); | |
applyForce(seekF); | |
applyForce(separateF); | |
} | |
PVector separate(ArrayList<Dot> ds) { | |
float desiredSeparation = maxSep; | |
PVector steer = new PVector(0,0); | |
int count = 0; | |
for (Dot d : ds) { | |
float dist = PVector.dist(location, d.location); | |
if ((dist > 0) && (dist < desiredSeparation)) { | |
PVector diff = PVector.sub(location, d.location); | |
diff.normalize(); | |
diff.div(dist); | |
steer.add(diff); | |
count++; | |
} | |
} | |
if (count > 0) { | |
steer.div(count); | |
steer.normalize(); | |
steer.mult(maxspeed); | |
steer.sub(velocity); | |
steer.limit(maxforce); | |
} | |
return steer; | |
} | |
PVector seek(PVector target) { | |
PVector desired = PVector.sub(target, location); | |
desired.normalize(); | |
//float currentspeed = map(desired.mag(), 0, r, 0, maxspeed); | |
//desired.mult(currentspeed); | |
desired.mult(maxspeed); | |
//formula | |
PVector steer = PVector.sub(desired, velocity); | |
steer.limit(maxforce); | |
return steer; | |
} | |
void display() { | |
stroke(255); | |
fill(0); | |
ellipse(location.x, location.y, radius*2, radius*2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment