Checking the Boundaries of an Ellipse in Java
From the equation
(x - h)2 |
+ |
(y - k)2 |
= 1 |
a2 |
b2 |
It can be deduced that
y = k ± b/a√(a2 - (x - h)2)
;
And conversely
x = h ± a/b√(b2 - (y - k)2)
Hence, the boundaries of any circle lie in the range
y ≥ k - b/a√(a2 - (xexternal - h)2);
y ≤ k + b/a√(a2 - (xexternal - h)2)
and
x ≥ h - a/b√(b2 - (yexternal - k)2);
x ≤ h + a/b√(b2 - (yexternal - k)2)
Code to Detect Entrance into an Elliptical Region in Java
To check for when a second graphic enters the ellipse,
we will continually use the x position
of this second graphic in the ellipse equation to detect
when its y position lies between the up and down
limits at the x position in question:
y2nd_img(top) >
k - b/a√(a2 - (x2nd_img - h)2)
and y2nd_img(bottom) <
k + b/a√(a2 - (x2nd_img - h)2)
;
At the same time, we will use the y position
of the second graphic in the circle equation to detect
when its x position lies between the left and
right limits at the y position in question:
x2nd_img(left) >
h - a/b√(b2 - (y2nd_img - k)2)
and x2nd_img(right) <
h + a/b√(b2 - (y2nd_img - k)2)
Create 2 new classes; File, New.
Call them PanelsEllipticalRegion and EllipticalRegion.
Type out the adjoining Java code for detecting the instance a travelling
body crosses the boundary of an ellipse.
By The Way: Notice how the equations for a circle
are similar to those of an ellipse;
No surprise there!
A circle is just an ellipse in its simplest form.
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 EllipticalRegion elp_region;
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());
elp_region = new EllipticalRegion();
canvas_panel.add(elp_region, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent evt) {
elp_region.ellipsedSquare();
}
}
Java code for EllipticalRegion class
package dymetric;
import java.awt.*;
public class EllipticalRegion extends Canvas {
protected Color sq_colour;
protected int x_square = 10;
protected int y_square = 150;
protected int previous_x = x_square;
protected int previous_y = y_square;
protected final int sqLENGTH = 100;
protected int h = 350;
protected int k = 175;
protected final int a = 200;
protected final int b = 125;
protected int x = h - a;
protected int y = k - b;
public EllipticalRegion() {
setBackground(Color.LIGHT_GRAY);
sq_colour = Color.YELLOW;
}
public void paint(Graphics g) {
g.drawOval(x, y, 2*a, 2*b);
g.clearRect(previous_x, previous_y, sqLENGTH, sqLENGTH);
g.setColor(sq_colour);
g.fillRect(x_square, y_square, sqLENGTH, sqLENGTH);
previous_x = x_square;
previous_y = y_square;
}
public void ellipsedSquare() {
while (x_square + sqLENGTH < 750) {
int square_left = x_square;
int square_right = x_square + sqLENGTH;
int square_top = y_square;
int square_bottom = y_square + sqLENGTH;
int x_left_det = (int) Math.round(((double) b / a)
* Math.sqrt(Math.pow(a, 2) - Math.pow((square_left - h), 2)));
int x_right_det = (int) Math.round(((double) b / a)
* Math.sqrt(Math.pow(a, 2) - Math.pow((square_right - h), 2)));
int y_up_det = (int) Math.round(((double) a / b)
* Math.sqrt(Math.pow(b, 2) - Math.pow((square_top - k), 2)));
int y_down_det = (int) Math.round(((double) a / b)
* Math.sqrt(Math.pow(b, 2) - Math.pow((square_bottom - k), 2)));
if (square_top > k - x_left_det && square_bottom < k + x_left_det
&& square_top > k - x_right_det && square_bottom < k + x_right_det
&& square_left > h - y_up_det && square_right < h + y_up_det
&& square_left > h - y_down_det && square_right < h + y_down_det) {
sq_colour = Color.GREEN;
} else {
sq_colour = Color.YELLOW;
}
paint(this.getGraphics());
x_square += 10;
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
}
}
}