Path: csiph.com!x330-a1.tempe.blueboxinc.net!aioe.org!news.glorb.com!postnews.google.com!17g2000prr.googlegroups.com!not-for-mail From: byhesed Newsgroups: comp.lang.java.programmer Subject: Programming question determining two circle's relation Date: Sat, 30 Apr 2011 01:33:34 -0700 (PDT) Organization: http://groups.google.com Lines: 92 Message-ID: <892252a0-e31d-426b-8f47-d37c0b2ec364@17g2000prr.googlegroups.com> NNTP-Posting-Host: 119.202.36.92 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: posting.google.com 1304152415 3492 127.0.0.1 (30 Apr 2011 08:33:35 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sat, 30 Apr 2011 08:33:35 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: 17g2000prr.googlegroups.com; posting-host=119.202.36.92; posting-account=v_GC8QoAAABz34PprEBWdejdnnHZvg4_ User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.60 Safari/534.24,gzip(gfe) Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:3317 What's wrong with contains and overlaps method in Circle2D class? It returns incorrect answers. - A method contains(double x, double y) returns true if the specified point(x,y) is inside this circle. - A method contains(Circle2D circle) returns true if the specified circle is inside this circle. - A method overlaps(Circle2D circle) return true if the specified circle overlaps with this circle. I calculate the distance between two circles: - double distance = Math.sqrt(Math.pow(circle.x - getX(), 2.0) + Math.pow(circle.y - getY(), 2.0)); It's wired too me because I think it is not wrong. // TEST CODE public class Exercise10_11 { public static void main(String[] args) { Circle2D c1 = new Circle2D(2, 2, 5.5); System.out.println("Area is " + c1.getArea()); System.out.println("Perimeter is " + c1.getPerimeter()); System.out.println(c1.contains(3, 3)); System.out.println(c1.contains(new Circle2D(4, 5, 10.5))); System.out.println(c1.overlaps(new Circle2D(3, 5, 2.3))); } } // Circle2D Class public class Circle2D { private double x; private double y; private double radius; public Circle2D() { x = 0; y = 0; radius = 1; } public Circle2D(double x, double y, double radius) { this.x = x; this.y = y; this.radius = radius; } public double getX() { return x; } public double getY() { return y; } public double getArea() { return radius * radius * Math.PI; } public double getPerimeter() { return 2 * radius * Math.PI; } public boolean contains(double x, double y) { double distance = Math.pow(getX() - x, 2.0) + Math.pow(getY() - y, 2.0); if (distance <= radius) return true; else return false; } public boolean contains(Circle2D circle) { double distance = Math.sqrt(Math.pow(circle.x - getX(), 2.0) + Math.pow(circle.y - getY(), 2.0)); if (distance <= Math.abs(radius - circle.radius)) return true; else return false; } public boolean overlaps(Circle2D circle) { double distance = Math.sqrt(Math.pow(circle.x - getX(), 2.0) + Math.pow(circle.y - getY(), 2.0)); if (distance > Math.abs(radius - circle.radius) && distance < Math.abs(radius + circle.radius)) return true; else return false; } }