20

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;
}
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
amanda
  • 601
  • 2
  • 8
  • 14
  • i assume you want at type of (int *) for end_x and end_y, as well as flipping your assignments around at the end of your function. – Ron Warholic Oct 28 '09 at 16:41
  • Thanks, I wanted the calculated values. Edited to reflect that. – amanda Oct 28 '09 at 16:53
  • He means your function signature is bad. I'll fix it. – Carl Norum Oct 28 '09 at 16:54
  • 2
    How long before Spolsky and Atwood set up mathoverflow.com? – Michael Burr Oct 28 '09 at 17:17
  • They'd just adopt mathoverflow.net – GManNickG Oct 28 '09 at 17:50
  • 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](http://www.teacherschoice.com.au/Maths_Library/Coordinates/polar_-_rectangular_conversion.htm). Remember however that when you implement it the C math library uses radians rather than degrees `r = d * PI / 180.0` – Clifford Oct 28 '09 at 16:44
  • Man, I got downvoted for answering the programming half. Oh how fickle... – Carl Norum Oct 28 '09 at 16:47
  • The coding is simple, I have little background in math however, sorry if it's the wrong place to ask. – amanda Oct 28 '09 at 16:58

5 Answers5

41
// 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 Van Brunt
  • 15,244
  • 11
  • 66
  • 92
  • 14
    `cos()` and `sin()` take radians, be careful. – Carl Norum Oct 28 '09 at 16:40
  • That worked perfectly... sometimes I make things too hard. Thanks much! – amanda Oct 28 '09 at 16:52
  • -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. Oct 28 '09 at 18:57
  • 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 Oct 28 '09 at 20:59
  • 5
    This question was about trig not macros vs. inline functions. I was simply pointing out that degree to radian conversion is trivial. – Nick Van Brunt Oct 29 '09 at 13:47
  • I would just remove the macros and create the variable names and values. Probably get more points out of it in the long run. – marksyzm Feb 06 '14 at 15:18
2

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
  • 17,058
  • 3
  • 60
  • 75
2
// Here is a complete program with the solution in C and command-line parameters
// Compile with the math library: 
//    gcc -Wall -o point_on_circle -lm point_on_circle.c
//
// point_on_circle.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

double inline degree2radian(int a) { return (a * 0.017453292519); }

void getEndPoint(double angle, int len, int start_x, 
    int start_y, int *end_x, int *end_y) {
        *end_x = start_x + len * cos(angle);
        *end_y = start_y + len * sin(angle);
} // getEndPoint

int main(int argc, char *argv[]) {
  double angle = atoi(argv[1]);
  int length   = atoi(argv[2]);
  int start_x  = atoi(argv[3]);
  int start_y  = atoi(argv[4]);
  int x, y;

  getEndPoint(degree2radian(angle), length, start_x, start_y, &x, &y);
  printf("end x=%d, end y=%d\n", x, y);

  return 0;
} // main
Dave
  • 21
  • 1
1

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
  • 219,201
  • 40
  • 422
  • 469
0

People are forgetting the complex library in C++, which does polar to rectangular conversions for us.

complex<double> getEndPoint(complex<double> const &startPoint, double magnitude, double radians)
{
    return startPoint + polar<double>(magnitude, radians);
}

int main()
{
    complex<double> startingPoint(0.0, 300.0);
    auto newPoint = getEndPoint(startingPoint, 600, 0.523598776);

    cout << newPoint << endl;
}

I'd also be careful with your chosen terminology. When I see get in a name, I think of it as retrieving an answer stored somewhere. In this example, we're computing something, and that could be false assurance given to a user of your code.

user904963
  • 1,520
  • 2
  • 15
  • 33