分支-02. 三角形判断

给定平面上任意三个点的坐标(x1,y1)、(x2,y2)、(x3,y3),检验它们能否构成三角形。

输入格式:输入在一行中顺序给出6个[-100, 100]范围内的数字,即3个点的坐标x1, y1, x2, y2, x3, y3。

输出格式:若这3个点不能构成三角形,则在一行中输出“Impossible”;若可以,则在一行中输出该三角形的周长和面积,格式为“L = 周长, A = 面积”,输出到小数点后2位。

输入样例1:4 5 6 9 7 8
输出样例1:L = 10.13, A = 3.00
输入样例2:4 6 8 12 12 18
输出样例2:Impossible
/*借鉴别人的,提交的时候不要有任何中文,注释也不行。*/
import
java.io.BufferedReader; import java.io.InputStreamReader; import java.text.DecimalFormat; public class Main { static class Point {
     /*点坐标*/
float x; float y; } private static boolean checkPoint(Point p1)/*检查点的范围*/ {
if(p1.x >= -100 && p1.x <= 100 && p1.y >= -100 && p1.y<=100) return true; else return false; } private static double getLength(Point p1,Point p2)/*获取两点距离,即边长*/ { return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.y - p2.y, 2)); } public static void main(String[] args) { Point p1 = new Point(); Point p2 = new Point(); Point p3 = new Point(); while (true) { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try { String line = reader.readLine(); boolean result = false; String space = " "; if(line != null && line.contains(space)) { String[] lines = line.split(space); if(line != null &&lines.length == 6) { p1.x = (Float.parseFloat(lines[0])); p1.y = (Float.parseFloat(lines[1])); p2.x = (Float.parseFloat(lines[2])); p2.y = (Float.parseFloat(lines[3])); p3.x = (Float.parseFloat(lines[4])); p3.y = (Float.parseFloat(lines[5])); if(checkPoint(p1)&&checkPoint(p2)&&checkPoint(p3))/*判断三点是否符合限定*/ { result = true; } } } if(result)/*三点符合限定,即result=true,则whlie到此停止,跳出whlie*/ { break; } else { System.out.println("Input error!Please try again..."); continue;/*输入有误,程序回到whlie继续判断,重新输入*/ } } catch (Exception e)
          {
            // TODO: handle exception
           
}//try }//whlie
double l1 = getLength(p1,p2); double l2 = getLength(p1,p3); double l3 = getLength(p2,p3); if(l1+l2<=l3 || l1+l3<=l2 || l2+l3<=l1)/*判断三点可否构成三角形*/ { System.out.println("Impossible"); } else { String L; DecimalFormat df = new DecimalFormat("##0.00");/*定义输出格式*/ L = df.format(l1+l2+l3);/*周长*/ float A = (float) (0.5*(p1.x*p2.y+p2.x*p3.y+p3.x*p1.y-p1.x*p3.y-p2.x*p1.y-p3.x*p2.y));/*坐标求面积*/ System.out.println("L = " + L + ", " + "A = " + df.format(Math.abs(A))); /*Math.abs:绝对值*/ } } }

 


posted @ 2014-09-12 11:47  winTeaer  阅读(434)  评论(0编辑  收藏  举报