Mercurial > gemma
diff pkg/octree/vertex.go @ 774:c55771b7c502
Moved Box2D and Plane2D into vertex.go and made to API public.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Wed, 26 Sep 2018 12:21:12 +0200 |
parents | ba2007b746ef |
children | 9be20bd0f131 |
line wrap: on
line diff
--- a/pkg/octree/vertex.go Wed Sep 26 09:49:00 2018 +0200 +++ b/pkg/octree/vertex.go Wed Sep 26 12:21:12 2018 +0200 @@ -22,6 +22,19 @@ LineStringZ []Vertex MultiLineStringZ []LineStringZ + + Box2D struct { + X1 float64 + Y1 float64 + X2 float64 + Y2 float64 + } + + Plane2D struct { + A float64 + B float64 + C float64 + } ) const ( @@ -297,3 +310,75 @@ return buf.Bytes() } + +func (a Box2D) Intersects(b Box2D) bool { + return !(a.X2 < a.X1 || a.X2 < b.X1 || + a.Y2 < a.Y1 || a.Y2 < b.Y1) +} + +func (a Box2D) Mid() (float64, float64) { + return (a.X2-a.X1)*0.5 + a.X1, (a.Y2-a.Y1)*0.5 + a.Y1 +} + +func (a Box2D) Xi(i int) float64 { + if i == 0 { + return a.X1 + } + return a.X2 +} + +func (a Box2D) Yi(i int) float64 { + if i == 0 { + return a.Y1 + } + return a.Y2 +} + +func NewPlane2D(x1, y1, x2, y2 float64) Plane2D { + a := x2 - x1 + b := y2 - y1 + + l := math.Sqrt(a*a + b*b) + a /= l + b /= l + + // a*x1 + b*y1 + c = 0 + c := -(a*x1 + b*y1) + return Plane2D{a, b, c} +} + +func (p Plane2D) Eval(x, y float64) float64 { + return p.A*x + p.B*y + p.C +} + +const epsPlane = 1e-5 + +func sides(s int, x float64) int { + if math.Signbit(x) { + return s | 2 + } + return s | 1 +} + +const eps = 1e-05 + +func (a Box2D) IntersectsPlane(p Plane2D) bool { + var s int + for i := 0; i < 2; i++ { + x := a.Xi(i) + for j := 0; j < 2; j++ { + y := a.Yi(j) + v := p.Eval(x, y) + if math.Abs(v) < epsPlane { + //log.Printf("on line") + return true + } + if s = sides(s, v); s == 3 { + //log.Printf("... on both sides (djt)") + return true + } + } + } + //log.Printf("side: %d\n", s) + return false +}