简体   繁体   English

Java在播放的圆上找到Point

[英]Java finding Point on a circle playing up

I have the following equation to find a point on a circle: 我有以下方程式可以找到圆上的点:

x = (int) (10 * Math.cos(45.0));
y = (int) (10 * Math.sin(45.0));
x1 = new Point(x, y);

I then draw a Line from the center of the circle to this new point. 然后,我从圆心到新点画一条线。

I would have thought that changing the parameters of the Math.cos and Math.sin functions would change the angle at which the line comes out from the center, but when I test this, it is the radius of the circle that, if changed, changes the angle at which the line is drawn at. 我本以为,更改Math.cos和Math.sin函数的参数会更改直线从中心出来的角度,但是当我测试此角度时,如果改变的是圆的半径,更改画线的角度。

Am I misunderstanding the math? 我误会数学了吗? What is going wrong here? 这是怎么了?

This is the line drawn from the center circle with the above equation, though it should only be as long as the radius of that center circle 这是使用上述公式从中心圆绘制的线,尽管它的长度应仅等于该中心圆的半径

在此处输入图片说明

This is the resulting image when the equation is: 当方程为时,这是结果图像:

x = (int) (350 * Math.cos(45.0));
y = (int) (350 * Math.sin(45.0));
x1 = new Point(x, y);    

在此处输入图片说明

Math.cos and Math.sin are documented as accepting radians, not degrees Math.cosMath.sin被记录为接受弧度,而不是度数

Use Math.toRadians and Math. toDegrees 使用Math.toRadiansMath. toDegrees Math. toDegrees to convert between them Math. toDegrees表示在它们之间进行转换

cos cos

public static double cos​(double a) 公共静态double cos(double a)

Returns the trigonometric cosine of an angle. 返回角度的三角余弦。 Special cases: 特别案例:
* If the argument is NaN or an infinity, then the result is NaN. *如果参数为NaN或无穷大,则结果为NaN。

The computed result must be within 1 ulp of the exact result. 计算结果必须在准确结果的1 ulp以内。 Results must be semi-monotonic. 结果必须是半单调的。

Parameters: 参数:
a - an angle, in radians. a-弧度角。
Returns: 返回值:
the cosine of the argument. 论证的余弦。

It seems you actually have more than one error in your code. 看来您的代码中实际上有多个错误。 It would really help if you showed as the full code of a Minimal, Complete, and Verifiable example . 如果您以最小,完整和可验证示例的完整代码显示,那将真的有帮助。 Still here is some guesses: 仍然有一些猜测:

  1. Assuming xc and yc are the variables with values of the center of the circle and R is the radius, the point on the circle at the angle alpha is 假设xcyc是具有圆心的值的变量, R是半径,则圆上角度为alpha

    x = R * Math.cos(alpha) + xc; x = R * Math.cos(alpha)+ xc; y = R * Math.sin(alpha) + yc; y = R * Math.sin(alpha)+ yc;

In your code it looks like your xc and yc are both 0 so you effectively draw a line from the center of the circle to a point on a circle with the center (0,0) which in Java 2d world is the top left corner of the screen. 在您的代码中,您的xcyc看起来都为0因此您可以有效地从圆心到中心为(0,0)的圆上的一点画一条线,这在Java 2d世界中是屏幕。

  1. As MadProgrammer pointed out Math.cos and Math.sin take arguments in radians and the value of 45.0 suggests you use degrees. 正如MadProgrammer指出的那样, Math.cosMath.sin接受以弧度表示的参数,并且45.0的值建议您使用度。 Using Math.toRadians will probably fix the issue. 使用Math.toRadians可能会解决此问题。

There might be more issues but it is hard to guess with current state of the question. 可能会有更多问题,但是很难根据问题的当前状态来猜测。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM