一、 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. Redis学习-redis概述

    最近刚刚接触了redis技术,对此有一些了解,这是简单做一点总结. Redis简介 首先,简单了解一下NoSQL(Not only sql),不要错误的理解为:没有SQL,而是不仅仅是SQL.NoSQ ...

  2. 在JAVA中生成RSA秘钥对实现SSH互信

    https://blog.csdn.net/u014196729/article/details/51496262 https://blog.csdn.net/u013066244/article/d ...

  3. 写在Web考试后的一点小总结

    在实验室折腾附加题折腾了一个多钟没做出来……晚上回到宿舍决定再试一试,按原来的思路居然行了,目测在实验室的时候什么地方打错字了吧(心在流血) 实现晃过元素后出现跟随鼠标的悬浮窗,只有几行代码给我折腾了 ...

  4. Qt5.4 webview 不能打开网址

    在使用Qwebview浏览器时不能打开网络地址,并报下面的错误 Starting E:\WorkSpace\QtWorkSpace\build-webTest-Desktop_Qt_5_4_2_MSV ...

  5. 【LOJ】#2016. 「SCOI2016」美味

    题解 做了一下SCOI2015,于是决定搬运SCOI2016= v = 如果没有加法,我们可以向左向右节点查找 每个总权值是2^18 - 1,然后左右分,那么每次是一个完整的节点 如果有了加法,那么我 ...

  6. Codeforces Round #490 (Div. 3) F - Cards and Joy

    F - Cards and Joy 思路:比较容易想到dp,直接dp感觉有点难,我们发现对于每一种数字要处理的情况都相同就是有 i 张牌 要给 j 个人分, 那么我们定义dp[ i ][ j ]表示 ...

  7. 使用自己的域名解析 cnblogs 博客

    使用自己的域名解析 cnblogs 博客(博客园) 1.实现原理 用户访问 -> 阿里云解析 -> github page 跳转 -> 真实的博客地址 2.创建 github pag ...

  8. 【WPF】城市级联(XmlDataProvider)

    首先在绑定的时候进行转换: public class RegionConverter : IValueConverter { public object Convert(object value, T ...

  9. ArrayBuffer对象、TypedArray视图和DataView视图

    转:http://es6.ruanyifeng.com/#docs/arraybuffer ArrayBuffer ArrayBuffer 对象 TypedArray 视图 复合视图 DataView ...

  10. 2017-2018-1 20179202《Linux内核原理与分析》第六周作业

    一.系统调用实验(下): 1.编辑 menu 中的 text.c 文件,给MenuOS增加 rename 和 rename_asm 命令: make rootf 打开 menu 镜像,可以看到Menu ...