diff pkg/octree/vertex.go @ 778:9be20bd0f131

Fixed a bug with 2d planes (lines) found while working on line intersections. Added unit test.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 26 Sep 2018 13:17:32 +0200
parents c55771b7c502
children 3d927e06b92c
line wrap: on
line diff
--- a/pkg/octree/vertex.go	Wed Sep 26 12:44:22 2018 +0200
+++ b/pkg/octree/vertex.go	Wed Sep 26 13:17:32 2018 +0200
@@ -335,8 +335,8 @@
 }
 
 func NewPlane2D(x1, y1, x2, y2 float64) Plane2D {
-	a := x2 - x1
-	b := y2 - y1
+	b := x2 - x1
+	a := -(y2 - y1)
 
 	l := math.Sqrt(a*a + b*b)
 	a /= l
@@ -360,8 +360,6 @@
 	return s | 1
 }
 
-const eps = 1e-05
-
 func (a Box2D) IntersectsPlane(p Plane2D) bool {
 	var s int
 	for i := 0; i < 2; i++ {
@@ -382,3 +380,25 @@
 	//log.Printf("side: %d\n", s)
 	return false
 }
+
+func (a Vertex) Cross(b Vertex) Vertex {
+	return Vertex{
+		a.Y*b.Z - a.Z*b.Y,
+		a.Z*b.X - a.X*b.Z,
+		a.X*b.Y - a.Y*b.X,
+	}
+}
+
+func (p1 Plane2D) Intersection(p2 Plane2D) (float64, float64, bool) {
+
+	u1 := Vertex{p1.A, p1.B, p1.C}
+	u2 := Vertex{p2.A, p2.B, p2.C}
+
+	p := u1.Cross(u2)
+
+	if p.Z == 0 {
+		return 0, 0, false
+	}
+
+	return p.X / p.Z, p.Y / p.Z, true
+}