Checking the Boundaries of a Quadratic Curve in Java
Remember that any quadratic equation always have two roots for any
value of y (except at it's maximum or minimum point).
All we need to do is use these two roots (x values) as boundaries
for our check.
y = ax2 + bx + c
ax2 + bx + (c-y) = 0
x = |
-b ± √(b2 - 4a(c-y)) |
2a |
Our range will then be:
-b - √(b2 - 4a(c-y)) |
≤ x ≤ |
-b + √(b2 - 4a(c-y)) |
2a |
2a |
where a, b, and c
are constants.
We will reuse the moving ball graphic and check for when
it enters the region of our curve.
Code to Detect Entrance into Quadratic Region in Java
To check for when our ball enters the quadratic curve,
we will continually check the x position
of the ball against the x position gotten using the
quadratic equation at the same y position
as that of the ball.
We'll designate the coordinates of the ball as
(xb, yb), and those of
the curve as (xq, yq).
Create 2 new classes; File, New.
Call them PanelsQuadraticRegion and QuadraticRegion.
Type out the adjoining Java code for detecting the instance a travelling
body crosses the boundary of a quadratic curve.
Canvas/Button class
package dymetric;
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
public class ButtonandCanvasPanels implements ActionListener {
public JPanel button_panel, canvas_panel;
public JButton motion_bttn;
public QuadraticRegion qregion;
public ButtonandCanvasPanels() {
button_panel = new JPanel();
button_panel.setBackground(Color.PINK);
button_panel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
motion_bttn = new JButton("Move");
motion_bttn.setBackground(new Color(255, 0, 255));
motion_bttn.addActionListener(this);
button_panel.add(motion_bttn);
canvas_panel = new JPanel();
canvas_panel.setLayout(new BorderLayout());
qregion = new QuadraticRegion();
canvas_panel.add(qregion, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent evt) {
qregion.checkBoundary();
}
}
Java code for QuadraticRegion class
package dymetric;
import java.awt.*;
public class QuadraticRegion extends Canvas {
Color ball_colour;
protected int x_ball = 50;
protected int y_ball = 100;
protected int previous_x = x_ball;
protected int previous_y = y_ball;
protected final int ballWIDTH, ballHEIGHT;
protected int xq_start = 300;
protected int yq_start = 50;
protected int xq_min = 450;
protected int yq_min = 300;
protected double xq_lb;
protected double xq_ub;
protected int xq_stop = 600;
protected int x = xq_start;
protected int y = yq_start;
protected double a, b, c;
protected double discriminant;
protected final int aWIDTH, aHEIGHT;
public QuadraticRegion() {
setBackground(Color.LIGHT_GRAY);
ball_colour = Color.YELLOW;
aWIDTH = aHEIGHT = 5;
a = (double) (yq_start - yq_min) / Math.pow((xq_start - xq_min), 2);
b = -2 * a * xq_min;
c = yq_min + a * Math.pow(xq_min, 2);
ballWIDTH = ballHEIGHT = 100;
discriminant = Math.sqrt(b * b - 4 * a * (c - (y_ball+(double)(ballHEIGHT/2))));
if (a < 0) {
xq_lb = (double)(-b + discriminant) / (2 * a);
xq_ub = (double)(-b - discriminant) / (2 * a);
} else {
xq_lb = (double)(-b - discriminant) / (2 * a);
xq_ub = (double)(-b + discriminant) / (2 * a);
}
}
public void paint(Graphics g) {
g.setColor(Color.BLACK);
for (; x < xq_stop; x++) {
y = (int) Math.round(a * x * x + b * x + c);
g.fillOval(x, y, aWIDTH, aHEIGHT);
}
x = xq_start;
g.setColor(Color.LIGHT_GRAY);
g.fillOval(previous_x, previous_y, ballWIDTH, ballHEIGHT);
g.setColor(ball_colour);
g.fillOval(x_ball, y_ball, ballWIDTH, ballHEIGHT);
previous_x = x_ball;
previous_y = y_ball;
}
public void checkBoundary() {
while (x_ball <= 630) {
if ((x_ball <= xq_lb && x_ball+ballWIDTH >= xq_lb)
|| (x_ball <= xq_ub && x_ball + ballWIDTH >= xq_ub)) {
ball_colour = Color.RED;
} else if (x_ball >= xq_lb && x_ball + ballWIDTH <= xq_ub) {
ball_colour = Color.GREEN;
} else {
ball_colour = Color.YELLOW;
}
paint(this.getGraphics());
x_ball += 5;
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
}
}
}