Geometry snippets

From vegard.wiki
Jump to navigation Jump to search

Line equation from two points

To obtain a line equation of the form Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle Ax + By + C = 0} from two points Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle a} and Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle b} , set:

Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \begin{align} A &= a_y - b_y \\ B &= b_x - a_x \\ C &= a_x \cdot b_y - b_x \cdot a_y \end{align} }

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

Shortest distance from point to line

Given a line Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle Ax + By + C = 0} and a point Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle p} , the shortest distance from the point to the line is given by:

Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \frac{\left | A \cdot p_x + B \cdot p_y + C \right |}{\sqrt{A^2 + B^2}} }

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