Answer the question
In order to leave comments, you need to log in
How to implement bezier curves for 3,4,5 points when there are 6?
There is a code for 6 Bizier points, what to change so that you can implement the same thing for 3,4,5 points. That is, use the same code, only for fewer points.
Bizje.java
import javax.swing.JFrame;
import javax.swing.JPanel;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import java.awt.Graphics;
public class Bizje extends JPanel {
Coordinates[] points;
Bizje(Coordinates[] points){
this.points = points;
}
public static void bezier6(Coordinates[] points,Graphics g) {
double t = 0.2, ts = 0.01;
while(t <= 1) {
double bn[] = {
/*1*/ -Math.pow((t-1), 5),
/*2*/ 5 * t * Math.pow(((1-t)), 4),
/*3*/ -10 * Math.pow((t),2)*Math.pow((1-t), 3),
/*4*/ 10 * Math.pow((t),3)*Math.pow((1-t), 2),
/*5*/ -Math.pow((t),4)*(-5 + 5*t),
/*6*/ Math.pow((t), 5)
};
double x = 0,y = 0;
for (int i =0; i < points.length; i++) {
x += points[i].x * bn[i];
y += points[i].y * bn[i];
}
g.drawRect((int) x,(int) y, 1, 1);
t += ts;
}
}
public static void main(String[] args) {
Coordinates []points = {new Coordinates(2,5),
new Coordinates(6, 1),
new Coordinates(8, 3),
new Coordinates(13, 12),
new Coordinates(21, 10),
new Coordinates(27, 21)
};
JFrame window = new JFrame("Brez");
window.setSize(100,100);
window.setContentPane(new Bizje(points));
window.setDefaultCloseOperation(EXIT_ON_CLOSE);
window.setVisible(true);
}
public void paintComponent(Graphics g){
super.paintComponents(g);
/*
* Coordinates []points = {new Coordinates(2,5), new Coordinates(6,1), new
* Coordinates(2,3), new Coordinates(13,12), new Coordinates(21,10), new
* Coordinates(27,21) };
*/
bezier6(points,g);
}
}
public class Coordinates {
int x,y;
Coordinates(int x, int y){
this.x = x;
this.y = y;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question