java - 贝塞尔曲线方法在java中绘制错误
问题描述
基本上我已经编写了绘制贝塞尔曲线的代码,但是它有问题,它没有绘制它应该如何绘制。这段代码的核心应该是正确的,它来自我的一个类。
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;
}
}
输入点在这里
double pointsX[][] = new double[][]
{ {10},
{100},
{100},
{350},
};
double pointsY[][] = new double[][]
{ {10},
{10},
{200},
{200},
};
Bezier bezier = new Bezier(pointsX, pointsY,10);
它画了这个
这也是我的 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;
}
解决方案
问题在于这段代码:
t = 1/t;
double[][] tValues = {{1,t,t*t,t*t*t}};
...
for (double i = t; i < 1; i+=t) {
...
}
该矩阵是一个变量,每次计算新值的点时都tValues
需要重新计算它。t
并且该t
值绝对不应该存储在对象级别,并且绝对不会在您运行绘图功能时被反转。作为您的循环变量,将其保留在您的for
循环中。充其量,将您的step
尺寸存储在对象级别。
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
推荐阅读
- javascript - 按下按钮时,如何使用带有新数据的 django 视图更新 firebase 记录?
- makefile - 查找:路径必须在表达式之前:'1'
- arrays - 保留内在分配中的分配界限
- sql-server - SQL Server 并发插入和大容量插入
- r - 如果多列的值在不同的数据框大小中相等 - R
- java - 无法连接到 Eureka 服务器。异常:java.net.ConnectException:连接被拒绝:连接
- firebase - 按需在 n 秒后执行云功能
- mysql - 我无法更改变量“max_execution_time”
- c# - Angular 6 和 .Net Core 2 - 生成 XSRF 令牌,但在发送回 API 时为空
- python - 如何调试此脚本,以便将数字替换为 python 中存在 % 的位置?