added area of three points

I am going to use the area of triangles when determining if the point is
inside of another square to determine containment.  This was just an
important step
This commit is contained in:
Derek McQuay 2016-08-26 12:02:43 -07:00
parent fc4c1c820b
commit 4ad50ba37c
2 changed files with 32 additions and 0 deletions

7
triangle.go Normal file
View File

@ -0,0 +1,7 @@
package rect
import "math"
func SizeTriangle(p1, p2, p3 Point) float64 {
return math.Abs((p1.X*p2.Y + p2.X*p3.Y + p3.X*p1.Y - p1.Y*p2.X - p2.Y*p3.X - p3.Y*p1.X) / 2)
}

25
triangle_test.go Normal file
View File

@ -0,0 +1,25 @@
package rect
import "testing"
func TestSizeTriangle(t *testing.T) {
var sizeTriangleTests = []struct {
pts []Point
expected float64
}{
{[]Point{Point{-2, 3}, Point{-3, -1}, Point{3, -2}}, 12.5},
{[]Point{Point{0, 0}, Point{1, 1}, Point{0, 1}}, 0.5},
{[]Point{Point{10, 14}, Point{20, 15}, Point{12, 52}}, 189},
}
for _, rt := range sizeTriangleTests {
actual := SizeTriangle(rt.pts[0], rt.pts[1], rt.pts[2])
if actual != rt.expected {
t.Errorf(
"failed spiral:\n\texpected: %d\n\t actual: %d",
rt.expected,
actual,
)
}
}
}