Java
程序员文章站
2024-02-28 08:38:04
...
Java:在一个已知原点半径的圆上随机产生三个点构成三角形,求三角形的三个角度。
public class RandomPointsOnACircle {
public static void main(String[] args) {
// TODO Auto-generated method stub
double x0 = 0;
double y0 = 0;
final int RADIUS = 40;
double angleInRadians1 = Math.random() * Math.PI * 2;
double angleInRadians2 = Math.random() * Math.PI * 2;
double angleInRadians3 = Math.random() * Math.PI * 2;
int x1 = (int) (x0 + RADIUS * Math.cos(angleInRadians1));
int y1 = (int) (y0 + RADIUS * Math.sin(angleInRadians1));
int x2 = (int) (x0 + RADIUS * Math.cos(angleInRadians2));
int y2 = (int) (y0 + RADIUS * Math.sin(angleInRadians2));
int x3 = (int) (x0 + RADIUS * Math.cos(angleInRadians3));
int y3 = (int) (x0 + RADIUS * Math.sin(angleInRadians3));
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));
System.out.println(a);
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
下一篇: Python学习小技巧之列表项的拼接