토요일, 9월 27, 2008

Draw Bezier curved line


Bezier 곡선 그리기

private void drawBezier(GC g, Point startPt, Point startCtrlPt,
            Point endPt, Point endCtrlPt, double step) {
        BezierDimension x = new BezierDimension(startPt.x, startCtrlPt.x,
                endCtrlPt.x, endPt.x);
        BezierDimension y = new BezierDimension(startPt.y, startCtrlPt.y,
                endCtrlPt.y, endPt.y);

        double t = 0;
        Point midPt = startPt;

        while (t < 1) {
            Point nextPt = new Point(x.getValue(t), y.getValue(t));

            g.drawLine(midPt.x, midPt.y, nextPt.x, nextPt.y);
            midPt = nextPt;
            t += step;
        }
        g.drawLine(midPt.x, midPt.y, endPt.x, endPt.y);
    }



public class BezierDimension {
   
    double a, b, c, d;

    public BezierDimension(int p0, int p1, int p2, int p3) {
        d = p0;
        c = 3 * (p1 - p0);
        b = 3 * (p2 - p1) - c;
        a = p3 - p0 - c - b;
    }

    public int getValue(double t) {
        // added 0.5 so that floor is actually rounding
        return (int) (a * t * t * t + b * t * t + c * t + d + 0.5);
    }
}

댓글 없음:

댓글 쓰기