import java.awt.*; public class BezierCurve { final double EPSILON = 0.025; final private int X = 0; final private int Y = 1; final private int A = 0; final private int B = 1; final private int C = 2; final private int D = 3; int[][] p = new int[4][2]; // POINTS int[][] c = new int[4][2]; // POLYNOMIAL COEFFICIENTS private int[][] bezierMatrix = { {-1, 3, -3, 1}, { 3, -6, 3, 0}, {-3, 3, 0, 0}, { 1, 0, 0, 0} }; public BezierCurve () { } // set the 4 points of the bezier curve public void setPoints(int p0x, int p0y, int p1x, int p1y, int p2x, int p2y, int p3x, int p3y) { p[0][X] = p0x; p[0][Y] = p0y; p[1][X] = p1x; p[1][Y] = p1y; p[2][X] = p2x; p[2][Y] = p2y; p[3][X] = p3x; p[3][Y] = p3y; setCoefficients(); } // draws the coordinates of a point next to that point public void writeCoordinates(Graphics g) { g.setColor(Color.ORANGE); for (int i = 0; i < 4; i++) { g.drawString("(" + p[i][X] + ", " + p[i][Y] + ")", p[i][X] + 8, p[i][Y]); } } // draw lines connecting the 4 points public void drawGuidelines(Graphics g) { g.setColor(Color.GREEN); g.drawLine(p[0][X], p[0][Y], p[1][X], p[1][Y]); g.drawLine(p[1][X], p[1][Y], p[2][X], p[2][Y]); g.drawLine(p[2][X], p[2][Y], p[3][X], p[3][Y]); } // draw the bezier curve public void drawCurve(Graphics g) { g.setColor(Color.BLACK); for (double t = 0 ; t <= 1 ; t += EPSILON) { g.drawLine((int)X(t), (int)Y(t), (int)X(t+EPSILON), (int)Y(t+EPSILON)); } } // draw a circle around the 4 points defining the curve public void drawPoints(Graphics g) { for (int i = 0; i < 4; i++) { if (i == 0 || i == 3) { g.setColor(Color.BLUE); g.fillOval(p[i][X] - 5, p[i][Y] - 5, 10, 10); } else { g.setColor(Color.RED); g.drawOval(p[i][X] - 5, p[i][Y] - 5, 10, 10); } } } // set coordinate of point n to (x, y) public void setPoint(int n, int x, int y) { p[n][X] = x; p[n][Y] = y; setCoefficients(); } // get (x, y) coordinates of point n public int[] getPoint(int n) { int[] coordinates = {p[n][X], p[n][Y]}; return coordinates; } // calculate x(t) = Ax*t^3 + Bx*t^2 + Cx*t + Dx public double X(double t) { return t * ( t * ( t * c[A][X] + c[B][X]) + c[C][X]) + c[D][X]; } // calculate y(t) = Ay*t^3 + By*t^2 + Cy*t + Dy public double Y(double t) { return t * ( t * ( t * c[A][Y] + c[B][Y]) + c[C][Y]) + c[D][Y]; } // calculate the coefficients // multiply the Bezier matrix by the matrix defined by the x values of 4 points // multiply the Bezier matrix by the matrix defined by the y values of 4 points public void setCoefficients() { c[A][X] = bezierMatrix[0][0] * p[0][X] + bezierMatrix[0][1] * p[1][X] + bezierMatrix[0][2] * p[2][X] + bezierMatrix[0][3] * p[3][X]; c[B][X] = bezierMatrix[1][0] * p[0][X] + bezierMatrix[1][1] * p[1][X] + bezierMatrix[1][2] * p[2][X] + bezierMatrix[1][3] * p[3][X]; c[C][X] = bezierMatrix[2][0] * p[0][X] + bezierMatrix[2][1] * p[1][X] + bezierMatrix[2][2] * p[2][X] + bezierMatrix[2][3] * p[3][X]; c[D][X] = bezierMatrix[3][0] * p[0][X] + bezierMatrix[3][1] * p[1][X] + bezierMatrix[3][2] * p[2][X] + bezierMatrix[3][3] * p[3][X]; c[A][Y] = bezierMatrix[0][0] * p[0][Y] + bezierMatrix[0][1] * p[1][Y] + bezierMatrix[0][2] * p[2][Y] + bezierMatrix[0][3] * p[3][Y]; c[B][Y] = bezierMatrix[1][0] * p[0][Y] + bezierMatrix[1][1] * p[1][Y] + bezierMatrix[1][2] * p[2][Y] + bezierMatrix[1][3] * p[3][Y]; c[C][Y] = bezierMatrix[2][0] * p[0][Y] + bezierMatrix[2][1] * p[1][Y] + bezierMatrix[2][2] * p[2][Y] + bezierMatrix[2][3] * p[3][Y]; c[D][Y] = bezierMatrix[3][0] * p[0][Y] + bezierMatrix[3][1] * p[1][Y] + bezierMatrix[3][2] * p[2][Y] + bezierMatrix[3][3] * p[3][Y]; } }