是否为三角形
Posted 王六六同学
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了是否为三角形相关的知识,希望对你有一定的参考价值。
三维空间中三个坐标判断是否可以组成三角形
输入:字符串(x1,y1,z1) (x2,y2,z2) (x3,y3,z3)
public class Main
public boolean isTriangle(String str)
if(str == null || str.equals(""))
return false;
String[] str_num = str.split(" ");
String A = str_num[0];
String B = str_num[1];
String C = str_num[2];
String A1 = A.substring(1,A.length()-1);
String B1 = B.substring(1,B.length()-1);
String C1 = A.substring(1,C.length()-1);
String[] A1_nums = A1.split(","); //(x1,y1,z1)
String[] B1_nums = B1.split(","); //(x2,y2,z2)
String[] C1_nums = C1.split(","); //(x3,y3,z3)
//斜率
double slope1 = (Double.parseDouble(A1_nums[1]) - Double.parseDouble(B1_nums[1])) / (Double.parseDouble(A1_nums[0]) - Double.parseDouble(B1_nums[0]));
double slope2 = (Double.parseDouble(A1_nums[1]) - Double.parseDouble(C1_nums[1])) / (Double.parseDouble(A1_nums[0]) - Double.parseDouble(C1_nums[0]));
double slope3 = (Double.parseDouble(B1_nums[1]) - Double.parseDouble(C1_nums[1])) / (Double.parseDouble(B1_nums[0]) - Double.parseDouble(C1_nums[0]));
//两点间距离
double A_B = Math.pow(
Math.pow((Double.parseDouble(A1_nums[0])-Double.parseDouble(B1_nums[0])),2)+
Math.pow((Double.parseDouble(A1_nums[1])-Double.parseDouble(B1_nums[1])),2)+
Math.pow((Double.parseDouble(A1_nums[2])-Double.parseDouble(B1_nums[2])),2),1/2);
double A_C = Math.pow(
Math.pow((Double.parseDouble(A1_nums[0])-Double.parseDouble(C1_nums[0])),2)+
Math.pow((Double.parseDouble(A1_nums[1])-Double.parseDouble(C1_nums[1])),2)+
Math.pow((Double.parseDouble(A1_nums[2])-Double.parseDouble(C1_nums[2])),2),1/2);
double B_C = Math.pow(
Math.pow((Double.parseDouble(B1_nums[0])-Double.parseDouble(C1_nums[0])),2)+
Math.pow((Double.parseDouble(B1_nums[1])-Double.parseDouble(C1_nums[1])),2)+
Math.pow((Double.parseDouble(B1_nums[2])-Double.parseDouble(C1_nums[2])),2),1/2);
if(isParallel(slope1,slope2,slope3))
return false;
if( (A_B + A_C) > B_C && (A_B + B_C) > A_B && (A_B + B_C) > A_C && Math.abs(A_B - A_C) < B_C && Math.abs(A_B-B_C) < A_C && Math.abs(A_C -B_C) < A_B)
return true;
else
return false;
// String[] s1 = str.substring(1,6).split(","); //(x1,y1,z1)
// String[] s2 = str.substring(8,13).split(",");//(x2,y2,z2)
// String[] s3 = str.substring(15,20).split(",");//(x3,y3,z3)
public boolean isParallel(double a, double b, double c)
if(a == b || a == c || b == c)
return true;
else
return false;
以上是关于是否为三角形的主要内容,如果未能解决你的问题,请参考以下文章