/*
Package triangle determines if a triangle is equilateral, isosceles or scalene.
*/
package triangle
import "math"
// Kind is a the type of the triangle
type Kind int
// Triangle types
const (
NaT Kind = iota // not a triangle
Equ // equilateral
Iso // isosceles
Sca // scalene
)
// KindFromSides reads the length of each side of a triangle and returns its Kind.
func KindFromSides(a, b, c float64) (k Kind) {
// XXX check for degenerate
switch {
case a <= 0 || b <= 0 || c <= 0:
return NaT
case a+b < c || a+c < b || b+c < a:
// Because of < instead of <= degenerate is accepted as a triangle
return NaT
case math.IsNaN(a) || math.IsNaN(b) || math.IsNaN(c):
return NaT
case math.IsInf(a, 0) || math.IsInf(b, 0) || math.IsInf(c, 0):
return NaT
case a == b && a == c:
return Equ
case a == b || b == c || a == c:
// only two sides can be equal, as we checked for all 3 side in the previous case
return Iso
default:
return Sca
}
}