Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
582 views
in Technique[技术] by (71.8m points)

matrix - Bezier curve method drawing wrong in java

Basically i have made code to draw Bezier curves but there is something wrong with it, it is not drawing how its suposed to. Core of this code should be correct, it was from one of my clases.

public class Bezier {

double[][] pointsY;
double[][] pointsX;
double t;

public Bezier(double[][] pointsX, double[][] pointsY, double t) {
    this.pointsY = pointsY;
    this.pointsX = pointsX;
    this.t = t;
}


    public void drawCurveNormalize (G_Graphics graphic){


        double[][] constants = {{1,0,0,0},
                                {-3,3,0,0},
                                {3,-6,3,0},
                                {-1,3,-3,1} };

        t = 1/t;
        double[][] tValues = {{1,t,t*t,t*t*t}};
       
        double[][] multiX = Util.matrixMultiplication(constants,pointsX);


        double[][] multiY = Util.matrixMultiplication(constants,pointsY);
        double[][] multi1;
        double[][] multi2;
        int dY = 0;
        int dX = 0;


        int xBeginning = (int)pointsX[0][0];
        int yBeginning = (int)pointsY[0][0];

        /*graphic.DDA(pointsX[0][0],pointsY[0][0],pointsX[1][0],pointsY[1][0],G_Color.G_cBlack);
        graphic.DDA(pointsX[1][0],pointsY[1][0],pointsX[2][0],pointsY[2][0],G_Color.G_cBlack);
        graphic.DDA(pointsX[2][0],pointsY[2][0],pointsX[3][0],pointsY[3][0],G_Color.G_cBlack);*/


        for (double i = t; i < 1; i+=t) {
            double z = 1;
            for (int j = 0; j < 3; j++) {
                tValues[0][j] = z;
                z = z*i;
            }

            multi1 = Util.matrixMultiplication(tValues,multiX);
            dX = (int)multi1[0][0];

            multi2 = Util.matrixMultiplication(tValues,multiY);
            dY = (int)multi2[0][0];

            graphic.DDA(xBeginning,yBeginning,dX,dY,G_Color.G_cBlack);
            xBeginning = dX;
            yBeginning = dY;
        }

}

input points are here

    double pointsX[][] = new double[][]
        {       {10},
                {100},
                {100},
                {350},
        };
    double pointsY[][] = new double[][]
            {       {10},
                    {10},
                    {200},
                    {200},
            };


Bezier bezier = new Bezier(pointsX, pointsY,10);

and it drawes this

and it drawes this

but it should draw betwen those lines but it should draw betwen those lines

also here is my code for MatrixMultiplication

public static double[][] matrixMultiplication(double[][] matrix1, double[][] matrix2) {
    if (matrix1[0].length != matrix2.length) {
       throw new RuntimeException("Matrix column/row fail");
    }

    double[][] matrixRet = new double[matrix1.length][matrix2[0].length];

    for (int i = 0; i < matrix1.length; i++) {
        for (int j = 0; j < matrix2[0].length; j++) {
            matrixRet[i][j] = 0.0;
        }
    }

    for (int i = 0; i < matrix1.length; i++) {
        for (int j = 0; j < matrix2[0].length; j++) {
            for (int k = 0; k < matrix1[0].length; k++) {
                matrixRet[i][j] += matrix1[i][k] * matrix2[k][j];
            }
        }
    }

   return matrixRet;
}
question from:https://stackoverflow.com/questions/66047918/bezier-curve-method-drawing-wrong-in-java

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is with this code:

t = 1/t;
double[][] tValues = {{1,t,t*t,t*t*t}};
...
for (double i = t; i < 1; i+=t) {
  ...
}

That tValues matrix is a variable, it needs to be recomputed every time you compute a point for a new t value. And that t value should definitely not be stored at the object level, and super definitely not get ivnerted any time you run your draw function. As your loop variable, keep it local to your for loop. At best, store your step size at the object level.

beginDrawing(); // whatever is your API's equivalent of this

double[][] xVal, yVal;
for(double t=0, step=0.01; t<1.0; t+=step) {
  double[][] tValues = {{1,t,t*t,t*t*t}};

  // get the x coordinate **for this t value**
  xVal = Util.matrixMultiplication(tValues, multiX);

  // get the y coordinate **for this t value**
  yVal = Util.matrixMultiplication(tValues, multiY);

  addVertex(xVal[0][0], yVal[0][0]);  // or equivalent
}

endDrawing();  // or equivalent

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...