Search code examples
javaconstructorgettersetter

Im trying to find the distance between two points


I have been at it for a couple of days and I can't seem to figure out how to find the distance between 2 points and how make the contains and overlaps

This is the UML diagram enter image description here

I have done all the setters and getter objects except for the contains and overlaps

This is the test parameters with the expected output https://i.sstatic.net/HJMYG.png

This is the code I have done

public class Question03 {
 
        public static void main(String[] args) {
            Circle c1 = new Circle(new Point(0, 0), 1.0);
            
            System.out.println(c1.getArea());
            
            Circle c2 = new Circle(new Point(1, 0), 1.0);
            
            System.out.println(c2.getArea());
            System.out.println(c1.getCenter().distance(c2.getCenter()));
            System.out.println(c1.contains(new Point(0, 0.5)));
            System.out.println(c1.overlaps(c2));
            
            Circle c3 = new Circle(new Point(0, 4), 1.0);
            
            System.out.println(c3.contains(new Point(0, 0)));
            System.out.println(c3.overlaps(c1));
        
    public class Circle {
            Point center;
            double radius;
        Circle (Point center, double radius){
        this.center=center;
        this.radius=radius;
        }
        public Point getCenter() {
            return this.center;
        }
        public double getRadius(){ 
            return this.radius; 
        }
        public Point setCenter(){
            return this.center;
        }
        public double setRadius(){
            return this.radius;
        }
        public double getArea(){
        double area = Math.PI * radius * radius;
                return area;
            
        }
        public double getPerimeter(){
        double perimeter = Math.PI * (radius * 2);    
                return perimeter;
            
        }
        public boolean contains(Point p){
         return contains;   
        }
        public boolean overlaps(Circle c){
            return overlaps;
        }        
        }
        
        public class Point {
         double x;
         double y;
         Point(double x, double y){   
         this.x=x;
         this.y=y;       
        }
            public double getX(){
            return this.x;
        }
            public double getY(){
            return this.y;
            
            }      
            public double setX(){
            return this.x;
            }
        public double setY(){
            return this.y;
            }  
        public double distance(Point p){
        double distance = Math.sqrt((-x)*(-x) + (-y)*(-y));   
            return distance;
        }
        }

I don't know what to do after this I am really stuck and I can't find anything on this on the interent please help :(


Solution

  • Properly apply the distance formula between "this" point and the "p" passed in as a parameter. Next contains is true if the point "p" passed in as a parameters distance is less than the circles radius. Lastly overlaps is true if the distance between the centers is less than the sum of the radius of each circle.

    The code is untested but it probably works if everything else in the project works.

    public double distance(Point p){
        return Math.sqrt( (this.x - p.getX()) * (this.x - p.getX()) + 
                          (this.y - p.getY()) * (this.y - p.getY())
                        );
    }
    
    public boolean contains(Point p){
        return this.center.distance(p) < this.radius;  
    }
    
    public boolean overlaps(Circle c){
        double d = this.center.distance(c.getCenter());
        return d < this.radius + c.getRadius();
    }