java 贝塞尔_在Java中绘制贝塞尔曲线

我需要创建一个简单的Java程序,通过任意数量的点逐个像素地绘制贝塞尔曲线.此刻,一切似乎都没问题,只是曲线总是在x = 0 y = 0坐标处结束.

截图1

3Wcli.png

截图2

rSpR3.png

我需要它在最后一点结束.我的大脑今天工作不太好,所以我正在寻求帮助.

这是我有的: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

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);

}

这是添加点的方法(pointCount最初为0):points[pointCount] = new Point();

points[pointCount].x = evt.getX() - this.offsetX;

points[pointCount].y = evt.getY() - this.offsetY;

pointCount ;

this.drawScene();

解决方法:

问题出在这里for(int i = 0; i < pointCount; i ){

bPoly[i] = bernstein(t, pointCount, i 1);

}

bernstein方法中的第二个参数是不正确的.基本上如果我有3分,那应该是2而不是3分;bPoly[i] = bernstein(t, pointCount-1, i 1);来源:https://www.icode9.com/content-1-304651.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值