Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 估计形状之间的面积_Java_Shapes_Area - Fatal编程技术网

Java 估计形状之间的面积

Java 估计形状之间的面积,java,shapes,area,Java,Shapes,Area,在我的课堂上,我们的任务如下: 对于本作业,您将尝试制作一个程序,用于计算一组形状的重叠区域,实现蒙特卡罗积分,如课堂上所讨论的。 Make AreaEstimator.java,一个使用随机估计来估计任意数量的圆和三角形的重叠区域的程序。该程序的参数将是在本次试验中使用的随机生成的点的数量,然后是定义形状的点的坐标。比如说, java AreaEstimator 1000000圆圈2.0 2.0 1.0三角形1.0 1.0 2.5 3.0 2.0-3.0圆圈2.5 1.0 3.0 将生成一百万

在我的课堂上,我们的任务如下:

对于本作业,您将尝试制作一个程序,用于计算一组形状的重叠区域,实现蒙特卡罗积分,如课堂上所讨论的。 Make AreaEstimator.java,一个使用随机估计来估计任意数量的圆和三角形的重叠区域的程序。该程序的参数将是在本次试验中使用的随机生成的点的数量,然后是定义形状的点的坐标。比如说,

java AreaEstimator 1000000圆圈2.0 2.0 1.0三角形1.0 1.0 2.5 3.0 2.0-3.0圆圈2.5 1.0 3.0

将生成一百万个随机点,以估计顶点为(1.0,1.0)、(2.5,3.0)、(2.0,-3.0)的三角形与两个圆心分别位于(2.0,2.0)和(2.5,1.0)且半径分别为1.0和3.0的圆的重叠

以下是我的Circle类代码:

public class Circle {
private double xcenter;
private double ycenter;
private double radius;
private double xcmax;
private double xcmin;
private double ycmax;
private double ycmin;


public Circle ( double xcenter, double ycenter, double radius){
    if(radius <= 0){
        throw new IllegalArgumentException();
    }
    this.xcenter = xcenter;
    this.ycenter = ycenter;
    this.radius = radius;

}
public void maxAndMinCircle (){ //find the minimun and maximum for the plane according to this circle
    xcmax = (this.xcenter + this.radius);
    ycmax = (this.ycenter + this.radius);
    xcmin = (this.xcenter - this.radius);
    ycmin = (this.ycenter - this.radius);

}
public double getXCMax (){
    return xcmax;
}
public double getYCMax () {
    return ycmax;
}
public double getXCMin() {
    return xcmin;
}
public double getYCMin(){
    return ycmin;
}

public boolean outsideCircle(double randX, double randY){ // find if the random point passed thru is in this circle
    double distance = Math.sqrt((randX-this.xcenter)*(randX-this.xcenter) + (randY-this.ycenter) * (randY - this.ycenter));
    return distance >= radius;  
}}
公共类圈子{
私人双xcenter;
私人双中心;
私人双半径;
专用双xcmax;
私有双xcmin;
私人双ycmax;
私人双ycmin;
公共圆(双圆心、双圆心、双半径){
如果(半径=半径);
}}
三角形类:

public class Triangle {
private double cornerx1;
private double cornery1;
private double cornerx2;
private double cornery2;
private double cornerx3;
private double cornery3;
private double xtmax;
private double xtmin;
private double ytmax;
private double ytmin;
private Double[] corners;

public Triangle (double cornerx1, double cornery1, double cornerx2, double cornery2, double cornerx3, double cornery3){
    corners = new Double [6];
    corners[0] = cornerx1;
    corners[1] = cornery1;
    corners[2] = cornerx2;
    corners[3] = cornery2;
    corners[4] = cornerx3;
    corners[5] = cornery3;
}
public void maxAndMinTriangle (){ // find the minimum and maximum of the plane according to this triangle
    xtmax = corners[0];  
    for(int i=1;i < corners.length;i += 2){  
        if(corners[i] > xtmax){  
            xtmax = corners[i];
        }
    }

    xtmin = corners[0];  
    for(int i=1;i < corners.length;i += 2){  
        if(corners[i] < xtmin){  
            xtmin = corners[i];
        }
    }

    ytmax = corners[1];  
    for(int i=1;i < corners.length;i += 2){  
        if(corners[i] > ytmax){  
            ytmax = corners[i];
        }
    }

    ytmin = corners[1];  
    for(int i=1;i < corners.length;i += 2){  
        if(corners[i] < ytmin){  
            ytmin = corners[i];
        }
    }
}
public double getXTMax (){
    return xtmax;
}

public double getYTMax () {
    return ytmax;
}
public double getXTMin() {
    return xtmin;
}
public double getYTMin(){
    return ytmin;
}


//public static boolean isLeft (double x1, double y1, double x2, double y2, double x3, double y3){
    //return ( 0 <= ( ( x2 - x1 ) * ( y - y1) )- (( y2 - y1) * ( x - x1)));
//}

public boolean isLeft1 (double randX, double randY){ // find if this point is to the left of the first line
    return ( 0 <= ( ( corners[2] - corners[0] ) * ( randY - corners[1]) )- (( corners[3] - corners[1]) * ( randX - corners[0])));
}
public boolean isLeft2 (double randX, double randY){ // find if this point is to the left of the second line
    return ( 0 <= ( ( corners[4] - corners[0]) * ( randY - corners[1]) )- (( corners[5] - corners[1]) * ( randX - corners[0])));
}
public boolean isLeft3 (double randX, double randY){ // find if this point is to the left of the third line
    return ( 0 <= ( ( corners[4] - corners[2] ) * ( randY - corners[3]) )- (( corners[5] - corners[3]) * ( randX - corners[2])));
}

public boolean outsideTriangle ( double randX, double randY ){ // find if this point is inside of the triangle
    int counter = 0;
    if (isLeft1(randX,randY)){
        counter++;
    }
    if (isLeft2(randX,randY)){
        counter++;
    }
    if (isLeft3(randX,randY)){
        counter++;
    }
    return counter == 2; // must be to the left of exactly 2 of the lines
}}
公共类三角形{
私人双转角x1;
私人双角1;
私人双拐角x2;
私人双角2;
私人双角x3;
私人垄断3;
私人双xtmax;
私有双xtmin;
私人双ytmax;
私人双ytmin;
私人双[]角;
公共三角形(双拐角X1、双拐角Y1、双拐角X2、双拐角Y2、双拐角X3、双拐角Y3){
角落=新的双[6];
拐角[0]=拐角x1;
拐角[1]=拐角1;
拐角[2]=拐角x2;
拐角[3]=拐角2;
拐角[4]=拐角X3;
拐角[5]=拐角3;
}
public void maxAndMinTriangle(){//根据此三角形查找平面的最小值和最大值
xtmax=角点[0];
对于(inti=1;ixtmax){
xtmax=角点[i];
}
}
xtmin=角点[0];
对于(inti=1;iytmax){
ytmax=转角[i];
}
}
ytmin=转角[1];
对于(inti=1;i如果(numThrowsmaxAndMinTriangle中至少有一个打字错误。您在需要一个0的地方写了一个1

xtmax = corners[0];  
for(int i=1;i < corners.length;i += 2){  
...
xtmin = corners[0];  
for(int i=1;i < corners.length;i += 2){  
xtmax=corners[0];
对于(inti=1;i
应该是

xtmax = corners[0];  
for(int i=0;i < corners.length;i += 2){  
...
xtmin = corners[0];  
for(int i=0;i < corners.length;i += 2){  
xtmax=corners[0];
对于(int i=0;i
xtmax = corners[0];  
for(int i=0;i < corners.length;i += 2){  
...
xtmin = corners[0];  
for(int i=0;i < corners.length;i += 2){