一、 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. HTML文件编码

    为了防止中文乱码,一般在网页头文件中加入 <meta http-equiv="Content-Type" content="text/html; charset=u ...

  2. nexus 安装配置

    一.下载Nexus http://nexus.sonatype.org/downloads 我是用的版本是 nexus-2.11.4-01-bundle.tar.gz 每个版本的配置有些许差别. 二. ...

  3. java基础3 循环语句:While 循环语句、do while 循环语句、 for 循环语句 和 break、continue关键字

    一.While循环语句 1.格式 while(条件表达式){ 执行语句: } 2.要点 1,先判断后执行 2,循环次数不定 3,避免死循环 3.举例 题目1:输出0-100之间的所有数 class D ...

  4. linux下查看资源使用情况

    //查看占用内存最多的前K的程序ps aux | sort -k4nr | head -K //查看占用CPU最多的前K的程序 ps aux | sort -k3nr | head -K

  5. HDU 4725 The Shortest Path in Nya Graph(spfa+虚拟点建图)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4725 题目大意:有n层,n个点分布在这些层上,相邻层的点是可以联通的且距离为c,还有额外给出了m个条边 ...

  6. Java容器---Collection接口中的共有方法

    1.Collection 接口 (1)Collection的超级接口是Iterable (2)Collection常用的子对象有:Map.List.Set.Queue. 右图中实现黑框的ArrayLi ...

  7. MySQL的表管理

    首先,先选择数据库(极其特别重要,如果不选择,将默认为第一个数据库) mysql > use db_name; 查看所有表 mysql > show tables; 1.创建表 creat ...

  8. 第三方登陆微博、qq、微信

    源文:http://blog.csdn.net/tivonalh/article/details/60954373 假设是已经申请完成各平台开发者账号. 先来简单的,微博和QQ 微博: 引入微博JS ...

  9. loadrunner场景执行出现:Failed to Initialize. Reason: TimeOut

      问题1: Failed to Initialize. Reason: TimeOut LoadRunner的异常原因(Failed to Initialize. Reason: TimeOut) ...

  10. Ubuntu 如何更换阿里源

    #进入源地址 cd /etc/apt #备份源文件 sudo cp sources.list sources.list.bak #编辑 sudo vim /etc/apt/sources.list d ...