Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find a middle point of a beizer curve?

I want to make a 'named' bezier curve. I want it to be one-word named so I don't have to worry about word-wrap.

I make bezier curve via P5 bezier(sx,sy,c1x,c1y,c2x,c2y,ex,ey) function and I want a string to be shown in the middle of bezier curve. But I don't know how to find 'the middle' of curve.

For now my result looks like this (I don't know where to start picking up this problem, so I went with the easier way of just printing text on a start of curve):

r1

But I want it to look like this:

r2

This means that I need P1 and P2 coordinates:

r3

Sorry for paint, but I don't have my code yet. As soon as I will have my hands on it I will add it here.

Here is code that draws a curve:

let
b = dest.inTriangle.middle, // destination triangle
g = this.outTriangle.p3,    // tip of out triangle
c = {x:b.x-g.x,y:b.y-g.y},  // distance between objects
r1 = {},                    // bezier point 1
r2 = {};                    // bezier point 2
if(c.x > 0){
    // b is on left
    r1 = {
        x: g.x + c.x/2,
        y: g.y
    };
    r2 = {
        x: b.x - c.x/2,
        y: b.y
    };
    }
else {
    // b is on right
    r1 = {
        x: g.x - c.x/2,
        y: g.y + c.y
    };
    r2 = {
        x: b.x + c.x/2,
        y: b.y - c.y
    };
}
noFill();
stroke(0);
bezier(
    g.x, g.y,
    r1.x, r1.y,
    r2.x, r2.y,
    b.x, b.y
);
noStroke();
fill(0);
text(this.name, g.x, (g.y-this.h/2))
like image 596
SkillGG Avatar asked Jan 16 '19 11:01

SkillGG


People also ask

How do you find the midpoint of a Bezier curve?

As a refresher, the formula for finding the midpoint of two points is a follows: M = (P0 + P1) / 2 . The calculation first determines the midpoint of the start point Z0 and the first control point C0, which gives us M0. It then finds the midpoint of both control points C0 and C1, which gives us M1.

How do you find the midpoint of a curve?

Measure the distance between the two end points, and divide the result by 2. This distance from either end is the midpoint of that line. Alternatively, add the two x coordinates of the endpoints and divide by 2.

How are Bezier curve points calculated?

These are vector equations. In other words, we can put x and y instead of P to get corresponding coordinates. For instance, the 3-point curve is formed by points (x,y) calculated as: x = (1−t)2x1 + 2(1−t)tx2 + t2x.


2 Answers

You can use the bezierPoint() function that comes with P5.js.

From the reference:

noFill();
var x1 = 85,
 x2 = 10,
 x3 = 90,
 x4 = 15;
var y1 = 20,
 y2 = 10,
 y3 = 90,
 y4 = 80;
bezier(x1, y1, x2, y2, x3, y3, x4, y4);
fill(255);
var steps = 10;
for (var i = 0; i <= steps; i++) {
  var t = i / steps;
  var x = bezierPoint(x1, x2, x3, x4, t);
  var y = bezierPoint(y1, y2, y3, y4, t);
  ellipse(x, y, 5, 5);
}

points drawn on curve

You'd probably want to use a value of 0.5 for t to get the midpoint.

like image 110
Kevin Workman Avatar answered Oct 24 '22 05:10

Kevin Workman


So, the formula to translate the 4 points in a function over time is the following (image from wikipedia):

enter image description here

Since you want the middle, and t ranges from 0 to 1, you just have to set t = 1/2

So

B(1/2) = 1/8 P0 + 3/8 P1 + 3/8 P2 + 1/8 P3

like image 34
Cristian Traìna Avatar answered Oct 24 '22 04:10

Cristian Traìna