0

I have read the thread to make this happen for 4 points but only in 2D space here .

I have implemented the answer for 3D but only for 3 control points here

I have read this post but don't understand the sudo code or the math

Can anyone simplify in java? I don't want to draw the curve as 2 segments of 3 points

Sync it
  • 1,180
  • 2
  • 11
  • 29

2 Answers2

1

Formula for cubic Bezier curve component (say X):

X(t) = P0.X*(1-t)^3 + 3*P1.X*(1-t)^2*t + 3*P2.X*(1-t)*t^2 + P3.X*t^3

where P0 and P3 are end points, and P1 an P2 are control points.

We have four points for curve (SrcPt array in that answer), end points coincide with Bezier endpoints, and two internal points on curve should define two control points P1 an P2 of Bezier curve. To calculate, we must know - what t parameters correspond to SrcPt[1] and SrcPt[2]. Let these parameters are 1/3 and 2/3 (possible issues are in linked answers).

So substitute t=1/3 and t=2/3 into the formula above:

SrcPt[1].X = SrcPt[0].X*(1-1/3)^3 + 3*P1.X*(1-1/3)^2*1/3 + 
             3*P2.X*(1-1/3)*1/3^2 + SrcPt[3].X*1/3^3
SrcPt[2].X = SrcPt[0].X*(1-2/3)^3 + 3*P1.X*(1-2/3)^2*2/3 + 
             3*P2.X*(1-2/3)*(2/3)^2 + SrcPt[3].X*2/3)^3

and solve this system for unknown P1.X and P2.X. So we will have all needed points to describe Bezier curve. Linked answer implements the solution.

Example - how changing t values for the same internal points influences on curve:

enter image description here

MBo
  • 77,366
  • 5
  • 53
  • 86
  • So that's what the determinants were about. Any reason why t was selected as 1/3 and 2/3 . Does selecting any other value effect the shape of the curve? – Sync it Dec 22 '20 at 18:12
  • Determinant is part of solution of equation system. 1/3 and 2/3 are choosed arbitrarily as equal-spaced points, selecting any other values definitely effects on the curve form. You can choose another values if you have some information about positions of internal points (function in that answer allows to use another values) – MBo Dec 23 '20 at 03:13
  • That's all I needed. Thank you – Sync it Dec 23 '20 at 07:49
  • Nice. And **what is the exact code** for drawing this? I know only that Java 7/8 has only a `path.quadTo()` method. whicah accepts 2 points (4 arguments). There's no `path.cubeTo()` which accepts 3 points (6 arguments), as e.g. in Android Java, so that we can move to the 1st point and draw the remaining points with `path.cubeTo()`. – Apostolos Feb 04 '22 at 17:09
  • @Apostolos This code is intended for ready cubic Bezier primitives. Perhaps some library/framework for Java contains cubics (language itself have no graphics primitives). Of course, you can implement cubic Bezier drawing using straight line segments through Bezier subdivision, but I'd choose simpler way with a library. – MBo Feb 05 '22 at 03:45
  • Thanks., I found a reference. No need for libraries or anything. (See my answer) – Apostolos Feb 06 '22 at 12:08
0

To draw the cubic curve after computing the points p[x][y]:

public void paint(Graphics g) {
  Graphics2D g2d = (Graphics2D) g;
  CubicCurve2D cubcurve = new CubicCurve2D.Float(p[0][0], p[0][1], p[1][0], p[1][1], p[2][0], p[2][1], p[3][0], p[3][0]);
  g2d.draw(cubcurve);
}

(Re: http://www.java2s.com/Tutorial/Java/0261__2D-Graphics/CubicCurve2DFloat.htm)

Apostolos
  • 3,115
  • 25
  • 28