misc PVector and example cleanup

This commit is contained in:
Daniel Shiffman
2013-03-18 22:12:27 -04:00
parent 0c35ad10a5
commit 9a466e1866
6 changed files with 10 additions and 21 deletions
@@ -7,7 +7,7 @@ class KochFractal {
ArrayList<KochLine> lines; // A list to keep track of all the lines
int count;
public KochFractal() {
KochFractal() {
start = new PVector(0,height-20);
end = new PVector(width,height-20);
lines = new ArrayList<KochLine>();
@@ -48,13 +48,12 @@ class KochLine {
PVector p = a.get();
p.add(v);
rotate(v,-radians(60));
v.rotate(-radians(60));
p.add(v);
return p;
}
// Easy, just 2/3 of the way
PVector kochright() {
PVector v = PVector.sub(a, b);
@@ -64,11 +63,5 @@ class KochLine {
}
}
public void rotate(PVector v, float theta) {
float xTemp = v.x;
// Might need to check for rounding errors like with angleBetween function?
v.x = v.x*cos(theta) - v.y*sin(theta);
v.y = xTemp*sin(theta) + v.y*cos(theta);
}
@@ -11,7 +11,7 @@ class Boid {
Boid(float x, float y) {
acceleration = new PVector(0,0);
velocity = new PVector(random(-1,1),random(-1,1));
velocity = PVector.random2D();
location = new PVector(x,y);
r = 2.0;
maxspeed = 2;
@@ -60,9 +60,8 @@ class Boid {
// STEER = DESIRED MINUS VELOCITY
PVector seek(PVector target) {
PVector desired = PVector.sub(target,location); // A vector pointing from the location to the target
// Normalize desired and scale to maximum speed
desired.normalize();
desired.mult(maxspeed);
// Scale to maximum speed
desired.setMag(maxspeed);
// Steering = Desired minus Velocity
PVector steer = PVector.sub(desired,velocity);
steer.limit(maxforce); // Limit to maximum steering force
@@ -120,8 +119,7 @@ class Boid {
// As long as the vector is greater than 0
if (steer.mag() > 0) {
// Implement Reynolds: Steering = Desired - Velocity
steer.normalize();
steer.mult(maxspeed);
steer.setMag(maxspeed);
steer.sub(velocity);
steer.limit(maxforce);
}
@@ -143,8 +141,7 @@ class Boid {
}
if (count > 0) {
sum.div((float)count);
sum.normalize();
sum.mult(maxspeed);
sum.setMag(maxspeed);
PVector steer = PVector.sub(sum,velocity);
steer.limit(maxforce);
return steer;
@@ -18,10 +18,9 @@ class Sun {
PVector attract(Planet m) {
PVector force = PVector.sub(location,m.location); // Calculate direction of force
float d = force.mag(); // Distance between objects
d = constrain(d,5.0,25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects
force.normalize(); // Normalize vector (distance doesn't matter here, we just want this vector for direction)
d = constrain(d,5.0,25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects
float strength = (G * mass * m.mass) / (d * d); // Calculate gravitional force magnitude
force.mult(strength); // Get force vector --> magnitude * direction
force.setMag(strength); // Get force vector --> magnitude * direction
return force;
}
@@ -15,7 +15,6 @@ Mover mover;
void setup() {
size(640,360);
smooth();
mover = new Mover();
}
@@ -30,6 +30,7 @@ void draw() {
translate(width/2,height/2);
// Draw the resulting vector
stroke(255);
strokeWeight(4);
line(0,0,mouse.x,mouse.y);
}