golang實現已知三角形三點坐標,求三角形面積


代碼如下:

func GetTriangleAreaByVector(x vector.Vector3,y vector.Vector3,z vector.Vector3) float64 {
    //根據三角形三個點坐標求面積
    //先算三角形三個邊的長度
    a := vector.GetDistance(x,y)
    b := vector.GetDistance(x,z)
    c := vector.GetDistance(y,z)
    s := (a + b + c) / 2
    area := math.Sqrt(s*(s-a)*(s-b)*(s - c))
    return area
}

//求兩點間距離
func GetDistance(a Vector3,b Vector3) float64{
if a==b {
return 0
}else {
return math.Sqrt(math.Pow(a.X - b.X, 2) + math.Pow(a.Y - b.Y, 2) + math.Pow(a.Z - b.Z, 2))
}
}

type Vector3 struct {
X float64 `json:"x"`
Y float64 `json:"y"`
Z float64 `json:"z"`
}
 

此函數的傳入參數是三角形三個點的坐標。輸出三角形面積

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM