Skip to content

Commit

Permalink
remove Point2 class since it was a duplicate of PVector
Browse files Browse the repository at this point in the history
  • Loading branch information
osresearch committed Jan 9, 2016
1 parent 96f6cac commit 37e9ffa
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 89 deletions.
27 changes: 7 additions & 20 deletions processingDemo/Clipping.pde
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,18 @@
* https://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland_algorithm
*/

class Point2
{
float x;
float y;

Point2(float _x, float _y)
{
x = _x;
y = _y;
}
}


class Clipping
{
final Point2 min;
final Point2 max;
final PVector min;
final PVector max;

final static int INSIDE = 0;
final static int LEFT = 1;
final static int RIGHT = 2;
final static int BOTTOM = 4;
final static int TOP = 8;

Clipping(Point2 p0, Point2 p1)
Clipping(PVector p0, PVector p1)
{
float x0, y0, x1, y1;

Expand All @@ -51,11 +38,11 @@ class Clipping
y1 = p0.y;
}

min = new Point2(x0, y0);
max = new Point2(x1, y1);
min = new PVector(x0, y0);
max = new PVector(x1, y1);
}

int compute_code(Point2 p)
int compute_code(PVector p)
{
int code = INSIDE;
if (p.x < min.x)
Expand All @@ -79,7 +66,7 @@ class Clipping
// rectangular clipping region min/max.
// p0 and p1 will be modified to be in the region
// returns true if the line segment is visible at all
boolean clip(Point2 p0, Point2 p1)
boolean clip(PVector p0, PVector p1)
{
int code0 = compute_code(p0);
int code1 = compute_code(p1);
Expand Down
16 changes: 8 additions & 8 deletions processingDemo/swarm.pde
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,24 @@ class Particle

void bee_move(PVector t)
{
PVector d = minus(t, p);
PVector d = PVector.sub(t, p);

float dist = mag(d);
float dist = d.mag();
if (dist == 0)
dist = 1;

// adjust the accelerations up to the maximum
// add some random noise to ensure that they don't bunch
PVector dv = times(d, max_a / dist);
v = plus(limit(plus(v, dv), -max_v, max_v), random3(-1,-1));
PVector dv = PVector.mult(d, max_a / dist);
v = PVector.add(limit(PVector.add(v, dv), -max_v, max_v), random3(-1,-1));

p = plus(p, v);
p.add(v);
}

void wasp_move()
{
// wasp is not acceleration limited
v = limit(plus(v, random3(-5, 5)), -max_wasp_v, max_wasp_v);
v = limit(PVector.add(v, random3(-5, 5)), -max_wasp_v, max_wasp_v);

// nudge the wasp towards the center of the screen
if (p.x < width/2)
Expand All @@ -50,7 +50,7 @@ class Particle
else
v.z -= random(2);

p = plus(p, v);
p.add(v);

// bounce the wasp off the corner of the screen
if (p.x < 0 || p.x > width)
Expand All @@ -65,7 +65,7 @@ class Particle

void draw(boolean bright)
{
PVector p2 = minus(p,v);
PVector p2 = PVector.sub(p,v);
vector_line(bright, p, p2);
}
};
Expand Down
88 changes: 27 additions & 61 deletions processingDemo/vector.pde
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/** \file
* V.st vector board interface
* V.st vector board interface.
*
* This handles storing the vectors, clipping them to the display,
* mirroring the line segments on the real display and sending them
* to the serial port.
*/
import processing.serial.*;

Expand All @@ -18,8 +22,8 @@ void
vector_setup()
{
clip = new Clipping(
new Point2(0,0),
new Point2(width-1,height-1)
new PVector(0,0),
new PVector(width-1,height-1)
);
// finding the right port requires picking it from the list
// should look for one that matches "ttyACM*" or "tty.usbmodem*"
Expand All @@ -37,16 +41,6 @@ vector_setup()
}


boolean
vector_offscreen(
float x,
float y
)
{
return (x < 0 || x >= width || y < 0 || y >= height);
}


void
vector_line(
boolean bright,
Expand All @@ -56,46 +50,40 @@ vector_line(
float y1
)
{
vector_line(bright, new PVector(x0,y0), new PVector(x1,y1));
}


void
vector_line(
boolean bright,
PVector p0_in,
PVector p1_in
)
{
if (p0_in == null || p1_in == null)
return;

// can we detect resize?
clip.max.x = width-1;
clip.max.y = height-1;

Point2 p0 = new Point2(x0,y0);
Point2 p1 = new Point2(x1,y1);

stroke(bright ? 255 : 120);

// clipping might modify the point, so we must copy
PVector p0 = p0_in.copy();
PVector p1 = p1_in.copy();

if (!clip.clip(p0, p1))
return;

line(p0.x, p0.y, p1.x, p1.y);

// The clip above should ensure that this never happens
// but just in case, we will discard those points
if (vector_offscreen(p0.x,p0.y)
|| vector_offscreen(p1.x,p1.y))
{
return;
}

vector_point(1, p0.x, p0.y);
vector_point(bright ? 3 : 2, p1.x, p1.y);
}


void
vector_line(
boolean bright,
PVector p0,
PVector p1
)
{
if (p0 == null || p1 == null)
return;
vector_line(bright, p0.x, p0.y, p1.x, p1.y);
}



void
vector_point(
int bright,
Expand Down Expand Up @@ -141,28 +129,6 @@ void vector_send()
bytes[byte_count++] = 0;
}

/*
* 3D vector math operations
*/
PVector plus(PVector a, PVector b)
{
return new PVector(a.x + b.x, a.y + b.y, a.z + b.z);
}

PVector minus(PVector a, PVector b)
{
return new PVector(a.x - b.x, a.y - b.y, a.z - b.z);
}

PVector times(PVector a, float k)
{
return new PVector(k*a.x, k*a.y, k*a.z);
}

float mag(PVector a)
{
return sqrt(a.x*a.x + a.y*a.y + a.z*a.z);
}

PVector random3(float min, float max)
{
Expand All @@ -189,7 +155,7 @@ PVector limit(PVector x, float min, float max)

PVector unit(PVector x)
{
return times(x, 1.0 / mag(x));
return PVector.mult(x, 1.0 / x.mag());
}


0 comments on commit 37e9ffa

Please sign in to comment.