views:

427

answers:

4

Assuming the upper left corner is (0,0) and I'm given an angle of 30 degrees, a starting point of (0,300), a line length of 600, how do I calculate the ending point of the line so that the line is representative of the angle given.

The C pseudo-code is

main() {
  int x,y;

  getEndPoint(30, 600, 0, 300, &x, &y);
  printf("end x=%d, end y=%d", x, y);
}

// input angle can be from 0 - 90 degrees

void getEndPoint(int angle, int len, int start_x, int start_y, int *end_x, int *end_y) 
{

    calculate the endpoint here for angle and length

    *end_x = calculated_end_x;
    *end_y = calculated_end_y;
}
+1  A: 

math.h has all the trigonometric functions you should need. You may need to give -lm to your linker, depending on what system you're building on (sometimes it's automatic).

Carl Norum
+5  A: 
// edit to add conversion
    #define radian2degree(a) (a * 57.295779513082)
    #define degree2radian(a) (a * 0.017453292519)

        x = start_x + len * cos(angle);
        y = start_y + len * sin(angle);
Nick
`cos()` and `sin()` take radians, be careful.
Carl Norum
radians = pi * degrees/180
Brian
That worked perfectly... sometimes I make things too hard. Thanks much!
amanda
-1 for macros... please use inlined functions! There is going to be a conversion at one point for the multiplication to occur anyway....
Matthieu M.
-1!? What if you don't have a C99 compiler?
Carl Norum
Heck, even if you do have a C99 compiler, making an inline function is only a suggestion. Macros are guaranteed to be the real deal.
Carl Norum
This question was about trig not macros vs. inline functions. I was simply pointing out that degree to radian conversion is trivial.
Nick
+3  A: 

Is it the maths or the code you are having trouble with? If it is the maths, then this is probably not the forum, and it is pretty elementary; I refer you to Polar -Rectangular Conversion. Remember however that when you implement it the C math library uses radians rather than degrees r = d * PI / 180.0

Clifford
Man, I got downvoted for answering the programming half. Oh how fickle...
Carl Norum
The coding is simple, I have little background in math however, sorry if it's the wrong place to ask.
amanda
@Carl: Not by me.@avio: I have no problem with the question, but it is a good idea to separate domain knowledge from programming knowledge. Searching maths related sites rather than programming sites would be better for donaim knowledge in this case. That said knowing the terms "polar coordinate" and "rectangular coordinate" obviously helps in targetting a search.
Clifford
+1  A: 

You don't say what the angle is measured relative to, or indeed what direction your axes go. These will make a difference.

You first need to convert from degrees to radians (multiply by PI and divide by 180). Then you need to take the sine and the cosine of your angle and multiply these by the length of the line. You now have two numbers for your coordinates, but it depends what directions your axes go and from where you're measuring your angles which of these values is the x coordinate and which is the y, and whether either of them needs to be negated.

Weeble