added documentation for all functions

This commit is contained in:
Derek McQuay 2016-08-27 21:14:16 -07:00
parent 9b908a8db1
commit ecc870943e
4 changed files with 34 additions and 3 deletions

13
line.go
View File

@ -5,20 +5,26 @@ import (
"math"
)
// line is described as two Points
type line struct {
pt1, pt2 Point
}
func lineHelper(l line) (float64, float64, float64) {
// determinants aids by calculating necesary values for performing
// calculations using the determinant of two points
func determinants(l line) (float64, float64, float64) {
a := l.pt2.Y - l.pt1.Y
b := l.pt1.X - l.pt2.X
c := a*l.pt1.X + b*l.pt1.Y
return a, b, c
}
// lineIntersection returns the Point of intersection between two lines.
// lineIntersection will only return a point if the point lies on the
// line, otherwise it will return a nil point and an error
func lineIntersection(l1, l2 line) (Point, error) {
a1, b1, c1 := lineHelper(l1)
a2, b2, c2 := lineHelper(l2)
a1, b1, c1 := determinants(l1)
a2, b2, c2 := determinants(l2)
det := a1*b2 - a2*b1
if det != 0 {
@ -37,6 +43,7 @@ func lineIntersection(l1, l2 line) (Point, error) {
return Point{}, fmt.Errorf("lines do not intersect")
}
// lineOnLine determins if both points of a line, l1, are on the same line, l2
func lineOnLine(l1, l2 line) bool {
if onLine(l1.pt1, l1.pt2, l2.pt1) && onLine(l1.pt1, l1.pt2, l2.pt2) {
return true

View File

@ -2,14 +2,17 @@ package rect
import "math"
// Point is a struct defining a coordinates position on a 2D plane
type Point struct {
X, Y float64
}
// distance calculates the distance between two points
func distance(p1, p2 Point) float64 {
return math.Sqrt(math.Pow((p1.X-p2.X), 2) + math.Pow((p1.Y-p2.Y), 2))
}
// onLine determines if point p lies on the line formed by points rp1 and rp2
func onLine(rp1, rp2, p Point) bool {
if math.Abs(distance(rp1, p)+distance(rp2, p)-distance(rp1, rp2)) < ɛ {
minx, maxx := math.Min(rp1.X, rp2.X), math.Max(rp1.X, rp2.X)

View File

@ -5,6 +5,8 @@ import (
"sort"
)
// Rectangle struct defines a plane figure with four straight sides
// and four right angles, which contains 4 vertixes points, P1 through P4
type Rectangle struct {
P1, P2, P3, P4 Point
}
@ -12,6 +14,9 @@ type Rectangle struct {
// maximum error used for floating point math
var ɛ = 0.00001
// isRect determins if the rectangle provided really is a rectangle, which
// by definition means a plane figure with four straight sides and four
// right angles.
func (r Rectangle) isRect() bool {
// make sure they aren't all just the same point
if (r.P1.X == r.P2.X && r.P1.X == r.P3.X && r.P1.X == r.P4.X) &&
@ -29,6 +34,8 @@ func (r Rectangle) isRect() bool {
return math.Abs(dd1-dd2) < ɛ && math.Abs(dd1-dd3) < ɛ && math.Abs(dd1-dd4) < ɛ
}
// neighbors returns the two points that form the line which creates the right
// angle of the point passed in as a parameter.
func (r Rectangle) neighbors(p Point) (Point, Point) {
keys := []float64{distance(r.P1, p), distance(r.P2, p), distance(r.P3, p), distance(r.P4, p)}
sort.Float64s(keys)
@ -52,11 +59,15 @@ func (r Rectangle) neighbors(p Point) (Point, Point) {
return n[0], n[1]
}
// size returns the size of the Rectangle
func (r Rectangle) size() float64 {
n1, n2 := r.neighbors(r.P1)
return distance(r.P1, n1) * distance(r.P1, n2)
}
// inOrder returns a []Point, let us call pts, in which the four sides of the
// Rectangle can be defined as pts[0] -- pts[1], pts[0] -- pts[2],
// pts[3] -- pts[1], and pts[3] -- pts[2]
func (r Rectangle) inOrder() []Point {
n1, n2 := r.neighbors(r.P1)
accross := &Point{}
@ -72,6 +83,8 @@ func (r Rectangle) inOrder() []Point {
return []Point{r.P1, n1, n2, *accross}
}
// Adjacency detects whether two rectangles, r1, and r2, are adjacent.
// Adjacency is defined as the sharing of a side
func Adjacency(r1, r2 Rectangle) bool {
order1 := r1.inOrder()
order2 := r2.inOrder()
@ -99,6 +112,9 @@ func Adjacency(r1, r2 Rectangle) bool {
return false
}
// Intersection determine whether two rectangles, r1 and r2, have one or more
// intersecting lines and produce a result, []Point, identifying the points
// of intersection
func Intersection(r1, r2 Rectangle) []Point {
order1 := r1.inOrder()
order2 := r2.inOrder()
@ -128,6 +144,10 @@ func Intersection(r1, r2 Rectangle) []Point {
return pts
}
// sumOfTri determines if the sum of the areas of the triangles created from
// point p to two neighboring vertices of a side of the Rectangle, r, add up
// to the same size as the Rectangle, r. This is one way to determine if
// a point is inside of a Rectangle.
func sumOfTri(r Rectangle, p Point) bool {
n1, n2 := r.neighbors(r.P1)
x1, x2 := Point{}, Point{}

View File

@ -2,6 +2,7 @@ package rect
import "math"
// Returns the size of the triangle formed by points p1, p2, and p3
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)
}