Skip to content

Instantly share code, notes, and snippets.

@whitehat101
Created June 16, 2017 10:18
Show Gist options
  • Save whitehat101/f40f101d961e5121783c392aec3efc8f to your computer and use it in GitHub Desktop.
Save whitehat101/f40f101d961e5121783c392aec3efc8f to your computer and use it in GitHub Desktop.
point_to_line / point_to_line_points
# point_to_line
# point: (x0, y0)
# line: ax+by+c=0
def point_to_line x0,y0, a,b,c
(a*x0+b*y0+c).abs / Math.sqrt(a**2+b**2)
end
# point: (x0, y0)
# line: between (x1, y1), (x2, y2)
def point_to_line_points x0,y0, x1,y1, x2,y2
((y2-y1)*x0-(x2-x1)*y0+x2*y1-y2*x1).abs / Math.sqrt((x2-x1)**2+(y2-y1)**2)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment