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

时间:2009-10-28 16:34: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;
}

5 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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

答案 2 :(得分:1)

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

答案 3 :(得分:1)

你没有说出相对于角度的测量角度,或者你的轴的确定方向。这些将有所作为。

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

答案 4 :(得分:0)

人们忘记了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时,我将其视为检索存储在某处的答案。在这个例子中,我们正在计算某些东西,这可能是给代码用户的错误保证。

相关问题