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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1638437/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:41:26  来源:igfitidea点击:

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

c++cmathtrigonometry

提问by amanda

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 line so that the line is representative of the angle given.

假设左上角是 (0,0) 并且给定的角度为 30 度,起点为 (0,300),线长为 600,我如何计算线的终点以便线代表给定的角度。

The C pseudo-code is

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

回答by Nick Van Brunt

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

回答by Weeble

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.

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

回答by Dave

// 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

回答by Carl Norum

math.hhas all the trigonometric functions you should need. You may need to give -lmto your linker, depending on what system you're building on (sometimes it's automatic).

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

回答by user904963

People are forgetting the complexlibrary in C++, which does polar to rectangular conversions for us.

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

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

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