Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing bezier curve in Java

Tags:

java

swing

bezier

I need to create a simple Java program, that draws a bezier curve pixel by pixel through any amount of points. At the moment, everything seems to be ok except that the curve always ends at x=0 y=0 coordinates.

Screenshot 1

enter image description here

Screenshot 2

enter image description here

I need it to end at the last point. My brain is not quite working today, so I'm looking for some help.

Here is what I have:

private void drawScene(){

    precision = Float.parseFloat(this.jTextField4.getText());
    //Clears the screen and draws X and Y lines
    g.setColor(Color.white);
    g.fillRect(0, 0, pWidth, pHeight);

    g.setColor(Color.gray);
    g.drawLine(0, offsetY, pWidth, offsetY);
    g.drawLine(offsetX, 0, offsetX, pHeight);
    //Drawing the points
    if(pointCount > 0){
        for(int i = 0;i<pointCount;i++){
            g.setColor(Color.red);
            g.drawString(String.valueOf(i+1), points[i].x + offsetX, points[i].y - 6 + offsetY);
            g.drawOval(points[i].x + offsetX, points[i].y - 6 + offsetY, 3, 3);
        }
    }
    //Drawing the curve
    if(pointCount > 1){
        float t = 0;
        while(t <= 1){
            g.setColor(Color.gray);
            this.besierCurvePixel(t);
            t += precision;
        }
    }
}

//Factorial
private static int fact(int n) {
    int fact = 1;
    for (int i = 1; i <= n; i++) {
        fact *= i;
    }
    return fact;
}
//Bernstein polynomial
private static double bernstein(float t, int n, int i){

   return (fact(n) / (fact(i) * fact(n-i))) * Math.pow(1-t, n-i) * Math.pow(t, i);
}

private void besierCurvePixel(float t){

    double bPoly[] = new double[pointCount];

    for(int i = 0; i < pointCount; i++){
        bPoly[i] = bernstein(t, pointCount, i+1);
    }

    double sumX = 0;
    double sumY = 0;

    for(int i = 0; i < pointCount;  i++){
        sumX += bPoly[i] * points[i].x;
        sumY += bPoly[i] * points[i].y;
    }

    int x, y;
    x = (int) Math.round(sumX);
    y = (int) Math.round(sumY);

    g.drawLine(x + offsetX, y + offsetY, x + offsetX, y + offsetY);

}

This is the method for adding the points (pointCount is 0 initially):

    points[pointCount] = new Point();
    points[pointCount].x = evt.getX() - this.offsetX;
    points[pointCount].y = evt.getY() - this.offsetY;
    pointCount++;

    this.drawScene();
like image 895
Rai Avatar asked Dec 15 '15 15:12

Rai


People also ask

How do you draw a Bezier curve?

To draw a line using this equation, one can divide the curve into smaller segments, calculate the end points of each segment using the Bezier cubic equation and draw the line for the segment. For instance, one can draw a line between the points defined by t = 0 and t = 0.01, then t = 0.01 and t = 0.02, and so on.

How do you draw a curved line in Java graphics?

You can draw a Bézier curve using the Java 2D Object Path2D. Double. Just call the method curveTo(float x1, float y1, float x2, float y2, float x3, float y3) and define the 3 coordinate. GeneralPath is a legacy class, Path2D.

How do you draw a Bezier tool?

Click the Bezier tool, press the letter b on your keyboard, or press Shift + F6 to activate the Bezier tool. Click the Spiro icon. Click where you want to begin your curved line, drag to where you want a curve, click again, drag to where you want another curve, click again. Continue until you have completed your line.

What is sketch Bezier curves?

A Bézier curve (/ˈbɛz. i. eɪ/ BEH-zee-ay) is a parametric curve used in computer graphics and related fields. A set of discrete "control points" defines a smooth, continuous curve by means of a formula.


1 Answers

The problem was here

for(int i = 0; i < pointCount; i++){
    bPoly[i] = bernstein(t, pointCount, i+1);
}

The second parameter in the bernstein method was incorrect. Basically If I have 3 points, it should be 2 not 3;

bPoly[i] = bernstein(t, pointCount-1, i+1);
like image 61
Rai Avatar answered Sep 30 '22 00:09

Rai