package main
import (
"fmt"
"math"
)
type point struct {
X float64
Y float64
}
// s = sqrt(p*(p-a)*(p-b)*(p-c)) (海伦公式),其中 p = (a+b+c)/2
//距离
func distance(a, b point) float64 {
return math.Sqrt((a.X-b.X)*(a.X-b.X) + (a.Y-b.Y)*(a.Y-b.Y))
}
//面积
func area(a, b, c point) float64 {
ab := distance(a, b)
ac := distance(a, c)
bc := distance(c, b)
p := (ab + ac + bc) / 2
return math.Sqrt(p * (p - ab) * (p - ac) * (p - bc))
}
//重心
func point_middle(a, b, c point) point {
return point{X: (a.X + b.X + c.X) / 3, Y: (a.Y + b.Y + c.Y) / 3}
}
//判断是否为钝角三角形
//钝角三角形特点是重心坐标在三角形外 与其它任何顶点组成的图形的面积>三角形面积
func is_obtuse(a, b, c point) bool {
area1 := area(a, b, c)
mPoint := point_middle(a, b, c)
s1 := area(mPoint, a, b)
s2 := area(mPoint, a, c)
s3 := area(mPoint, b, c)
if (s1 + s2 + s3) > area1 {
return true
} else {
return false
}
}
func main() {
a := point{0, 0}
b := point{-1, 1}
c := point{3, 0} //明显的钝角三角形坐标
fmt.Printf("is_obtuse %v \n", is_obtuse(a, b, c))
}
转载于:https://my.oschina.net/yang1992/blog/595457