获取距离另一个点最近的线段上的点

4

我想在线段AB上找到离另一个点P最近的点。

我的想法是:

  1. 从A和B点坐标中获取线段公式y1 = a1x + b1的a1和b1。
  2. 从a1和P坐标中获取垂线的公式y2 = a2x + b2。
  3. 通过等式y1和y2来得到交点的x坐标,然后使用上述公式之一来得到y坐标。

enter image description here

我的代码:

#include <SFML\Graphics.hpp>
#include <iostream>

sf::Vector2f getClosestPointOnLine(sf::Vector2f A, sf::Vector2f B, sf::Vector2f P)
{
    //convert to line formula
    float a = (B.y - A.y)/(B.x - A.x);
    float b = -a * A.x + A.y;

    //get normal line formula
    float a2 = -a / 2;
    float b2 = -a2 * P.x + P.y;

    //get x
    float a3 = a - a2;
    float b3 = b2 - b;

    float x = b3 / a3;

    //get y
    float y = a * x + b;

    return { x, y };
}

int main()
{
    sf::RenderWindow gameWindow(sf::VideoMode(800, 600), "App");

    sf::View view(sf::FloatRect(0, 0, 800, 600));
    gameWindow.setView(view);

    gameWindow.setFramerateLimit(60);

    sf::VertexArray plane(sf::LinesStrip, 2);

    plane[0] = { { view.getSize().x * 0.5f, view.getSize().y * 0.8f } };
    plane[1] = { { view.getSize().x * 0.8f, view.getSize().y * 0.6f } };

    sf::CircleShape ball(10);

    ball.setOrigin(10, 10);
    ball.setPosition({view.getSize().x * 0.7f, view.getSize().y * 0.4f});

    while (gameWindow.isOpen())
    {
        sf::Event e;
        while (gameWindow.pollEvent(e))
        {
            if (e.type == sf::Event::Closed)
            {
                gameWindow.close();
            }
        }

        //draw
        gameWindow.clear(sf::Color{30, 30, 30});

        ball.setPosition((sf::Vector2f)sf::Mouse::getPosition(gameWindow));

        sf::Vector2f closest = getClosestPointOnLine(plane[0].position, plane[1].position, ball.getPosition());

        sf::CircleShape cs(5);
        cs.setOrigin(5, 5 );
        cs.setPosition(closest);

        gameWindow.draw(cs);
        gameWindow.draw(plane);
        gameWindow.draw(ball);
        gameWindow.display();
    }
}

结果: enter image description here 如您所见,函数getClosestPointOnLine返回的交点是错误的。我的函数有什么问题吗?
------------------编辑: 正如n.m.所提到的,-a / 2不是法线斜率公式,我关于这个公式错了,正确的公式是:-1 / a

为了淡化所有的实现细节,你想要什么?(顺便说一句,图片很好) - user1767754
1
“float a2 = -a / 2;” 不是一般的直线公式,你从哪里得到的?而且你的想法也没有考虑到直线可以是垂直和水平的。 - n. m.
@user1767754 我的偏移量很奇怪,线段中较大点和较小点之间的连线必须垂直于该线段。 - TKK
1个回答

15
你需要的是点 P 在线段上的投影。你可以使用点积来实现:
auto AB = B - A;
auto AP = P - A;
float lengthSqrAB = AB.x * AB.x + AB.y * AB.y;
float t = (AP.x * AB.x + AP.y * AB.y) / lengthSqrAB;

现在,t是介于AB之间的插值参数。如果它是0,则该点投影到A。如果是1,则它投影到B。分数值表示两点之间的点。如果您想将投影限制在线段上,则需要夹住t
if(t < 0)
    t = 0;
if(t > 1)
    t = 1;

最后,您可以计算得出这个点:
return A + t * AB;

你能解释一下t值的计算方式和原理吗? - BossCode
@BossCode:如果你对它背后的数学感兴趣,可以查看一下维基百科关于向量投影的文章。那里的图形比我在评论中能更好地展示这个概念。 - Nico Schertler

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接