Мне нужно создать простую Java-программу, которая рисует пиксель безье по пикселям через любое количество точек. На данный момент все выглядит нормально, за исключением того, что кривая всегда заканчивается на координатах x = 0 y = 0.Рисование кривой Безье в Java
Скриншот 1
Скриншот 2
мне нужно, чтобы он заканчивался в последний момент. Мой мозг сегодня не совсем работает, поэтому я ищу какую-то помощь.
Вот что у меня есть:
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);
}
Это метод для добавления точки (pointCount 0 изначально):
points[pointCount] = new Point();
points[pointCount].x = evt.getX() - this.offsetX;
points[pointCount].y = evt.getY() - this.offsetY;
pointCount++;
this.drawScene();
Что произойдет, если вы измените 'Math.pow (1-т, п-I)' в методе Бернштейна к: 'Math.pow (1-т, п + 1-я)'? – Rekin
@Rekin [Это происходит] (https://i.gyazo.com/a859b04c7494fec943f721eafecdd548.png) – Rai
У меня есть проблемы, переводящие значения параметров метода факториала в те, что указаны в Википедии: https://en.wikipedia.org/wiki/ Binomial_coefficient – Rekin