Given an angle and length, how do I calculate the coordinates

后端 未结 5 1517
太阳男子
太阳男子 2020-12-30 02:18

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

相关标签:
5条回答
  • 2020-12-30 02:46
    // 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
    
    0 讨论(0)
  • 2020-12-30 02:53

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

    0 讨论(0)
  • 2020-12-30 03:00
    // 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);
    
    0 讨论(0)
  • 2020-12-30 03:10

    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.

    0 讨论(0)
  • 2020-12-30 03:12

    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.

    0 讨论(0)
提交回复
热议问题