三角形角度公式:
从以上公式中我们可以看出,要计算角度,就必须知道边长,那么我们该如何计算边长呢?
首先我们需要知道三个顶点的坐标,然后计算点与点之间的距离即可。
代码:
package com.zhuo.base.com.zhuo.base;import java.util.scanner;public class computeangles { public static void main(string[] args) { scanner input = new scanner(system.in); //提示用户输入三个点 system.out.print("enter three points:"); 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 a = math.sqrt((x2 - x3) * (x2 - x3) + (y2 - y3) * (y2- y3)); double b = math.sqrt((x1 - x3) * (x1 - x3) + (y1 - y3) * (y1 - y3)); double c = math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); //计算三个角 double a = math.todegrees(math.acos((a * a - b * b - c * c) / (-2 * b * c))); double b = math.todegrees(math.acos((b * b - a * a - c * c) / (-2 * a * c))); double c = math.todegrees(math.acos((c * c - a * a - b * b) / (-2 * a * b))); //显示结果,保留小数点后两位 system.out.println("the three angles are " + math.round(a * 100) / 100.0 + " " + math.round(b * 100) / 100.0 + " " + math.round(c * 100) / 100.0); }}
结果显示:
相关推荐:java入门教程
以上就是利用java解决三角形角度问题的详细内容。