本文介绍了给予开始和结束点,以及一个距离,计算沿着行的一个点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找来计算位于线路上的点的最快方式
一个给定的距离,从行的终点距离:

Looking for the quickest way to calculate a point that lies on a linea given distance away from the end point of the line:

void calculate_line_point(int x1, int y1, int x2, int y2, int distance, int *px, int *py)
{
    //calculate a point on the line x1-y1 to x2-y2 that is distance from x2-y2
    *px = ???
    *py = ???
}

感谢您的答复,不,这不是功课,只是一些黑客出来
我的正常区域的专业知识。

Thanks for the responses, no this is not homework, just some hacking out ofmy normal area of expertise.

这是以下建议的功能。这不是接近的工作。如果我
计算点每5度上的右上90度的部
圆作为起点,并调用函数下面的圆作为X2,Y2为4的距离的终点是完全错误的中心位置。它们下方和该中心的右侧躺下和长度只要中心点。任何人有什么建议吗?

This is the function suggested below. It's not close to working. If Icalculate points every 5 degrees on the upper right 90 degree portion ofa circle as starting points and call the function below with the center of the circle as x2,y2 with a distance of 4 the end points are totally wrong. They lie below and to the right of the center and the length is as long as the center point. Anyone have any suggestions?

void calculate_line_point(int x1, int y1, int x2, int y2, int distance)
{

//calculate a point on the line x1-y1 to x2-y2 that is distance from x2-y2

  double vx = x2 - x1; // x vector
  double vy = y2 - y1; // y vector

  double mag = sqrt(vx*vx + vy*vy); // length

  vx /= mag;
  vy /= mag;

// calculate the new vector, which is x2y2 + vxvy * (mag + distance).

  px = (int) ( (double) x2 + vx * (mag + (double)distance) );
  py = (int) ( (double) y2 + vy * (mag + (double)distance) );

}

我发现this解决方案的计算器,但不完全理解它,任何人都可以澄清?

I've found this solution on stackoverflow but don't understand it completely, can anyone clarify?

推荐答案

我觉得这属于上MathOverflow,但我会回答,因为这是你的第一篇文章。
首先,你从X1Y1计算矢量X2Y2:

I think this belongs on MathOverflow, but I'll answer since this is your first post.First you calculate the vector from x1y1 to x2y2:

float vx = x2 - x1;
float vy = y2 - y1;

然后计算长度:

float mag = sqrt(vx*vx + vy*vy);

规范化向量单位长度:

Normalize the vector to unit length:

vx /= mag;
vy /= mag;

最后计算新的载体,它是X2Y2 + vxvy *(MAG +距离)。

Finally calculate the new vector, which is x2y2 + vxvy * (mag + distance).

*px = (int)((float)x1 + vx * (mag + distance));
*py = (int)((float)y1 + vy * (mag + distance));

您可以省略一些计算距离/ MAG,而不是相乘。

You can omit some of the calculations multiplying with distance / mag instead.

这篇关于给予开始和结束点,以及一个距离,计算沿着行的一个点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 06:40