【问题标题】:Given an angle and length, how do I calculate the coordinates给定角度和长度,如何计算坐标
【发布时间】:2009-10-28 16:34:19
【问题描述】:

假设左上角为(0,0),给定30度角,起点(0,300),线长600,如何计算线的终点所以 这条线代表给定的角度。

C 伪代码是

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

【问题讨论】:

  • 我假设您希望 end_x 和 end_y 的类型为 (int *),并在函数结束时翻转您的作业。
  • 是数学问题还是代码问题?如果是数学,那这可能不是论坛,而且很初级;我推荐你Polar -Rectangular Conversion。但是请记住,当您实现它时,C 数学库使用弧度而不是度数 r = d * PI / 180.0
  • 伙计,我因为回答编程问题而被否决了。哦,多么善变……
  • 谢谢,我想要计算值。编辑以反映这一点。
  • Spolsky 和 ​​Atwood 建立 mathoverflow.com 需要多长时间?

标签: c++ c math trigonometry


【解决方案1】:
// 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);

【讨论】:

  • cos()sin() 采用弧度,小心。
  • 效果很好……有时我把事情做得太难了。非常感谢!
  • -1 用于宏...请使用内联函数!无论如何都会有一个转换发生乘法....
  • 见鬼,即使你有 C99 编译器,创建内联函数也只是一个建议。宏保证是真正的交易。
  • 这个问题是关于三角而不是宏与内联函数。我只是指出度数到弧度的转换是微不足道的。
【解决方案2】:

你没有说角度是相对于什么测量的,或者你的轴的方向是什么。这些都会有所作为。

您首先需要将度数转换为弧度(乘以 PI 再除以 180)。 然后你需要把你的角度的正弦和余弦乘以线的长度。现在,您的坐标有两个数字,但这取决于您的轴的方向以及测量角度的位置,这些值中的哪个是 x 坐标,哪个是 y,以及是否需要对它们中的任何一个求反。

【讨论】:

    【解决方案3】:
    // 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
    

    【讨论】:

      【解决方案4】:

      math.h 具有您应该需要的所有三角函数。您可能需要将-lm 提供给链接器,具体取决于您构建的系统(有时是自动的)。

      【讨论】:

        【解决方案5】:

        人们忘记了 C++ 中的 complex 库,它为我们进行极坐标到矩形的转换。

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

        我也会小心您选择的术语。当我在名称中看到get 时,我认为它是在检索存储在某处的答案。在此示例中,我们正在计算一些东西,这可能是向您的代码用户提供的虚假保证。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-04-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多