gg/src/gx/line.go

37 lines
641 B
Go
Raw Normal View History

2023-05-28 19:14:02 +03:00
package gx
import (
"math"
)
2023-05-28 19:45:58 +03:00
// The type represents mathematical equation of line and line itself.
type Line struct {
2023-05-28 19:14:02 +03:00
K, C Float
}
// The type represents a line segment.
type LineSegment [2]Point
// Get square of length of line segment.
func (ls LineSegment) LenSqr() Float {
return Sqr(ls[0].X - ls[1].X) +
Sqr(ls[0].Y - ls[1].Y)
}
// Get length of the line segment.
func (ls LineSegment) Len() Float {
return math.Sqrt(ls.LenSqr())
}
2023-05-28 19:45:58 +03:00
// Returns corresponding to the segment line line.
func (l LineSegment) Line() Line {
2023-05-28 19:14:02 +03:00
p0 := l[0]
p1 := l[1]
k := (p0.Y - p1.Y) / (p0.X - p1.X)
c := p0.Y - p0.X*k
2023-05-28 19:45:58 +03:00
return Line{k, c}
2023-05-28 19:14:02 +03:00
}