39 lines
1.1 KiB
Go
39 lines
1.1 KiB
Go
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
|
|
// length, the returned path will only be as long as the smaller one.
|
|
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
|
|
}
|
|
|
|
// 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))
|
|
}
|
|
|
|
func lerp (fac float64, x, y int) int {
|
|
return int(float64(x) * fac + float64(y) * (1.0 - fac))
|
|
}
|