Skip to content

Commit 65b8656

Browse files
committed
misc PVector and example cleanup
1 parent 507f4f2 commit 65b8656

6 files changed

Lines changed: 10 additions & 21 deletions

File tree

java/examples/Topics/Fractals and L-Systems/Koch/KochFractal.pde

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class KochFractal {
77
ArrayList<KochLine> lines; // A list to keep track of all the lines
88
int count;
99

10-
public KochFractal() {
10+
KochFractal() {
1111
start = new PVector(0,height-20);
1212
end = new PVector(width,height-20);
1313
lines = new ArrayList<KochLine>();

java/examples/Topics/Fractals and L-Systems/Koch/KochLine.pde

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,12 @@ class KochLine {
4848
PVector p = a.get();
4949
p.add(v);
5050

51-
rotate(v,-radians(60));
51+
v.rotate(-radians(60));
5252
p.add(v);
5353

5454
return p;
5555
}
5656

57-
5857
// Easy, just 2/3 of the way
5958
PVector kochright() {
6059
PVector v = PVector.sub(a, b);
@@ -64,11 +63,5 @@ class KochLine {
6463
}
6564
}
6665

67-
public void rotate(PVector v, float theta) {
68-
float xTemp = v.x;
69-
// Might need to check for rounding errors like with angleBetween function?
70-
v.x = v.x*cos(theta) - v.y*sin(theta);
71-
v.y = xTemp*sin(theta) + v.y*cos(theta);
72-
}
7366

7467

java/examples/Topics/Simulate/Flocking/Boid.pde

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Boid {
1111

1212
Boid(float x, float y) {
1313
acceleration = new PVector(0,0);
14-
velocity = new PVector(random(-1,1),random(-1,1));
14+
velocity = PVector.random2D();
1515
location = new PVector(x,y);
1616
r = 2.0;
1717
maxspeed = 2;
@@ -60,9 +60,8 @@ class Boid {
6060
// STEER = DESIRED MINUS VELOCITY
6161
PVector seek(PVector target) {
6262
PVector desired = PVector.sub(target,location); // A vector pointing from the location to the target
63-
// Normalize desired and scale to maximum speed
64-
desired.normalize();
65-
desired.mult(maxspeed);
63+
// Scale to maximum speed
64+
desired.setMag(maxspeed);
6665
// Steering = Desired minus Velocity
6766
PVector steer = PVector.sub(desired,velocity);
6867
steer.limit(maxforce); // Limit to maximum steering force
@@ -120,8 +119,7 @@ class Boid {
120119
// As long as the vector is greater than 0
121120
if (steer.mag() > 0) {
122121
// Implement Reynolds: Steering = Desired - Velocity
123-
steer.normalize();
124-
steer.mult(maxspeed);
122+
steer.setMag(maxspeed);
125123
steer.sub(velocity);
126124
steer.limit(maxforce);
127125
}
@@ -143,8 +141,7 @@ class Boid {
143141
}
144142
if (count > 0) {
145143
sum.div((float)count);
146-
sum.normalize();
147-
sum.mult(maxspeed);
144+
sum.setMag(maxspeed);
148145
PVector steer = PVector.sub(sum,velocity);
149146
steer.limit(maxforce);
150147
return steer;

java/examples/Topics/Simulate/GravitationalAttraction3D/Sun.pde

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ class Sun {
1818
PVector attract(Planet m) {
1919
PVector force = PVector.sub(location,m.location); // Calculate direction of force
2020
float d = force.mag(); // Distance between objects
21-
d = constrain(d,5.0,25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects
22-
force.normalize(); // Normalize vector (distance doesn't matter here, we just want this vector for direction)
21+
d = constrain(d,5.0,25.0); // Limiting the distance to eliminate "extreme" results for very close or very far objects
2322
float strength = (G * mass * m.mass) / (d * d); // Calculate gravitional force magnitude
24-
force.mult(strength); // Get force vector --> magnitude * direction
23+
force.setMag(strength); // Get force vector --> magnitude * direction
2524
return force;
2625
}
2726

java/examples/Topics/Vectors/AccelerationWithVectors/AccelerationWithVectors.pde

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ Mover mover;
1515

1616
void setup() {
1717
size(640,360);
18-
smooth();
1918
mover = new Mover();
2019
}
2120

java/examples/Topics/Vectors/VectorMath/VectorMath.pde

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ void draw() {
3030
translate(width/2,height/2);
3131
// Draw the resulting vector
3232
stroke(255);
33+
strokeWeight(4);
3334
line(0,0,mouse.x,mouse.y);
3435

3536
}

0 commit comments

Comments
 (0)