1
votes

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.

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

enter image description here

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);    

enter image description here

2
Your description is not really clear. Probably a few screenshots for various parameters would improve the question a lot. Also show us how the code that draws the line. See also How to create a Minimal, Complete, and Verifiable exampleSergGr

2 Answers

4
votes

Math.cos and Math.sin are documented as accepting radians, not degrees

Use Math.toRadians and Math. toDegrees to convert between them

cos

public static 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.

The computed result must be within 1 ulp of the exact result. Results must be semi-monotonic.

Parameters:
    a - an angle, in radians.
Returns:
    the cosine of the argument.

1
votes

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

    x = R * Math.cos(alpha) + xc; 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.

  1. As MadProgrammer pointed out Math.cos and Math.sin take arguments in radians and the value of 45.0 suggests you use degrees. Using Math.toRadians will probably fix the issue.

There might be more issues but it is hard to guess with current state of the question.