一、 c#实现
/*
Vector3 Definition
Created by taotao man on 2016-4-12
brief:封装三位向量类Vector3
// 修改记录:
date:
add SetA()
Change GetA();
*/ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ceshi
{
public class Vector3
{
private float x;
private float y;
private float z;
private const float E = 0.0000001f; public float X
{
set { x = value; }
get { return x; }
} public float Y
{
set { y = value; }
get { return y; }
} public float Z
{
set { z = value; }
get { return z; }
} public Vector3(float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
} public Vector3(Vector3 vct)
{
this.x = vct.x;
this.y = vct.y;
this.z = vct.z;
} //向量加法
public static Vector3 operator +(Vector3 a, Vector3 b)
{
Vector3 result = new Vector3(a.x + b.x , a.y + b.y, a.z +b.z);
return result;
} //向量减法
public static Vector3 operator -(Vector3 a, Vector3 b)
{
Vector3 result = new Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
return result;
} //向量除以一个数
public static Vector3 operator /(Vector3 a, float b)
{
if (b != 0)
{
return new Vector3(a.x / b, a.y / b, a.z / b);
}
else
{
return new Vector3(0, 0, 0);
}
} // 左乘一个数
public static Vector3 operator *(float a, Vector3 b)
{
return new Vector3(a * b.x, a * b.y, a * b.z);
} // 右乘一个数
public static Vector3 operator *(Vector3 a, float b)
{
return new Vector3(a.x * b, a.y * b, a.z * b);
} // 向量的点乘
public static float operator *(Vector3 a, Vector3 b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
} // 判断两个向量是否相等
public static bool operator ==(Vector3 a, Vector3 b)
{
if (Math.Abs(a.x - b.x) < E && Math.Abs(a.y - b.y) < E && Math.Abs(a.z - b.z) < E)
{
return true;
}
else
return false;
} // 判断两个向量不等
public static bool operator !=(Vector3 a, Vector3 b)
{
return !(a == b);
} public override bool Equals(object obj)
{
return base.Equals(obj);
} public override int GetHashCode()
{
return base.GetHashCode();
} public override string ToString()
{
return base.ToString();
} // 向量叉积
public static Vector3 Cross(Vector3 a, Vector3 b)
{
return new Vector3(a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x);
} //向量的模
public static float Magnitude(Vector3 a)
{
return (float)Math.Sqrt(a.x * a.x + a.y * a.y + a.z * a.z);
} // 单位化向量 public static Vector3 Normalize(Vector3 a)
{
float magnitude = Magnitude(a);
return new Vector3(a.x / magnitude, a.y / magnitude, a.z / magnitude);
}
}
}

二、c++实现

转载自:3D数学基础图形与游戏开发

#include <math.h>

class Vector3
{
public:
float x, y, z; // 默认构造函数
Vector3(){}
// 复制构造函数
Vector3(const Vector3 &a) : x(a.x), y(a.y), z(a.z){}
//
// 带参数的构造函数,用三个值完成初始化
Vector3(float nx, float ny, float nz) : x(nx), y(ny), z(nz){}
// 标准对象操作
// 重载运算符,并返回引用,以实现左值
Vector3 &operator = (const Vector3 &a)
{
x = a.x; y = a.y; z = a.z;
return *this;
}
//
//重载“==”操作符
bool operator == (const Vector3 &a) const
{
return x == a.x && y == a.y && z == a.z;
}
bool operator != (const Vector3 &a) const
{
return x != a.x || y != a.y || z != a.z;
} //向量运算
// 置为零向量
void zero()
{
x = y = z =0.0f;
}
// 重载一元“-”运算符
Vector3 operator -()const
{
return Vector3(-x, -y , -z);
}
//重载二元“+”和“-”运算符
Vector3 operator +(const Vector3 &a) const
{
return Vector3(x + a.x, y + a.y, z + a.z);
}
Vector3 operator -(const Vector3 &a) const
{
return Vector3(x - a.x, y - a.y, z - a.z);
}
// 与标量的乘除法
Vector3 operator * (float a) const
{
return Vector3(x * a, y * a, z * a);
}
Vector3 operator / (float a) const
{
float oneOverA = 1.0f / a;
return Vector3(x * oneOverA, y * oneOverA, z * oneOverA);
}
// 重载自反运算符
Vector3 &operator += (const Vector3 &a)
{
x += a.x; y += a.y; z += a.z;
return *this;
}
Vector3 &operator -= (const Vector3 &a)
{
x -= a.x; y -= a.y; z -= a.z;
return *this;
}
Vector3 &operator *= (float a)
{
x *= a; y *= a; z *= a;
return * this;
}
Vector3 &operator /= (float a)
{
float oneOverA = 1.0f / a;
x *= oneOverA; y *= oneOverA; z *= oneOverA;
return *this;
}
// 向量标准化
void normalize()
{
float magSq = x * x + y * y + z * z;
if(magSq > 0.0f)
{
float oneOverMag = 1.0f / sqrt(magSq);
x *= oneOverMag;
y *= oneOverMag;
z *= oneOverMag;
}
}
// 向量点乘,重载标准的乘法运算符
float operator *(const Vector3 &a) const
{
return x * a.x + y * a.y + z * a.z;
}
}; // 非成员函数
// 求向量模
inline float vectorMag(const Vector3 &a)
{
return sqrt(a.x * a.x + a.y * a.y + a.z * a.z);
}
//计算两个向量的叉乘
inline Vector3 crossProduct(const Vector3 &a, const Vector3 &b)
{
return Vector3
(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x);
}
//
//实现标量左乘
inline Vector3 operator *(float k, const Vector3 &v)
{
return Vector3(k * v.x, k * v.y, k * v.z);
}
// 计算两点间的距离
inline float distance(const Vector3 &a, const Vector3 &b)
{
float dx = a.x - b.x;
float dy = a.y - b.y;
float dz = a.x - b.z;
return sqrt(dx * dx + dy * dy + dz * dz);
}
//提供一个全局零向量
extern const Vector3 kZeroVector;
 

c#封装三维向量,另外也看了下别人的C++封装的更多相关文章

  1. golang 三维向量相关操作

    package vector import ( "math" "fmt" )// 三维向量:(x,y,z) type Vector3 struct { X fl ...

  2. 看了下opengl相关的资料,踩了一个坑,记录一下

    2019/03/10 下午看了下关于opengl的资料,然后把敲了下代码,然后程序报错了.代码如下: #include <glad/glad.h> #include <GLFW/gl ...

  3. Vue 可输入可下拉组件的封装

    由于业务需要,需要一个可输入也可下拉的组件,看了iview没有现成的组件用,就自己封装了个小组件~~ 组件input-select.vue代码: <template> <div cl ...

  4. 真机下, 如何在File Explorer里看data下的数据?

    首先手机得Root , 你如果想单个单个的看, root explorer可以设置Permission Other下的两个权限点上就ok了. 如果想看到所有的, 即子目录也可以看到, 只需要adb r ...

  5. mui的上拉加载更多 下拉刷新 自己封装的demo

    ----------------------------------------------- 这是一个非常呆萌的程序妹子,深夜码的丑代码------------------------------- ...

  6. 硬盘上的一些算法小题目||and今天看了下林锐的书以及gdb调试 及一些变成算法小题目

    gdb调试:观察点,断点,事件捕捉点.step 进入函数,next 跳过函数,until 跳出循环,finish 结束函数 林锐:书后试题 & c++的对象模型图 看了二叉树的非递归遍历, 链 ...

  7. Android 下的 SQLite 操作封装 —— DatabaseUtil

    看到别人写的代码不错,对自己目前的开发很有用,所以转载一下,希望也能帮助到其他人: 1.DatabaseUtil.java(封装的类) package com.dbexample; import an ...

  8. 【JavaScript框架封装】实现一个类似于JQuery的CSS样式框架的封装

    // CSS 样式框架 (function (xframe) { // 需要参与链式访问的(必须使用prototype的方式来给对象扩充方法)[只要是需要使用到this获取到的元素集合这个变量的时候, ...

  9. uni-app 环境配置,uni.request封装,接口配置,全局配置,接口调用的封装

    1.环境配置 (可参考uni-官网的环境配置) common文件夹下新建config.js let url_config = "" if(process.env.NODE_ENV ...

随机推荐

  1. [转]Java中堆和栈创建对象的区别

    转载自http://blog.csdn.net/hbhhww/article/details/8152838 栈与堆都是Java用来在Ram中存放数据的地方.与C++不同,Java自动管理栈和堆,程序 ...

  2. JavaScript(获取或设置html元素的宽,高,坐标),确定和判断鼠标是否在元素内部,二级导航菜单鼠标离开样式问题解决

    设置: document.getElementById('id').style.width=value    document.getElementById('id').style.height=va ...

  3. python定制类(1):__getitem__和slice切片

    python定制类(1):__getitem__和slice切片 1.__getitem__的简单用法: 当一个类中定义了__getitem__方法,那么它的实例对象便拥有了通过下标来索引的能力. c ...

  4. .NET 社区汇总

    英文社区: 名称:MSDN 地址:http://msdn.microsoft.com/zh-cn/default.aspx 描述:这个网站是大家学.Net的初始网站,也是.net方面官方和权威的资料, ...

  5. Void运算符 与 undefined类型

    void 运算符 对给定的表达式进行求值,然后返回 undefined. 何为求值,就是执行之后的表达式. 我们最常见的就是 <a href="javascript: void(0)& ...

  6. 第9天-BOM和DOM

    什么是DOM 文档对象模型(Document Object Model),DOM作用:可以去修改网页内容.样式.结构. 每个浏览器都会把html文档解析成dom树,就可以用相关方法和属性操作网页元素 ...

  7. 洗牌问题 FZU - 1062 (传说中的思路题,hhh)

    设2n张牌分别标记为1, 2, -, n, n+1, -, 2n,初始时这2n张牌按其标号从小到大排列.经一次洗牌后,原来的排列顺序变成n+1, 1, n+2, 2, -, 2n, n.即前n张牌被放 ...

  8. Prime Number CodeForces - 359C (属于是数论)

    Simon has a prime number x and an array of non-negative integers a1, a2, ..., an. Simon loves fracti ...

  9. Vue 2.0学习(六)内置指令

    基本指令 1.v-cloak v-cloak不需要表达式,它会在Vue实例结束编译时从绑定的HTML元素上移除,经常和CSS的display:none配合使用. <div id="ap ...

  10. Codeforces 30 E. Tricky and Cleve Password

    \(>Codeforces \space 30\ E. Tricky\ and\ Cleve\ Password<\) 题目大意 : 给出一个串 \(S\),让你找出 \(A, B, C\ ...