From 1f04d688cf35c6944bdaa28291e5f44e0c373949 Mon Sep 17 00:00:00 2001 From: "sashakoshka@tebibyte.media" Date: Tue, 25 Jun 2024 01:55:28 -0400 Subject: [PATCH] Add DistanceToLine function --- image/path/path.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/image/path/path.go b/image/path/path.go index 78a620e..92c0438 100644 --- a/image/path/path.go +++ b/image/path/path.go @@ -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)) }