Geometry snippets: Difference between revisions
Jump to navigation
Jump to search
Content added Content deleted
(new page) |
(shortest distance from point to line) |
||
Line 3: | Line 3: | ||
To obtain a line equation of the form <math>Ax + By + C = 0</math> from two points <math>a</math> and <math>b</math>, set: |
To obtain a line equation of the form <math>Ax + By + C = 0</math> from two points <math>a</math> and <math>b</math>, set: |
||
<math> |
:<math> |
||
\begin{align} |
\begin{align} |
||
A &= a_y - b_y \\ |
A &= a_y - b_y \\ |
||
Line 13: | Line 13: | ||
Source: https://bobobobo.wordpress.com/2008/01/07/solving-linear-equations-ax-by-c-0/ |
Source: https://bobobobo.wordpress.com/2008/01/07/solving-linear-equations-ax-by-c-0/ |
||
=== |
=== Shortest distance from point to line === |
||
Given a line <math>Ax + By + C = 0</math> and a point <math>p</math>, the shortest distance from the point to the line is given by: |
|||
:<math> |
|||
\frac{\left | A \cdot p_x + B \cdot p_y + C \right |}{\sqrt{A^2 + B^2}} |
|||
</math> |
|||
Source: https://www.toppr.com/guides/maths/straight-lines/distance-of-point-from-a-line/ |
|||
=== Shortest distance from point to line segment === |
|||
<source lang="C++"> |
<source lang="C++"> |
Latest revision as of 10:00, 28 January 2020
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);
}