62 lines
1.1 KiB
Go
62 lines
1.1 KiB
Go
|
package mx
|
||
|
|
||
|
//import "fmt"
|
||
|
|
||
|
// The type represents mathematical equation of line and line itself.
|
||
|
type LineExpr struct {
|
||
|
K, C, X Float
|
||
|
Vertical bool
|
||
|
}
|
||
|
|
||
|
// Returns the line itself. Made to implement the Liner interface.
|
||
|
func (l LineExpr) GetLineExpr() LineExpr {
|
||
|
return l
|
||
|
}
|
||
|
|
||
|
func (e LineExpr) GetContainedPoints(pts Vectors) Vectors {
|
||
|
ret := make(Vectors, 0, len(pts))
|
||
|
for _, pt := range pts {
|
||
|
if e.ContainsPoint(pt) {
|
||
|
ret = append(ret, pt)
|
||
|
}
|
||
|
}
|
||
|
return ret
|
||
|
}
|
||
|
|
||
|
func (l LineExpr) ContainsPoint(p Vector) bool {
|
||
|
buf := LineExpr{0, p.Y, 0, false}
|
||
|
pc, ok := DoLinersCross(l, buf)
|
||
|
if !ok {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
//Println("points:", l, p, pc)
|
||
|
return Neq(pc.Point.X, p.X) && Neq(pc.Point.Y, p.Y)
|
||
|
}
|
||
|
|
||
|
func (l1 LineExpr) Crosses(l2 LineExpr) (Vector, bool) {
|
||
|
var x, y Float
|
||
|
if AreLinersParallel(l1, l2) {
|
||
|
return ZV, 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 Vector{x, y}, true
|
||
|
}
|
||
|
|
||
|
|