Curvy beziers
in
Programming Questions
•
8 months ago
Hi,
I found
thiscode online showing bezier curves dashed line. The code shows one curve. I am using this to map on an image. Is there a way i can make 2 or more simultaneous curves from one Pvector to another. Been trying, but unsuccessfull. Few edits in there are/ might be off..Here's the code-
- float x1, y1, x2, y2, x3, y3, x4, y4, t;
- float bl = 0;
- int hselected = -1;
- PVector Delhi;
- PVector Mumbai;
- PVector Dubai;
- PVector Chennai;
- PVector Bangkok;
- int dashlength = 10;
- PImage img;
- void setup() {
- size(1024, 709);
- img = loadImage("map2.jpg");
- background(0);
- Delhi = new PVector(427, 235);
- Mumbai = new PVector(169, 325);
- Dubai = new PVector(36, 84 );
- Chennai = new PVector(219, 453 );
- Bangkok = new PVector(991, 433);
- t =0;
- x1 = Delhi.x;
- y1 = Delhi.y;
- x2 = 294;
- y2 = 75;
- x3 = 162;
- y3 = 101;
- x4 = Mumbai.x;
- y4 = Mumbai.y;
- noFill();
- }
- void draw() {
- image(img, 0 ,0);
- println(mouseX + "mouseY " + mouseY);
- // if(mouse
- // delhi();
- //mumbai();
- DelhiOrigin();
- if(mousePressed)
- if (hselected == 1) {
- x1 = mouseX;
- y1 = mouseY;
- } else if (hselected == 2) {
- x2 = mouseX;
- y2 = mouseY;
- } else if (hselected == 3) {
- x3 = mouseX;
- y3 = mouseY;
- } else if (hselected == 4) {
- x4 = mouseX;
- y4 = mouseY;
- }
- }
- void DelhiOrigin() {
- stroke(0.0);
- strokeWeight(1.0);
- beginShape();
- vertex(x1, y1);
- bezierVertex(x2, y2, x3, y3, x4, y4);
- endShape();
- ellipse(x1,y1,10,10);
- ellipse(x2,y2,10,10);
- ellipse(x3,y3,10,10);
- ellipse(x4,y4,10,10);
- bl = blength(x1, y1, x2, y2, x3, y3, x4, y4);
- //find any point on the curve:
- t+=0.01;
- if(t>1) {
- t=0;
- }
- double mu, mum1,mum13,mu3;
- mu = t;
- mum1 = 1 - mu;
- mum13 = mum1 * mum1 * mum1;
- mu3 = mu * mu * mu;
- float xn = (float) ( mum13*x1 + 3*mu*mum1*mum1*x2 + 3*mu*mu*mum1*x3 + mu3*x4 );
- float yn = (float) ( mum13*y1 + 3*mu*mum1*mum1*y2 + 3*mu*mu*mum1*y3 + mu3*y4 );
- stroke(255.0, 0.0, 0.0);
- strokeWeight(5.0);
- point(xn, yn);
- stroke(0.0, 100.0);
- strokeWeight(1.0);
- //line(x1, y1, x2, y2);
- //line(x2, y2, x3, y3);
- //line(x3, y3, x4, y4);
- stroke(255,0,0);
- strokeWeight(2);
- float dashnum = bl/dashlength;
- float dx = x1;
- float dy = y1;
- float t = 0;
- for (int i=0; i < dashnum*2; i++) {
- t += 0.001;
- float[] p = findPositionOnBezier(x1, y1, x2, y2, x3, y3, x4, y4, t);
- while ( t < 1 && dist(dx,dy,p[0],p[1]) < dashlength) {
- t += 0.001;
- p = findPositionOnBezier(x1, y1, x2, y2, x3, y3, x4, y4, t);
- }
- if (t >= 1) { p[0] = x4; p[1] = y4; }
- if(i*0.5 == Math.round(i*0.5)) {
- line(dx,dy,p[0],p[1]);
- }
- dx = p[0];
- dy = p[1];
- if (t == 1) { break; }
- }
- }
- void keyPressed() {
- if (keyCode == 38) {
- if (dashlength < 40) { dashlength++; }
- } else if (keyCode == 40) {
- if (dashlength > 4) { dashlength--; }
- } else {
- System.out.println(keyCode);
- }
- }
- void mouseReleased() {
- hselected = -1;
- }
- void mousePressed() {
- if (dist(mouseX,mouseY,x1,y1) < 5) {
- hselected = 1;
- } else if (dist(mouseX,mouseY,x2,y2) < 5) {
- hselected = 2;
- } else if (dist(mouseX,mouseY,x3,y3) < 5) {
- hselected = 3;
- } else if (dist(mouseX,mouseY,x4,y4) < 5) {
- hselected = 4;
- } else {
- hselected = -1;
- }
- }
- float[] findPositionOnBezier(float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4, float t) {
- float [] out = new float[]{0,0};
- double mu, mum1,mum13,mu3;
- mu = t;
- mum1 = 1 - mu;
- mum13 = mum1 * mum1 * mum1;
- mu3 = mu * mu * mu;
- out[0] = (float) ( mum13*x1 + 3*mu*mum1*mum1*x2 + 3*mu*mu*mum1*x3 + mu3*x4 );
- out[1] = (float) ( mum13*y1 + 3*mu*mum1*mum1*y2 + 3*mu*mu*mum1*y3 + mu3*y4 );
- return out;
- }
- float blength(float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4) {
- float out = 0;
- float t = 0;
- float px = x1;
- float py = y1;
- for (int i=0; i<100; i++) {
- t+=(0.01);
- float[] p = findPositionOnBezier(x1, y1, x2, y2, x3, y3, x4, y4, t);
- out+=dist(px,py,p[0],p[1]);
- px = p[0];
- py = p[1];
- }
- return out;
- }
1