How to make a curve to join three points?

Hi, I’m developing a graphical editor and now I’m trying to create a curve that passes through three given points with Java2D (I’m drawing the curves inside a class that extends JPanel by overriding the paintComponent method). How can I make it?

Also, I have to know where the middle point is to drag it and modify the curve.

The image that is attached is what I’m looking for (there is done with a QuadCurve2D, so I cannot obtain the middle point).

Thanks!

You should check out cubic bezier curves.

In a Quadratic Bézier curve, the mid point does not go through the curve. However, you can calculate where the mid point is on the curve by this formula:

float t = 0.5;
float midPointX = ttx2 + 2ft(1f-t)x1 + (1f-t)(1f-t)x0;
float midPointY = t
ty2 + 2ft*(1f-t)y1 + (1f-t)(1f-t)*y0;

Now I suspect, as you’re doing an editor, you don’t want to calculate the mid point on the curve, rather you want to calculate the Quadratic Bézier curves mid point (so you can draw the curve). So I think you want to transform the above algorithm to find x1 and y1. But I’m not 100% sure. If you get it working, please post your findings. Cheers.

Riven and Tusaki made some code to do that here: