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

发布于 2024-08-09 09:16:35 字数 468 浏览 7 评论 0原文

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

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.

The C pseudo-code is

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

生生漫 2024-08-16 09:16:35
// 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);
// 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);
罪#恶を代价 2024-08-16 09:16:35

您没有说明相对于什么角度进行测量,或者实际上您的轴的方向是什么。这些都会有所作为。

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

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.

在巴黎塔顶看东京樱花 2024-08-16 09:16:35
// 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
// 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
回心转意 2024-08-16 09:16:35

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

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

日暮斜阳 2024-08-16 09:16:35

人们忘记了 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 时,我认为它是检索存储在某处的答案。在此示例中,我们正在计算某些内容,这可能是向代码用户提供的错误保证。

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

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 get in 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文