I have a simple bezier code here and would like to know if it is possible to give the weight point that shapes the line into a curve more weight? Let the curve be pulled closer to the point. Is there a way to get a weight value? Like I tried to mockup in this image:
Thank you! @mnse
but I am seeking a way to increase the amount of curvature. Pull the curve closer to the dot. I have a specific application where I try to match a specific behavior. This is a screenshot from that app:
and this is how it looks with my code in comparison
I try to find out how to match the curvature from the example… It seems to be more attracted and the curve is a bit steaper towards the weight point.
Yes it’s possible, and the P5.Vector class offers many helpful methods to enhance the capabilities of vectors. In your original post you use vectors to save x- & y-coordinates, but you could do much more with it such as calculating its angle and magnitude. Combined with P5’s build-in bezier methods you can achieve your goal.
From the midpoint of the anchor points —which are the begin and end vectors— you can calculate the angle between the midpoint and your mouse coordinates. Your new, ‘weighted’ control point is an amplification of the magnitude between those two.
The code below is done with Processing, so you’ll have to convert it to P5 yourself.
PVector ap1; // anchor point 1
PVector ap2; // anchor point 2
PVector mid; // midpoint of anchors
void setup() {
size(600, 600);
ap1 = new PVector(0, height/2);
ap2 = new PVector(width, height/2);
mid = PVector.lerp(ap1, ap2, 0.5);
noLoop();
strokeWeight(2);
noFill();
}
void draw() {
background(0);
stroke(#00FF00);
point(mid.x, mid.y);
drawBezier(#FFFFFF, calcControlPoint(1.0));
drawBezier(#FF0000, calcControlPoint(1.4));
}
PVector calcControlPoint(float weight) {
PVector base = new PVector(mouseX, mouseY);
PVector diff = PVector.sub(base, mid);
float magnitude = diff.mag() * weight;
float angle = diff.heading();
println("'cp' is now at an angle of", degrees(angle), "degrees towards 'mid'");
return PVector.fromAngle(angle).setMag(magnitude).add(mid);
}
void drawBezier(color c, PVector cp) {
stroke(c);
beginShape();
vertex(ap1.x, ap1.y);
quadraticVertex(cp.x, cp.y, ap2.x, ap2.y);
endShape();
}
void mouseMoved() {
redraw();
}