added bounds function and segline to intersect function
This commit is contained in:
25
internal/geom/fit.go
Normal file
25
internal/geom/fit.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package geom
|
||||
|
||||
import "math"
|
||||
|
||||
type AABB struct{ Min, Max Vec }
|
||||
|
||||
func Bounds(pts []Vec) AABB {
|
||||
minX, minY := math.Inf(1), math.Inf(1)
|
||||
maxX, maxY := math.Inf(-1), math.Inf(-1)
|
||||
for _, p := range pts {
|
||||
if p.X < minX {
|
||||
minX = p.X
|
||||
}
|
||||
if p.Y < minY {
|
||||
minY = p.Y
|
||||
}
|
||||
if p.X > maxX {
|
||||
maxX = p.X
|
||||
}
|
||||
if p.Y > maxY {
|
||||
maxY = p.Y
|
||||
}
|
||||
}
|
||||
return AABB{Min: V(minX, minY), Max: V(maxX, maxY)}
|
||||
}
|
||||
11
internal/geom/intersect.go
Normal file
11
internal/geom/intersect.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package geom
|
||||
|
||||
func SegLineIntersect(A, B, O, D Vec) (bool, float64) {
|
||||
r := Sub(B, A)
|
||||
den := Cross(r, D)
|
||||
if NearlyZero(den) {
|
||||
return false, 0
|
||||
}
|
||||
t := Cross(Sub(O, A), D) / den
|
||||
return t > EPS && t < 1-EPS, t
|
||||
}
|
||||
Reference in New Issue
Block a user