golang 三维向量相关操作
package vector import (
"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 + v
func (this *Vector3)Add(v Vector3) {
this.X += v.X
this.Y += v.Y
this.Z += v.Z
} // 三维向量:减去
// this = this - v
func (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))
}
golang 三维向量相关操作的更多相关文章
- php对二维数组进行相关操作(排序、转换、去空白等)
php对二维数组进行相关操作(排序.转换.去空白等) 投稿:lijiao 字体:[增加 减小] 类型:转载 时间:2015-11-04 这篇文章主要介绍了php对二维数组进行相关操作,包括php对 ...
- Scala学习(三)----数组相关操作
数组相关操作 摘要: 本篇主要学习如何在Scala中操作数组.Java和C++程序员通常会选用数组或近似的结构(比如数组列表或向量)来收集一组元素.在Scala中,我们的选择更多,不过现在我们先假定不 ...
- golang中的文件操作
一.文件的基本介绍 文件是数据源(保存数据的地方)的一种,比如经常使用的word文档,txt文件,excel文件都是文件.文件最主要的作用就是保存数据,它既可以保存一张图片,也可以保持视频,声音等等. ...
- 关于golang中IO相关的Buffer类浅析
io重要的接口 在介绍buffer之前,先来认识两个重要的接口,如下边所示: type Reader interface { Read(p []byte) (n int, err error) } t ...
- [阿里DIN]从模型源码梳理TensorFlow的形状相关操作
[阿里DIN]从模型源码梳理TensorFlow的形状相关操作 目录 [阿里DIN]从模型源码梳理TensorFlow的形状相关操作 0x00 摘要 0x01 reduce_sum 1.1 reduc ...
- 从零自学Hadoop(20):HBase数据模型相关操作上
阅读目录 序 介绍 命名空间 表 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 序 ...
- 从零自学Hadoop(21):HBase数据模型相关操作下
阅读目录 序 变量 数据模型操作 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephisto)写的,SourceLink 序 ...
- 理解CSV文件以及ABAP中的相关操作
在很多ABAP开发中,我们使用CSV文件,有时候,关于CSV文件本身的一些问题使人迷惑.它仅仅是一种被逗号分割的文本文档吗? 让我们先来看看接下来可能要处理的几个相关组件的词汇的语义. Separat ...
- Liunx下的有关于tomcat的相关操作 && Liunx 常用指令
先记录以下liunx下的有关于tomcat的相关操作 查看tomcat进程: ps-ef|grep java (回车) 停止tomcat进程: kill -9 PID (进程号如77447) (回车) ...
随机推荐
- 使用DTK创建模糊背景窗口并自定义阴影效果
DTK是deepin开发的基于Qt的开发套件,提供了大量的具有独特风格的美化控件,也提供了很多非常方便的API,下边我们用DTK实现一个模糊窗口,并设置其阴影效果. 使用场景 一切需要模糊窗口作为美化 ...
- In_array()函数弱比较
0x01 定义 (PHP 4, PHP 5, PHP 7) in_array - 检查数组中是否存在某个值 说明 in_array ( mixed $needle , array $haystack ...
- Vue3(四)从jQuery 转到 Vue工程化 的捷径
不会 webpack 还想学 vue 工程化开发 的福音 熟悉jQuery开发的,学习vue的简单使用是没用啥问题的,但是学习vue的工程化开发方式,往往会遇到各种问题,比如: webpack.nod ...
- vue watcher errors
vue watcher errors Error in callback for watcher TypeError: Cannot set property of undefined" n ...
- postman skills
postman skills variables https://learning.postman.com/docs/sending-requests/variables/ https://learn ...
- leetcode & vscode
leetcode & vscode vscode-leetcode https://marketplace.visualstudio.com/items?itemName=LeetCode.v ...
- ng 基础
文档 组件的工作只管用户体验,而不用顾及其它. 它应该提供用于数据绑定的属性和方法,以便作为视图和应用逻辑的中介者 组件应该把诸如从服务器获取数据.验证用户输入或直接往控制台中写日志等工作委托给各种服 ...
- Android Kotlin 数据驱动模板
Android开发人员文档: 数据绑定入门 数据绑定库 生成的绑定类 布局和绑定表达式 1. 搭建环境build.gradle:app apply plugin: "kotlin-kapt& ...
- js滚轮事件兼容写法
/** * 简易的事件添加方法 */ define(function(require, exports, module) { exports.addEvent = (function(window, ...
- C++算法代码——求数列[coci2014/2015 contest #1]
题目来自:http://218.5.5.242:9018/JudgeOnline/problem.php?id=1815 题目描述 Mirko在数学课上以一种有趣的方式操作数列,首先,他写下一个数列A ...