51 lines
890 B
Go
51 lines
890 B
Go
package gg
|
|
|
|
//import "fmt"
|
|
|
|
// The type represents mathematical equation of line and line itself.
|
|
type Line struct {
|
|
K, C, X Float
|
|
Vertical bool
|
|
}
|
|
|
|
// Returns the line itself. Made to implement the Liner interface.
|
|
func (l Line) Line() Line {
|
|
return l
|
|
}
|
|
|
|
func (l Line) ContainsPoint(p Point) bool {
|
|
buf := Line{0, p.Y, 0, false}
|
|
pc, ok := l.crossesLine(buf)
|
|
if !ok {
|
|
return false
|
|
}
|
|
|
|
//Println("points:", l, p, pc)
|
|
return Neq(pc.X, p.X) && Neq(pc.Y, p.Y)
|
|
}
|
|
|
|
func (l1 Line) crossesLine(l2 Line) (Point, bool) {
|
|
var x, y Float
|
|
if LinersParallel(l1, l2) {
|
|
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)
|
|
return Point{x, y}, true
|
|
}
|
|
|
|
|