這是我在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;
}
}
一個圈子內互相如果之間的距離中心小於或等於兩個半徑之間的絕對差值。如果兩個中心之間的距離小於兩個半徑之和,則兩個圓重疊。使用畢達哥拉斯定理(x^2 + y^2 = z^2)很容易找到中心之間的距離。 – MT0
我知道規則找到它即時尋找如何做一個方法與輸入是一個對象,然後之前,我讓我的if語句如果沒有理解如何將有兩個半徑,如果只有一個對象。我的老師說方法中有一個循環對象。如果只有一個對象,那麼我在哪裏得到第二個半徑。 – WatsDavies