一、 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. Linux命令参数处理 shell脚本函数getopts

    getopts 命令 用途 处理命令行参数,并校验有效选项. 语法 getopts 选项字符串 名称 [ 参数 ...] 描述 getopts 的设计目标是在循环中运行,每次执行循环,getopts ...

  2. Linux打补丁的一个简单例子

        前言 在做开发的过程中难免需要给内核及下载的一些源码打补丁或者说是升级,所以我们学习在Linux下使用diff制作补丁以及如何使用patch打补丁显得尤为重要. diff与patch命令介绍 ...

  3. 想弄一弄tensorflow,先弄numpy

    现在晚上凉快点了, 下班回家可以学会东东了.. 这次的书是一个印度人写的. 按着示例代码弄起先.. #!/usr/bin/env python # -*- coding: utf-8 -*- impo ...

  4. 【原创】Scrapyd 的 .net 客户端

    最近项目需要部署Scrapy爬虫,采用最简单的Scrapyd服务进行部署,基于.net core 进行了客户端的封装. 1)Scrapyd API文档:http://scrapyd.readthedo ...

  5. loadrunner 分用户日志

    loadrunner 分用户日志 loadrunner在run脚本时,模拟多用户并发场景下,通常需要分别关注每个用户的脚本执行日志,可以按照以下操作进行: 在“Run Load Tests”中选择需要 ...

  6. bzoj 1116

    思路:每个连通块都判是否有环. #include<bits/stdc++.h> #define LL long long #define fi first #define se secon ...

  7. 【转】Serializers 序列化组件

    https://www.cnblogs.com/MayDayTime/p/9890582.html 为什么要用序列化组件 当我们做前后端分离的项目~~我们前后端交互一般都选择JSON数据格式,JSON ...

  8. STM32使用定时器实现输入捕获

    输入捕获简介输入捕获模式可以用来测量脉冲宽度或者测量频率.STM32的定时器,除了TIM6和TIM7,其他定时器都有输入捕获功能. STM32的输入捕获,简单地说就是通过检测TIMx_CHx上的边沿信 ...

  9. JSP之登录验证码

    1.JSP页面中设置输入选项和验证码 <form action=login.do" method="post" > <div class="l ...

  10. C和指针之学习笔记(3)

    第8章 数组 1.数组与指针 数组名是一个个元素的地址. int  a[10];  int  b[10];  int  *c; (1) c = & a[0]; &a[0]表示一个指向数 ...