java 三点求角度,计算两点之间的角度-Java

这篇博客提供了一个方法来计算两个点与固定点连线所成角度,使用Java编程实现。通过Math.atan2方法计算角度,并将其从弧度转换为度数。示例代码中展示了如何获取两个点与第三个固定点之间的角度差。

I need to calculate the angle in degrees between two points, with a fixed point that is connected with the given two points by a line.

Here is an image that illustrates what I need:

JVPIq.png

Here is what I have tried so far:

public static float GetAngleOfLineBetweenTwoPoints(float x1, float x2, float y1, float y2) {

float xDiff = x2 - x1;

float yDiff = y2 - y1;

return (float) (Math.atan2(yDiff, xDiff) * (180 / Math.PI));

}

It's pointless to say that it doesn't provide the correct answer.

解决方案

You can have the following method that calculates the angle in radians using the Math.atan2 method:

public static double angleBetweenTwoPointsWithFixedPoint(double point1X, double point1Y,

double point2X, double point2Y,

double fixedX, double fixedY) {

double angle1 = Math.atan2(point1Y - fixedY, point1X - fixedX);

double angle2 = Math.atan2(point2Y - fixedY, point2X - fixedX);

return angle1 - angle2;

}

And call it with three points (using Math.toDregrees to transform resulting angle from radians to degrees):

System.out.println(Math.toDegrees(

angleBetweenTwoPointsWithFixedPoint(0, 0, // point 1's x and y

1, 1, // point 2

1, 0 // fixed point

)));

Output: 90.0

Feel free to use Java's standard Point or Line2D classes in your solution though. This was just to demonstrate it works.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值