gg/line.go

52 lines
890 B
Go
Raw Normal View History

2023-10-23 15:45:18 +03:00
package gg
2023-05-28 19:14:02 +03:00
//import "fmt"
2023-05-28 19:45:58 +03:00
// The type represents mathematical equation of line and line itself.
type Line struct {
K, C, X Float
Vertical bool
2023-05-28 19:14:02 +03:00
}
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, 0, false}
2023-05-28 21:22:52 +03:00
pc, ok := l.crossesLine(buf)
if !ok {
return false
2023-05-28 21:07:05 +03:00
}
//Println("points:", l, p, pc)
return Neq(pc.X, p.X) && Neq(pc.Y, p.Y)
2023-05-28 21:22:52 +03:00
}
2023-05-28 21:07:05 +03:00
func (l1 Line) crossesLine(l2 Line) (Point, bool) {
var x, y Float
2023-06-03 12:26:31 +03:00
if LinersParallel(l1, l2) {
2023-05-28 21:22:52 +03:00
return Point{}, false
}
switch {
case l1.Vertical :
x = l1.X
y = l2.K*x + l2.C
//Println("l1-vert:", x, y)
case l2.Vertical :
x = l2.X
y = l1.K*x + l1.C
//Println("l2-vert:", x, y)
default:
x = (l1.C - l2.C) / (l2.K - l1.K)
y = l1.K*x + l1.C
}
//Println("in:", x, y)
2023-05-28 21:07:05 +03:00
return Point{x, y}, true
}