1
votes

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.

enter image description here

2
Note: a Bezier curve would not go through P2.Howard
Look at asymptote.sourceforge.net/doc/Bezier-curves.html on how the control points, and mid-points are used to construct a Bezier curve. In your case you know the end points, and the mid-point m5John Alexiou

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

Bezier Constructions

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

  1. ( p1x, p1y )
  2. ( -(p1x-8*p2x+p3x)/6, -(p1y-8*p2y+p3y)/6 )
  3. ( -(p1x-8*p2x+p3x)/6, -(p1y-8*p2y+p3y)/6 )
  4. ( 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

  1. ( 0, 0 )
  2. ( 2, 17/6 )
  3. ( 2, 17/6 )
  4. ( 4, -1)

with the results shown below:

Derive Plot

1
votes

Sounds like you want a Hermite spline.