博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
golang 三维向量相关操作
阅读量:4607 次
发布时间:2019-06-09

本文共 3105 字,大约阅读时间需要 10 分钟。

package vectorimport (    "math"    "fmt")// 三维向量:(x,y,z)type Vector3 struct {    X float64    `json:"x"`    Y float64    `json:"y"`    Z float64    `json:"z"`}func (this *Vector3)Equal(v Vector3) bool {    return this.X == v.X && this.Y == v.Y && this.Z == v.Z}// 三维向量:设值func (this *Vector3)Set(x, y, z float64) {    this.X = x    this.Y = y    this.Z = z}// 三维向量:拷贝func (this *Vector3)Clone() Vector3 {    return NewVector3(this.X, this.Y, this.Z)}// 三维向量:加上// this = this + vfunc (this *Vector3)Add(v Vector3) {    this.X += v.X    this.Y += v.Y    this.Z += v.Z}// 三维向量:减去// this = this - vfunc (this *Vector3)Sub(v Vector3) {    this.X -= v.X    this.Y -= v.Y    this.Z -= v.Z}// 三维向量:数乘func (this *Vector3)Multiply(scalar float64) {    this.X *= scalar    this.Y *= scalar    this.Z *= scalar}func (this *Vector3)Divide(scalar float64) {    if scalar == 0 {        panic("分母不能为零!")    }    this.Multiply(1 / scalar)}// 三维向量:点积func (this *Vector3)Dot(v Vector3) float64 {    return this.X * v.X + this.Y * v.Y + this.Z * v.Z}// 三维向量:叉积func (this *Vector3)Cross(v Vector3) {    x, y, z := this.X, this.Y, this.Z    this.X = y * v.Z - z * v.Y;    this.Y = z * v.X - x * v.Z;    this.Z = x * v.Y - y * v.X;}// 三维向量:长度func (this *Vector3)Length() float64 {    return math.Sqrt(this.X * this.X + this.Y * this.Y + this.Z * this.Z)}// 三维向量:长度平方func (this *Vector3)LengthSq() float64 {    return this.X * this.X + this.Y * this.Y + this.Z * this.Z}// 三维向量:单位化func (this *Vector3)Normalize() {    this.Divide(this.Length())}// 返回:新向量func NewVector3(x, y, z float64) Vector3 {    return Vector3{X:x, Y:y, Z:z}}// 返回:零向量(0,0,0)func Zero3() Vector3 {    return Vector3{X:0, Y:0, Z:0}}// X 轴 单位向量func XAxis3() Vector3 {    return Vector3{X:1, Y:0, Z:0}}// Y 轴 单位向量func YAxis3() Vector3 {    return Vector3{X:0, Y:1, Z:0}}// Z 轴 单位向量func ZAxis3() Vector3 {    return Vector3{X:0, Y:0, Z:1}}func XYAxis3() Vector3 {    return Vector3{X:1, Y:1, Z:0}}func XZAxis3() Vector3 {    return Vector3{X:1, Y:0, Z:1}}func YZAxis3() Vector3 {    return Vector3{X:0, Y:1, Z:1}}func XYZAxis3() Vector3 {    return Vector3{X:1, Y:1, Z:1}}// 返回:a + b 向量func Add3(a, b Vector3) Vector3 {    return Vector3{X:a.X + b.X, Y:a.Y + b.Y, Z:a.Z + b.Z}}// 返回:a - b 向量func Sub3(a, b Vector3) Vector3 {    return Vector3{X:a.X - b.X, Y:a.Y - b.Y, Z:a.Z - b.Z}}// 返回:a X b 向量 (X 叉乘)func Cross3(a, b Vector3) Vector3 {    return Vector3{X:a.Y * b.Z - a.Z * b.Y, Y:a.Z * b.X - a.X * b.Z, Z:a.X * b.Y - a.Y * b.X}}func AddArray3(vs []Vector3, dv Vector3) []Vector3 {    for i,_ := range vs {        vs[i].Add(dv)    }    return vs}func Multiply3(v Vector3,scalars []float64) []Vector3 {    vs := []Vector3{}    for _,value := range scalars {        vector := v.Clone()        vector.Multiply(value)        vs = append(vs, vector)    }    return vs}// 返回:单位化向量func Normalize3(a Vector3) Vector3 {    b := a.Clone()    b.Normalize()    return b}//求两点间距离func GetDistance(a Vector3,b Vector3) float64{    return math.Sqrt(math.Pow(a.X - b.X, 2) + math.Pow(a.Y - b.Y, 2) + math.Pow(a.Z - b.Z, 2))}

 

转载于:https://www.cnblogs.com/zheng123/p/9648868.html

你可能感兴趣的文章
Android模拟器Genymotion安装向导
查看>>
USB
查看>>
碎碎叨叨
查看>>
The declared package..does not match the expected package..
查看>>
WPF之DataTemplate(转)
查看>>
sql的行转列(PIVOT)与列转行(UNPIVOT)(转)
查看>>
php判断是否是微信客户端的浏览器访问
查看>>
文件转换成字符串
查看>>
作业要求 20180925-6 四则运算试题生成
查看>>
【Vjudge】P1989Subpalindromes(线段树)
查看>>
ExtJs之Ext.util.TaskRunner
查看>>
Solved problems updating perl-XML-SAX-0.96-7.el6.noarch on CentOS 6
查看>>
redis为什么快
查看>>
【树状数组(二叉索引树)】轻院热身—candy、NYOJ-116士兵杀敌(二)
查看>>
116830
查看>>
c#,将pdf文件转换成图片文件。
查看>>
美国 100 名以后的大学还有去读研究生的必要吗?[无聊的时候看看]
查看>>
吴昊品游戏核心算法 Round 12(特别篇) —— 吴昊教你玩筷子游戏(计算几何)
查看>>
nginx的域名解析
查看>>
thymeleaf 拼接 超链接
查看>>