gg/line.go

34 lines
565 B
Go
Raw Normal View History

2023-10-23 15:45:18 +03:00
package gg
2023-05-28 19:14:02 +03:00
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
}
2023-06-03 12:26:31 +03:00
// Returns the line itself. Made to implement the Liner interface.
2023-05-28 21:07:05 +03:00
func (l Line) Line() Line {
return l
}
2023-05-28 21:22:52 +03:00
func (l Line) ContainsPoint(p Point) bool {
buf := Line{0, p.Y}
pc, ok := l.crossesLine(buf)
if !ok {
return false
2023-05-28 21:07:05 +03:00
}
2023-05-28 21:22:52 +03:00
return pc == p
}
2023-05-28 21:07:05 +03:00
func (l1 Line) crossesLine(l2 Line) (Point, bool) {
2023-06-03 12:26:31 +03:00
if LinersParallel(l1, l2) {
2023-05-28 21:22:52 +03:00
return Point{}, false
}
2023-05-28 21:07:05 +03:00
x := (l1.C - l2.C) / (l2.K - l1.K)
y := l1.K*x + l1.C
return Point{x, y}, true
}