2015-11-07 35 views
-1

这是我在java写的一些代码,它是一个圆圈对象类。我写了方法,我需要两个更多的方法,并且与第一个方法构造的是 一个包含(圆c)的方法,如果指定圆c在圆内,则返回true。第二个是一个重叠的方法(圆c),如果指定的圆c与该圆重叠,则返回true。所有我需要的是如何使这两个方法与参数是另一个圈子对象java圈内圈,如何知道两圈是否重叠

这是我的代码

public class Circle 
{ 
// instance variables - replace the example below with your own 
private double radius; 
private double centerX; 
private double centerY; 

/** 
* Constructor for objects of class Circle 
*/ 
public Circle() 
{ 
    this.radius = 1; 
    this.centerX = 0; 
    this.centerY = 0; 
} 
/** 
* Constructor for objects of class Circle 
*/ 
public Circle(double radius, double centerx, double centery) 
{ 
    this.radius = radius; 
    this.centerX = centerX; 
    this.centerY = centerY; 
} 
/** 
* Constructor for objects of class Circle 
*/ 
public Circle(double pRadius) 
{ 
    radius = pRadius; 
} 
/** 
* An example of a method - replace this comment with your own 
* 
* @param y a sample parameter for a method 
* @return  the sum of x and y 
*/ 
public void setRadius(double pRadius) 
{ 
    radius = pRadius; 
} 
/** 
* getRadius method will return the radius of the cricle 
* @param none 
* @return radius of type double 
*/ 
public double getRadius() 
{ 
    return radius ; 
} 
/** 
* getcenterX method will return the x coordinates of the circle 
* @param none 
* @return centerx of type double 
*/ 
public double getCenterX() 
{ 
    return centerX ; 
} 
/** 
* getcenterY method will return the y coordinates of the circle 
* @param none 
* @return centery of type double 
*/ 
public double getCenterY() 
{ 
    return centerY ; 
} 
/** 
* 
*/ 
    public double calcPerimeter() 
{ 
    return 2*Math.PI*getRadius(); 
} 
/** 
* 
*/ 
    public double calcArea() 
{ 
    return Math.PI*Math.pow(getRadius(),2); 
} 
/** 
* 
*/ 
public double distance(double x1, double y1, double x2, double y2) 
{ 
    return (Math.sqrt(Math.pow(x1-x2,2)+ Math.pow(y1-y2,2))) ; 

} 
/** 
* 
*/ 
public boolean contains(double x, double y) 
{ 
    double dist = distance(x, y, centerX, centerY); // calculate the distance between the point(x,y) and center of the circle 
    boolean flag = false; 
    if (dist < radius) 
    { 
     flag = true; 

    } 
    else if (dist > radius) 
    { 
     flag = false ; 
    } 
    else 
    { 
     System.out.println("The point is on the circle."); 
    } 
    return flag; 
} 

} 
/** 
* 
*/ 
public String toString() 
{ 
    String outString = "The radius of the circle is: " + getRadius() + "\nThe perimeter of the circle object is; " + calcPerimeter()+ "\nThe area of the circle is: " + calcArea() ; 
    return outString; 
} 

}

+1

一个圈子内互相如果之间的距离中心小于或等于两个半径之间的绝对差值。如果两个中心之间的距离小于两个半径之和,则两个圆重叠。使用毕达哥拉斯定理(x^2 + y^2 = z^2)很容易找到中心之间的距离。 – MT0

+0

我知道规则找到它即时寻找如何做一个方法与输入是一个对象,然后之前,我让我的if语句如果没有理解如何将有两个半径,如果只有一个对象。我的老师说方法中有一个循环对象。如果只有一个对象,那么我在哪里得到第二个半径。 – WatsDavies

回答

1
import java.awt.geom.Point2D; 

/** 
* 
* @author MT0 
*/ 
public class Circle { 
    private Point2D.Double center; 
    private double radius; 

    public Circle(
      final double x, 
      final double y, 
      final double r 
    ){ 
     assert(r >= 0); 
     center = new Point2D.Double(x, y); 
     radius = r; 
    } 

    public Point2D.Double getCenter(){ 
     return center; 
    } 

    public double getRadius(){ 
     return radius; 
    } 

    public Double distance(final Point2D.Double point){ 
     if (point == null) 
      return null; 
     return getCenter().distance(point); 
    } 

    public boolean contains(final Point2D.Double point){ 
     if (point == null) 
      return false; 
     return distance(point) <= getRadius(); 
    } 

    public boolean overlaps(final Circle c){ 
     return distance(c.getCenter()) <= getRadius() + c.getRadius(); 
    } 

    public boolean contains(final Circle c){ 
     if (c == null) 
      return false; 
     return distance(c.getCenter()) <= Math.abs(getRadius() - c.getRadius()); 
    } 
} 
+0

谢谢这样做以不同的方式,但我想我可以弄清楚如何使用这个谢谢 – WatsDavies

+0

即时通讯编写一个测试类,看看这是否适用于圆圈 – WatsDavies

+0

为什么当我使用时,它会给我补偿错误c1.contains()如果我想使用该方法 – WatsDavies