给定角度和长度,如何计算坐标

ama*_*nda 20 c c++ math trigonometry

假设左上角是(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;
}
Run Code Online (Sandbox Code Playgroud)

Nic*_*unt 39

// 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);
Run Code Online (Sandbox Code Playgroud)

  • `cos()`和`sin()`取弧度,小心点. (12认同)
  • 这个问题是关于trig而不是宏与内联函数.我只是指出弧度转换的程度是微不足道的. (5认同)
  • 弧度= pi*度/ 180 (4认同)