Add DistanceToLine function

This commit is contained in:
Sasha Koshka 2024-06-25 01:55:28 -04:00
parent c1c166efcf
commit 1f04d688cf

View File

@ -59,6 +59,21 @@ func Distance (start, end image.Point) float64 {
return math.Sqrt(float64(delta.X * delta.X) + float64(delta.Y * delta.Y))
}
// DistanceToLine calculates the distance from a point to a line which passes
// through points line1 and line2.
func DistanceToLine (point, line1, line2 image.Point) float64 {
x := float64(point.X)
y := float64(point.Y)
x1 := float64(line1.X)
y1 := float64(line1.Y)
x2 := float64(line2.X)
y2 := float64(line2.Y)
triangle := math.Abs((y2 - y1)*x - (x2 - x1)*y + x2*y1 - y2*x1)
return triangle / Distance(line1, line2)
}
func lerp (fac float64, x, y int) int {
return int(float64(x) * fac + float64(y) * (1.0 - fac))
}