Skip to main content
Clarify the return value
Source Link

It is possible with the data you have. Let's call the line segment vw and the point p:

vec2 projection_point(vec2 v, vec2 w, vec2 p) {
  const float l2 = length_squared(v, w);  // i.e. |w-v|^2 -  avoid a sqrt
  if (l2 == 0.0) return distance(p, v);   // v == w case
  // Consider the line extending the segment, parameterized as v + t (w - v).
  // We find projection of point p onto the line. 
  // It falls where t = [(p-v) . (w-v)] / |w-v|^2
  // We clamp t from [0,1] to handle points outside the segment vw.
  const float t = max(0, min(1, dot(p - v, w - v) / l2));
  return v + t * (w - v);  // Projection point that falls on the segment
}

This function returns the nearest point on the line segment.

The above code is derived from here: https://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment

It is possible with the data you have. Let's call the line segment vw and the point p:

vec2 projection_point(vec2 v, vec2 w, vec2 p) {
  const float l2 = length_squared(v, w);  // i.e. |w-v|^2 -  avoid a sqrt
  if (l2 == 0.0) return distance(p, v);   // v == w case
  // Consider the line extending the segment, parameterized as v + t (w - v).
  // We find projection of point p onto the line. 
  // It falls where t = [(p-v) . (w-v)] / |w-v|^2
  // We clamp t from [0,1] to handle points outside the segment vw.
  const float t = max(0, min(1, dot(p - v, w - v) / l2));
  return v + t * (w - v);  // Projection point that falls on the segment
}

The above code is derived from here: https://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment

It is possible with the data you have. Let's call the line segment vw and the point p:

vec2 projection_point(vec2 v, vec2 w, vec2 p) {
  const float l2 = length_squared(v, w);  // i.e. |w-v|^2 -  avoid a sqrt
  // Consider the line extending the segment, parameterized as v + t (w - v).
  // We find projection of point p onto the line. 
  // It falls where t = [(p-v) . (w-v)] / |w-v|^2
  // We clamp t from [0,1] to handle points outside the segment vw.
  const float t = max(0, min(1, dot(p - v, w - v) / l2));
  return v + t * (w - v);  // Projection point that falls on the segment
}

This function returns the nearest point on the line segment.

The above code is derived from here: https://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment

Source Link

It is possible with the data you have. Let's call the line segment vw and the point p:

vec2 projection_point(vec2 v, vec2 w, vec2 p) {
  const float l2 = length_squared(v, w);  // i.e. |w-v|^2 -  avoid a sqrt
  if (l2 == 0.0) return distance(p, v);   // v == w case
  // Consider the line extending the segment, parameterized as v + t (w - v).
  // We find projection of point p onto the line. 
  // It falls where t = [(p-v) . (w-v)] / |w-v|^2
  // We clamp t from [0,1] to handle points outside the segment vw.
  const float t = max(0, min(1, dot(p - v, w - v) / l2));
  return v + t * (w - v);  // Projection point that falls on the segment
}

The above code is derived from here: https://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment