查找3点外心的Handel边事例。[JAVA]
所以我做了一些代数,找到了一个公式,可以找到3点的外心。我也编写了代码,除了斜率为0的一些边缘情况外,它对许多输入都能顺利工作。所以我的问题是如何更新代码来处理一些边缘情况。这是我的密码查找3点外心的Handel边事例。[JAVA],java,Java,所以我做了一些代数,找到了一个公式,可以找到3点的外心。我也编写了代码,除了斜率为0的一些边缘情况外,它对许多输入都能顺利工作。所以我的问题是如何更新代码来处理一些边缘情况。这是我的密码 import java.util.*; class CircleTest { public static void main(String args[]) { int x[]; int y[]; int n; Scanner sc=new Scanner
import java.util.*;
class CircleTest
{
public static void main(String args[])
{
int x[];
int y[];
int n;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the value of n");
n=sc.nextInt();// assume n is 3 ,made this for some other reason
x=new int[n];
y=new int[n];
for(int i=0;i<n;i++)
{
x[i]=sc.nextInt();//taking 3 points as input
y[i]=sc.nextInt();
}
/*
* Assume P=(x[0],y[0])
* Q=(x[1],y[1])
* R=(x[2],y[2])
*/
double ymidPQ=(y[0]+y[1])/2.0;// y corr of mid point of PQ
double xmidPQ=(x[0]+x[1])/2.0;// x corr. of mid point of PQ
double slopePQ=(y[1]-y[0])/(x[1]-x[0]);// slope of line PQ
double ymidQR=(y[1]+y[2])/2.0;
double xmidQR=(x[1]+x[2])/2.0;
double slopeQR=(y[2]-y[1])/(x[2]-x[1]);
double centerY= ( (ymidPQ * slopePQ + xmidPQ) + (ymidQR * slopeQR + xmidQR) )/(slopePQ-slopeQR);//y corr. of circumcenter
double centerX= (ymidPQ * slopePQ+ xmidPQ ) -centerY*slopePQ;// x corr of circum center
System.out.println(centerX+","+centerY);
}
}
import java.util.*;
类循环测试
{
公共静态void main(字符串参数[])
{
int x[];
int y[];
int n;
扫描仪sc=新的扫描仪(System.in);
系统输出打印(“输入n的值”);
n=sc.nextInt();//假设n是3,这是出于其他原因
x=新整数[n];
y=新整数[n];
对于(int i=0;i哈哈,我不认为这可以用这么简单的方法解决,我刚刚解决了它
import java.util.*;
class CircleTest
{
public static void main(String args[])
{
int x[];
int y[];
int n;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the value of n");
n=sc.nextInt();// assume n is 3 ,made this for some other reason
x=new int[n];
y=new int[n];
for(int i=0;i<n;i++)
{
x[i]=sc.nextInt();//taking 3 points as input
y[i]=sc.nextInt();
}
/*
* Assume P=(x[0],y[0])
* Q=(x[1],y[1])
* R=(x[2],y[2])
*/
double ymidPQ=(y[0]+y[1])/2.0;// y corr of mid point of PQ
double xmidPQ=(x[0]+x[1])/2.0;// x corr. of mid point of PQ
double slopePQ;
try{
slopePQ=(y[1]-y[0])/(x[1]-x[0]);// slope of line PQ
}
catch(Exception e)
{
slopePQ=Double.MAX_VALUE;
}
double ymidQR=(y[1]+y[2])/2.0;
double xmidQR=(x[1]+x[2])/2.0;
double slopeQR;
try{
slopeQR=(y[2]-y[1])/(x[2]-x[1]);
}
catch(Exception e)
{
slopeQR=Double.MAX_VALUE;
}
double centerY= ( (ymidPQ * slopePQ + xmidPQ) + (ymidQR * slopeQR + xmidQR) )/(slopePQ-slopeQR);//y corr. of circumcenter
double centerX= (ymidPQ * slopePQ+ xmidPQ ) -centerY*slopePQ;// x corr of circum center
System.out.println(centerX+","+centerY);
}
}
import java.util.*;
类循环测试
{
公共静态void main(字符串参数[])
{
int x[];
int y[];
int n;
扫描仪sc=新的扫描仪(System.in);
系统输出打印(“输入n的值”);
n=sc.nextInt();//假设n是3,这是出于其他原因
x=新整数[n];
y=新整数[n];
对于(int i=0;iDid您尝试使用?只有在不存在外心的情况下(或在外心距三点很远的情况下发生溢出时),才会导致错误)。您可以通过将计算移到一个方法中,并从问题中删除输入处理,使此代码更加简单。此外,您还应该明确使用的公式以及它是如何失败的。这是如何回答问题的?请详细说明为什么这是一个答案。斜率为tan(θ),因为θ趋向于90斜率tan(θ)倾向于无穷大,或者说是一个大的数字,这就是我所做的,如果斜率平行于y轴,也就是θ是90,我给了斜率一个大的值,也就是Double.MAX_值@Polygnome@Polygnome然后,我根据原始输入(0,0)(5,0)和(0,5)测试了我的程序,它返回(2.5,2.5).然后描述什么是不同的。发生了什么变化?详细说明。这个答案目前只是一个代码转储。