1

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

  • 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 example](https://stackoverflow.com/help/mcve) – SergGr Dec 06 '18 at 23:12

2 Answers2

4

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.

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Good guess for such a question. – SergGr Dec 06 '18 at 23:15
  • 1
    @SergGr It's a mistake I've made more then once – MadProgrammer Dec 06 '18 at 23:15
  • That doesnt actually seem to affect the output. In fact no matter what double value I put in the parameters for cos and sin, the resulting line is still as long as in the first picture :( – Michael Tracey Dec 06 '18 at 23:26
  • 1
    @MichaelTracey [Runnable example](https://stackoverflow.com/help/mcve) every thing else is guess work. You could also have a look at something like [this](https://stackoverflow.com/questions/47369565/connect-two-circles-with-a-line/47371140#47371140) or [this](https://stackoverflow.com/questions/25923480/simple-circle-rotation-simulate-motion/25923780#25923780) or [this](https://stackoverflow.com/questions/30228146/drawing-a-line-maximum-point/30228270#30228270) – MadProgrammer Dec 06 '18 at 23:30
  • @MadProgrammer you were right, it was something else where in my implementation that was bugging out :) thanks – Michael Tracey Dec 06 '18 at 23:32
1

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.

SergGr
  • 23,570
  • 2
  • 30
  • 51