github.com/agnivade/pgm@v0.0.0-20210528073050-e2df0d9cb72d/rectangle.go (about) 1 package pgm 2 3 import ( 4 "math" 5 ) 6 7 type rectangle [4]point 8 9 func newRectangle(i, j, k, l point) rectangle { 10 return rectangle{i, j, k, l} 11 } 12 13 func (r *rectangle) getArea() float64 { 14 deltaXAB := r[0].x - r[1].x 15 deltaYAB := r[0].y - r[1].y 16 deltaXBC := r[1].x - r[2].x 17 deltaYBC := r[1].y - r[2].y 18 19 lengthAB := math.Sqrt((deltaXAB * deltaXAB) + (deltaYAB * deltaYAB)) 20 lengthBC := math.Sqrt((deltaXBC * deltaXBC) + (deltaYBC * deltaYBC)) 21 22 return lengthAB * lengthBC 23 } 24 25 func (r *rectangle) getHeight() float64 { 26 deltaXAB := r[0].x - r[1].x 27 deltaYAB := r[0].y - r[1].y 28 29 lengthAB := math.Sqrt((deltaXAB * deltaXAB) + (deltaYAB * deltaYAB)) 30 return lengthAB 31 } 32 33 func (r *rectangle) slopeAndIntercept() (slope, intercept float64) { 34 // We take the midpoint of two sides and draw a line between them. 35 p1 := point{x: (r[0].x + r[1].x) / 2, y: (r[0].y + r[1].y) / 2} 36 p2 := point{x: (r[2].x + r[3].x) / 2, y: (r[2].y + r[3].y) / 2} 37 38 slope = (p2.y - p1.y) / (p2.x - p1.x) 39 intercept = p2.y - slope*p2.x 40 return 41 }