Javaを使って、3点の座標で三角形の面積を求めます

1182 ワード

プログラムを作成し、ユーザに三角形の3つの点(x 1,y 1)、(x 2,y 2)、(x 3,y 3)を入力し、その面積を表示するように促す.
	import java.util.Scanner;
	public class Home04 {
			public static void main(String[] args) {
			Scanner scanner = new Scanner(System.in);
			System.out.println("    1   :");
			double x1 = scanner.nextDouble();
			double y1 = scanner.nextDouble();
			System.out.println("    2   :");
			double x2 = scanner.nextDouble();
			double y2 = scanner.nextDouble();
			System.out.println("    3   :");
			double x3 = scanner.nextDouble();
			double y3 = scanner.nextDouble();
			
			/*
			 * 	  A(x1,y1),B(x2,y2)
			  AB      √[(X1-X2)^2+(Y1-Y2)^2]
			 * */
	
			/*
			 *  1  2   
			 *       :Math.sqrt(double s); 
			 * */
			double first = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); 
		
			/*
			 *  2  3   
			 * */
			double second =  Math.sqrt((x2-x3)*(x2-x3)+(y2-y3)*(y2-y3));
		
			/*
			 *  3  1   
			 * */				
			double third = Math.sqrt((x3-x1)*(x3-x1)+(y3-y1)*(y3-y1));
			 
			/*
			 *        
			 * */
			double s = (first+second+third)/2;
			double area = Math.sqrt(s*(s-first)*(s-second)*(s-third));
			System.out.println("area"+area);
			}
	}