2023-10-23 15:45:18 +03:00
|
|
|
package gg
|
2023-05-28 19:14:02 +03:00
|
|
|
|
2024-01-22 10:23:47 +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 {
|
2024-01-22 10:23:47 +03:00
|
|
|
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 {
|
2024-01-22 10:23:47 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-01-22 20:08:50 +03:00
|
|
|
//Println("points:", l, p, pc)
|
2024-01-22 10:23:47 +03:00
|
|
|
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) {
|
2024-01-22 10:23:47 +03:00
|
|
|
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
|
|
|
|
}
|
2024-01-22 10:23:47 +03:00
|
|
|
|
2024-01-22 20:08:50 +03:00
|
|
|
switch {
|
|
|
|
case l1.Vertical :
|
2024-01-22 10:23:47 +03:00
|
|
|
x = l1.X
|
|
|
|
y = l2.K*x + l2.C
|
2024-01-22 20:08:50 +03:00
|
|
|
//Println("l1-vert:", x, y)
|
|
|
|
case l2.Vertical :
|
2024-01-22 10:23:47 +03:00
|
|
|
x = l2.X
|
|
|
|
y = l1.K*x + l1.C
|
2024-01-22 20:08:50 +03:00
|
|
|
//Println("l2-vert:", x, y)
|
|
|
|
default:
|
2024-01-22 10:23:47 +03:00
|
|
|
x = (l1.C - l2.C) / (l2.K - l1.K)
|
|
|
|
y = l1.K*x + l1.C
|
|
|
|
}
|
|
|
|
|
2024-01-22 20:08:50 +03:00
|
|
|
//Println("in:", x, y)
|
2023-05-28 21:07:05 +03:00
|
|
|
return Point{x, y}, true
|
|
|
|
}
|
|
|
|
|
2023-05-30 13:12:11 +03:00
|
|
|
|