计算三角形的面积公式是:
下面是一个运算示例:
Enter three points for a tringle: 1.5 -3.4 4.6 5 9.5 -3.4
The area of the triangle is 33.6
package Second;
import java.util.Scanner;
public class Code_19 {
public static void main(String[] args) {
System.out.println("Enter three points for a tringle: ");
Scanner input = new Scanner(System.in);
double x1 = input.nextDouble();
double y1 = input.nextDouble();
double x2 = input.nextDouble();
double y2 = input.nextDouble();
double x3 = input.nextDouble();
double y3 = input.nextDouble();
double side01 = Math.abs(Math.pow(x1-x2, 2)) + Math.abs(Math.pow(y1-y2, 2));
double side1 = Math.pow(side01,0.5);
double side02 = Math.abs(Math.pow(x3-x2, 2)) + Math.abs(Math.pow(y3-y2, 2));
double side2 = Math.pow(side01,0.5);
double side03 = Math.abs(Math.pow(x3-x1, 2)) + Math.abs(Math.pow(y3-y1, 2));
double side3 = Math.pow(side03,0.5);
double S = (side1 + side2 +side3) / 2;
double area1 = S * (S - side1) * (S - side2) * (S - side3);
double area = Math.pow(area1,0.5);
System.out.println("The area of the triangle is " + area);
}
}
输出
Enter three points for a tringle: 1.5 -3.4 4.5 5 9.5 -3.4 The area of the triangle is 31.889810284791597