/**************************************************************************** ** COPYRIGHT (C): 1997 Cay S. Horstmann. All Rights Reserved. ** PROJECT: Practical OO Development with C++ and Java ** FILE: Ellipse.java ** PURPOSE: shape class ** VERSION 1.0 ** PROGRAMMERS: Cay Horstmann (CSH) ** RELEASE DATE: 3-15-97 (CSH) ** UPDATE HISTORY: ****************************************************************************/ package practicaloo; import java.awt.Graphics; import java.io.PrintStream; public class Ellipse { public Ellipse(Point c, int a, int b) { _center = c; _xradius = a; _yradius = b; } public void scale(Point center, double s) { _center.scale(center, s); _xradius = (int)(_xradius * s); _yradius = (int)(_yradius * s); } public void move(int x, int y) { _center.move(x, y); } public void plot(Graphics g) { g.drawOval(_center.get_x() - _xradius, _center.get_y() - _yradius, 2 * _xradius, 2 * _yradius); } public void print(PrintStream p) { p.print(System.out); } public Point center() { return _center; } public int xradius() { return _xradius; } public int yradius() { return _yradius; } public boolean is_inside(Point p) { double x = (p.get_x() - _center.get_x()) / (double)_xradius; double y = (p.get_y() - _center.get_y()) / (double)_yradius; return Approx.lessOrEqual(x * x + y * y, 1.0); } public Point boundary_point(Point p) /* PURPOSE: Compute a point on the boundary of the shape RECEIVES: p - a point distinct from the center RETURNS: the intersection of the line joining the center and p with the boundary */ { double a = _center.angle(p); Point r = (Point)_center.clone(); r.move((int)(_xradius * Math.cos(a)), (int)(_yradius * Math.sin(a))); return r; } private Point _center; private int _xradius; private int _yradius; }