view pkg/octree/plane2d_test.go @ 963:e0825645f00e

merge
author Thomas Junk <thomas.junk@intevation.de>
date Wed, 17 Oct 2018 15:22:38 +0200
parents 9be20bd0f131
children a244b18cb916
line wrap: on
line source

package octree

import (
	"math"
	"testing"
)

func TestIntersection(t *testing.T) {

	table := []struct {
		a          [4]float64
		b          [4]float64
		intersects bool
		x, y       float64
	}{
		{[4]float64{-1, -1, 1, 1}, [4]float64{-1, 1, 1, -1}, true, 0, 0},
		{[4]float64{0, 0, 1, 1}, [4]float64{0, 1, 1, 0}, true, 0.5, 0.5},
		{[4]float64{0, 0, 1, 0}, [4]float64{0, 1, 1, 1}, false, 0, 0},
	}

	for _, e := range table {
		p1 := NewPlane2D(e.a[0], e.a[1], e.a[2], e.a[3])
		p2 := NewPlane2D(e.b[0], e.b[1], e.b[2], e.b[3])
		x, y, intersects := p1.Intersection(p2)
		if intersects != e.intersects {
			t.Fatalf("Have %t want %t\n", intersects, e.intersects)
		}
		if e.intersects {
			if math.Abs(e.x-x) > epsPlane || math.Abs(e.y-y) > epsPlane {
				t.Fatalf("Have (%f, %f)t want (%f, %f)\n", x, y, e.x, e.y)
			}
		}
	}
}