This repository has been archived on 2024-12-14. You can view files and clone it, but cannot push or open issues or pull requests.
goutil/image/path/path.go

86 lines
2.5 KiB
Go
Raw Normal View History

2024-06-23 06:45:58 -06:00
package upath
import "math"
import "image"
// P creates a path fom a list of integers that form X-Y pairs.
func P (data ...int) []image.Point {
result := make([]image.Point, len(data) / 2)
for index := range result {
result[index].X = data[index * 2]
result[index].Y = data[index * 2 + 1]
}
return result
}
// Lerp linearally interpolates between two paths. If the paths differ in
2024-06-24 23:55:11 -06:00
// length, the returned path will only be as long as the smaller one. This
// function will always return a new slice.
2024-06-23 06:45:58 -06:00
func Lerp (fac float64, start, end []image.Point) []image.Point {
result := make([]image.Point, len(start))
for index, startPt := range start {
if index >= len(end) { return result[:index] }
endPt := end[index]
result[index] = image.Pt (
lerp(fac, startPt.X, endPt.X),
lerp(fac, startPt.Y, endPt.Y))
}
return result
}
2024-06-24 23:55:11 -06:00
// MultiLerp is like Lerp, but interpolates through several "stages". This
// function will always return a new slice. If no stages are provided, it will
// return nil.
func MultiLerp (fac float64, stages ...[]image.Point) []image.Point {
if len(stages) == 0 { return nil }
if len(stages) == 1 {
result := make([]image.Point, len(stages[0]))
copy(result, stages[0])
return result
}
2024-06-26 08:41:03 -06:00
expandedFac := fac * float64(len(stages) - 1)
2024-06-24 23:55:11 -06:00
startIndex := int(expandedFac)
if startIndex >= len(stages) { startIndex = len(stages) - 1 }
endIndex := startIndex + 1
if endIndex >= len(stages) { endIndex = len(stages) - 1 }
start := stages[startIndex]
end := stages[endIndex]
partialFac := expandedFac - float64(startIndex)
return Lerp(partialFac, start, end)
}
2024-06-23 06:45:58 -06:00
// Distance returns the distance between two points.
func Distance (start, end image.Point) float64 {
delta := start.Sub(end)
return math.Sqrt(float64(delta.X * delta.X) + float64(delta.Y * delta.Y))
}
2024-06-24 23:55:28 -06:00
// 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)
}
2024-06-24 23:56:15 -06:00
// Translate adds delta to all points in path. This modifies the slice contents.
func Translate (delta image.Point, path []image.Point) {
for index, point := range path {
path[index] = point.Add(delta)
}
}
2024-06-23 06:45:58 -06:00
func lerp (fac float64, x, y int) int {
2024-06-26 08:41:03 -06:00
return int(float64(x) * (1.0 - fac) + float64(y) * fac)
2024-06-23 06:45:58 -06:00
}