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

假设左上角是(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; } 

 // 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); 
 // 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  #include  #include  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 

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

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

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

人们忘记了C ++中的complex库,它为我们做了极性到矩形的转换。

 complex getEndPoint(complex const &startPoint, double magnitude, double radians) { return startPoint + polar(magnitude, radians); } int main() { complex startingPoint(0.0, 300.0); auto newPoint = getEndPoint(startingPoint, 600, 0.523598776); cout << newPoint << endl; } 

我也会小心你选择的术语。 当我看到名字时,我认为它是检索存储在某个地方的答案。 在这个例子中,我们正在计算某些东西,这可能是给代码用户的错误保证。