From 4ad50ba37ccf377d411f1d2967c964ec7bcb33be Mon Sep 17 00:00:00 2001 From: Derek McQuay Date: Fri, 26 Aug 2016 12:02:43 -0700 Subject: [PATCH] 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 --- triangle.go | 7 +++++++ triangle_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 triangle.go create mode 100644 triangle_test.go diff --git a/triangle.go b/triangle.go new file mode 100644 index 0000000..489dfda --- /dev/null +++ b/triangle.go @@ -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) +} diff --git a/triangle_test.go b/triangle_test.go new file mode 100644 index 0000000..7c59186 --- /dev/null +++ b/triangle_test.go @@ -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, + ) + } + + } +}