Basically I wish to draw a curve between 3 points in openGL as the below image indicates. I've found a couple of segments of code which would be useful for drawing a bezier curve using 4 points but for 3 points I've had really no success.
1
votes
2 Answers
7
votes
From the definition of a Bezier curve you have the following formula (for each x, y component):
x(t) = (1-t)^3*p1x + 3*t*(1-t)^2*c1x + 3*t^2*(1-t)*c3x + t^3*p3x
y(t) = (1-t)^3*p1y + 3*t*(1-t)^2*c1y + 3*t^2*(1-t)*c3y + t^3*p3y
In your case you know the mid point (p2x,p2y)
. You can also assume that c1x
and c2x
have the same value; and that c1y
and c2y
also have the same value
So we have the following equations at t=0.5
p2x = (3/4)*c1x+(p1x+p3x)/8
p2y = (3/4)*c1y+(p1y+p3y)/8
which are solved for c1x=c2x
and c1y=c2y
with
c1x = c2x = -(p1x-8*p2x+p3x)/6
c1y = c2y = -(p1y-8*p2y+p3y)/6
to give the final Bezier equation to use in terms of points (p1x,p1y)
, (p2x,p2y)
and (p3x,p3y)
:
x(t) = (1-t)^3 * [p1x]
+ 3*t*(1-t)^2 * [-(p1x-8*p2x+p3x)/6]
+ 3*t^2*(1-t) * [-(p1x-8*p2x+p3x)/6]
+ t^3 * [p3x]
y(t) = (1-t)^3 * [p1y]
+ 3*t*(1-t)^2 * [-(p1y-8*p2y+p3y)/6]
+ 3*t^2*(1-t) * [-(p1y-8*p2y+p3y)/6]
+ t^3 * [p3y]
Summary
Try the four control points
( p1x, p1y )
( -(p1x-8*p2x+p3x)/6, -(p1y-8*p2y+p3y)/6 )
( -(p1x-8*p2x+p3x)/6, -(p1y-8*p2y+p3y)/6 )
( p3x, p3y )
Here is an example I made with p1=(0,0)
, p2=(2,2)
and p3=(4,-1)
. I calculated the following control points
( 0, 0 )
( 2, 17/6 )
( 2, 17/6 )
( 4, -1)
with the results shown below:
1
votes
m5
– John Alexiou