46 lines
767 B
Go
46 lines
767 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
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
if l1.Vertical {
|
|
x = l1.X
|
|
y = l2.K*x + l2.C
|
|
} else if l2.Vertical {
|
|
x = l2.X
|
|
y = l1.K*x + l1.C
|
|
} else {
|
|
x = (l1.C - l2.C) / (l2.K - l1.K)
|
|
y = l1.K*x + l1.C
|
|
}
|
|
|
|
return Point{x, y}, true
|
|
}
|
|
|
|
|