Create a curve that uses a quadratic parametric approach with three interpolated control points. The equations which describe the curve are:
Create a curve that uses a quadratic parametric approach with three interpolated control points. The equations which describe the curve are:
$$f_x(u) = c_0 u^2 + c_1 u + c_2 $$
and
$$f_y(u) = c_3 u^2 + c_4 u + c_5 $$
where $u$ ranges from 0.0 to 1.0. The first control point $$p_0$$ is for $u=0$, the second $p_1$ for $u=0.5$, and the third for $p_2$ and at $u=1.0$.
Modify the code to implement your solution. It is sufficient for this task to get the program working with three control points, but you are most welcome to attempt a solution that works on four or more.
Code:
import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; import java.awt.geom.Path2D; import java.awt.geom.Point2D; import javax.swing.JFrame; import javax.swing.JTabbedPane; import javax.swing.SwingUtilities; public class DotToDotDemo implements Runnable { /** * DotToDotDemo - A demo which draws dots between points to form a curve. * Eric McCreath 2015, 2019 - GPL * Modified by Matthew Aitchison 2022. */ JFrame jframe; public DotToDotDemo() { SwingUtilities.invokeLater(this); } public static void main(String[] args) { new DotToDotDemo(); } public void run() { jframe = new JFrame("Splines"); jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTabbedPane tab = new JTabbedPane(); tab.addTab("Dot-To-Dot", new CurveDraw(3, new Spline() { // drawCurve - draw the "curve" bye connecting the control points @Override void drawCurve(Point2D.Double[] p, Graphics2D g) { for (int i = 0; i < p.length - 1; i++) { for (double u = 0.0; u < 1.0; u += 0.001) { // We are using linear interpolation to draw straight lines between the control points // you need to modify this to draw a quadratic fitted to the three control points. // Bonus: Get this working with 5 control points (optional, and not required for full marks) // this is much harder, and will require taking the derivative... as well as making a few // assumptions about the end points. double x = p[i].x * u + p[i + 1].x * (1.0 - u); double y = p[i].y * u + p[i + 1].y * (1.0 - u); g.fillRect((int) x, (int) y, 1, 1); } } } })); jframe.getContentPane().add(tab); jframe.setVisible(true); jframe.pack(); } }
Step by step
Solved in 2 steps with 3 images