Geometry snippets

From vegard.wiki
Jump to navigation Jump to search

Line equation from two points

To obtain a line equation of the form from two points and , set:

Source: https://bobobobo.wordpress.com/2008/01/07/solving-linear-equations-ax-by-c-0/

Shortest distance from point to line

Given a line and a point , the shortest distance from the point to the line is given by:

Source: https://www.toppr.com/guides/maths/straight-lines/distance-of-point-from-a-line/

Shortest distance from point to line segment

// https://www.iquilezles.org/www/articles/distfunctions2d/distfunctions2d.htm
static float sdLineSegment(glm::vec2 p, glm::vec2 a, glm::vec2 b)
{
    auto pa = p - a;
    auto ba = b - a;
    auto h = glm::clamp(glm::dot(pa, ba) / glm::dot(ba, ba), 0.f, 1.f);
    return glm::length(pa - ba * h);
}