寻找一种最快的方法来计算距离线段端点一定距离处的点:
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 = ???
}
感谢回复,这不是作业,只是我在专业领域之外的一些尝试。
以下是建议使用的函数。它与实际情况相距甚远。如果我在圆形的右上90度部分以每5度计算一个点作为起始点,并将该函数称为具有距离为4的x2、y2圆心,则终点完全错误。它们位于中心下方和右侧,并且长度与中心点一样长。大家有什么建议吗?
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) );
我在stackoverflow找到了这个解决方案,但我并不完全理解,请问有人能澄清一下吗?