Pvector offset - anyone?
in
Programming Questions
•
2 years ago
Hello folks, i've been messing with PVector for now few days and gettinng almost the result i wanted but humm. not exactly... there is my class straightly from master D. Schiffman Path following sample. I'm trying to get an offset curve from vertex, i now miss the connection points for the offset. I might be dumb but cannot see how to get that.
- // Path Following
- // Daniel Shiffman <http://www.shiffman.net>
- // The Nature of Code, Spring 2009
- class Path {
- // A Path is an arraylist of points (PVector objects)
- ArrayList points;
- // A path has a radius, i.e how far is it ok for the boid to wander off
- float radius;
- Path() {
- // Arbitrary radius of 20
- radius = 20;
- points = new ArrayList();
- }
- // Add a point to the path
- void addPoint(float x, float y) {
- PVector point = new PVector(x,y);
- points.add(point);
- }
- // Draw the path
- void display() {
- // Draw the radius as thick lines and circles
- if (debug) {
- // Draw end points
- for (int i = 0; i < points.size(); i++) {
- PVector point = (PVector) points.get(i);
- //ellipse(point.x,point.y,radius,radius);
- //}
- // Draw Polygon around path
- for (int j = 0; j < points.size()-1; j++) {
- PVector start = (PVector) points.get(j);
- PVector end = (PVector) points.get(j+1);
- PVector line = PVector.sub(end,start);
- PVector normal = new PVector((line.y),-(line.x));
- normal.normalize();
- normal.mult(radius);
- PVector a = PVector.add(start, normal);
- PVector b = PVector.add(end, normal);
- if(i == points.size()-1) {
- start = (PVector) points.get(i);
- end = (PVector) points.get(0);
- PVector Bline = PVector.sub(end,start);
- PVector Bnormal = new PVector((Bline.y),-(Bline.x));
- Bnormal.normalize();
- Bnormal.mult(radius);
- a = PVector.add(start, Bnormal);
- b = PVector.add(end, Bnormal);
- }
- stroke(0);
- beginShape();
- vertex(a.x,a.y);
- vertex(b.x,b.y);
- endShape();
- }
- }
- }
- // Draw Regular Line
- stroke(0);
- noFill();
- beginShape();
- for (int i = 0; i < points.size(); i++) {
- PVector loc = (PVector) points.get(i);
- // vertex(loc.x,loc.y);
- }
- endShape();
- }
- }
1