Non-linear connections between two points in 3D space
in
Contributed Library Questions
•
6 months ago
Hi,
I'm having a hard time figuring this one out - I've the sample code as seen below, in order to draw a connection between two points. Thing is, I want the connection not to be a single line, but a random number of lines. Also, I want each line to have a random number of kinks (vertices) between the start point and the end point. Then, each kink (not including the start point and the end point), should have a slight random offset from what would be a straight line, resulting in a kind of 'blurry' connection, when applied to a bunch of start points and end points.
Now, I got it to work with a straight up example, connecting the left side of a box with the right side. Next step, which is where I'm stuck, is to get it to work in a larger network of points.
How do I get the interpolations/kinks to offset perpendicular to the connection?
Any help is appreciated,
Thanks in advance,
/Claus
- import peasy.*;
- PeasyCam cam;
- float maxOff = 25.0; // maximum offset (deviation from the straight line)
- ArrayList connections;
- void setup() {
- size(1200, 600, P3D);
- cam = new PeasyCam(this, 500);
- //cam.setYawRotationMode(); // maybe comment this out?
- cam.setMinimumDistance(-1000);
- cam.setMaximumDistance(1000);
- generate();
- }
- void draw() {
- background(0);
- for (int i = connections.size()-1; i >= 0; i--) {
- Connect c = (Connect) connections.get(i);
- c.render();
- }
- noFill();
- strokeWeight(.5);
- stroke(255);
- box(1000);
- }
- void generate() {
- connections = new ArrayList(); // create an empty ArrayList
- int num = int(random(1, 10)); // get random value for number of connections
- for (int i = 0; i < num; i++) {
- PVector start = new PVector(-500, 0, 0); // start-vector in left side of box
- PVector end = new PVector(500, 0, 0); // end-vector in right side of the box
- connections.add(new Connect(start, end, maxOff));
- }
- }
- void mousePressed() {
- generate();
- }
- class Connect {
- // do random number of connections between start PVector and end PVector
- // for each connection, do random interpolation calculation (number of 'kinks' on each curve)
- // offset each 'kink' sligthly from what would be a straight line between start and end
- int num;
- float offset;
- color c = color(#FFD848);
- PVector start, end;
- ArrayList interpolation;
- // constructor
- Connect(PVector _start, PVector _end, float _maxOff) {
- start = _start;
- end = _end;
- offset = _maxOff;
- // random number off interpolations
- num = int(random(1, 10));
- interpolation = new ArrayList();
- for (float i = 0; i < 1; i += (1.0 / num)) {
- PVector l;
- l = PVector.lerp(start, end, i);
- // THIS IS NOT CORRECT, AS IT NEEDS A PERPENDICULAR OFFSET, NOT JUST A Y-Z OFFSET
- l.y = random(-offset, offset);
- l.z = random(-offset, offset);
- interpolation.add(l);
- }
- }
- void render() {
- stroke(c, 150);
- strokeWeight(0.5);
- noFill();
- beginShape();
- vertex(end.x, end.y, end.z);
- for (int i = interpolation.size()-1; i >= 1; i--) {
- PVector p = (PVector) interpolation.get(i);
- vertex(p.x, p.y, p.z);
- }
- vertex(start.x, start.y, start.z);
- endShape();
- }
- }
1