Advertisement
Guest User

Loc Faure-Lacroix

a guest
Mar 9th, 2009
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.00 KB | None | 0 0
  1. /* Vec3.h */
  2. #include <cmath>
  3.  
  4. class Vec3{
  5. private:
  6.     float elements[3];
  7. public:
  8.     Vec3(float x = 0, float y = 0, float z = 0);
  9.     ~Vec3();
  10.     Vec3 copy();
  11.  
  12.  
  13.     Vec3& set(float x, float y, float z);
  14.     Vec3& setX(float x);
  15.     Vec3& setY(float y);
  16.     Vec3& setZ(float z);
  17.  
  18.     Vec3 add(const Vec3& v1, int scale=1);
  19.     Vec3 substract(const Vec3& v1, int scale=1);
  20.     Vec3 scale(int scale);
  21.  
  22.     float dot(const Vec3& v2);
  23.     Vec3 cross(const Vec3& v2);
  24.  
  25.     Vec3& normalize();
  26.     float length();
  27.     float lengthSquared();
  28.  
  29.     bool equals(const Vec3& v2, float epsilon);
  30.     float operator[](int pos) const;
  31.  
  32.     Vec3 operator+(const Vec3& v2);
  33.     Vec3 operator-(const Vec3& v2);
  34.  
  35.     Vec3& operator+=(const Vec3& v2);
  36.     Vec3& operator-=(const Vec3& v2);
  37. }
  38.  
  39. /* Vec3.cpp */
  40. #include "Vec3.h"
  41. #include <iostream>
  42.  
  43. Vec3::Vec3(float x, float y, float z){
  44.     this->elements[0] = x;
  45.     this->elements[1] = y;
  46.     this->elements[2] = z;
  47.     //std::cout << "Created Vec3 {" << this->elements[0] << ", " << this->elements[1] << ", " << this->elements[2] << "}\n";
  48. }
  49.  
  50. Vec3::~Vec3(){
  51.     //std::cout << "Destroyed Vec3 {" << this->elements[0] << ", " << this->elements[1] << ", " << this->elements[2] << "}\n";
  52. }
  53.  
  54. Vec3 Vec3::copy(){
  55.     Vec3 copy(
  56.         this->elements[0],
  57.         this->elements[1]
  58.         this->elements[2]
  59.         );
  60.     return copy;       
  61. }
  62.  
  63. bool Vec3::equals(const Vec3& v2, float epsilon){
  64.     return ((std::abs(this->elements[0] - v2[0]) < epsilon) &&
  65.             (std::abs(this->elements[1] - v2[1]) < epsilon) &&
  66.             (std::abs(this->elements[2] - v2[2]) < epsilon));  
  67. }
  68.  
  69.  
  70. float Vec3::length(){
  71.     return std::sqrt(this->lengthSquared());
  72. }
  73.  
  74. float Vec3::lengthSquared(){
  75.     return (
  76.         this->elements[0]*this->elements[0] +
  77.         this->elements[1]*this->elements[1] +
  78.         this->elements[2]*this->elements[2]
  79.         );
  80. }
  81.  
  82. Vec3& Vec3::normalize(){
  83.     float mag = this->length();
  84.     if(mag){
  85.         float multiplier = 1.0f/mag;
  86.         this->elements[0] *= multiplier;
  87.         this->elements[1] *= multiplier;
  88.         this->elements[2] *= multiplier;
  89.     }
  90.     return *this;
  91. }
  92.  
  93. Vec3 Vec3::cross(const Vec3& v2){
  94.     Vec3 cross(
  95.         (this->elements[1] * v2[2] - this->elements[2] * v2[1]),
  96.         (this->elements[2] * v2[0] - this->elements[0] * v2[2]),
  97.         (this->elements[0] * v2[1] - this->elements[1] * v2[0])
  98.     );
  99.     return cross;
  100. }
  101.  
  102. Vec3 Vec3::substract(const Vec3& v1, int scale){
  103.     Vec3 sub(
  104.         (this->elements[0] - v1[0]*scale),
  105.         (this->elements[1] - v1[1]*scale),
  106.         (this->elements[2] - v1[2]*scale)
  107.         );
  108.     return sub;
  109. }
  110.  
  111. Vec3& Vec3::set(float x, float y, float z){
  112.     this->elements[0] = x;
  113.     this->elements[1] = y;
  114.     this->elements[2] = z;
  115.     return *this;
  116. }
  117.  
  118. Vec3& Vec3::setX(float x){
  119.     this->elements[0] = x;
  120.     return *this;
  121. }
  122.  
  123. Vec3& Vec3::setY(float y){
  124.     this->elements[1] = y;
  125.     return *this;
  126. }
  127.  
  128. Vec3& Vec3::setZ(float z){
  129.     this->elements[2] = z;
  130.     return *this;
  131. }
  132.  
  133. float Vec3::dot(const Vec3& v2){
  134.     return (this->elements[0]*v2[0] +
  135.         this->elements[1]*v2[1] +
  136.         this->elements[2]*v2[2]
  137.         );
  138. }
  139.  
  140. Vec3 Vec3::scale(int scale){
  141.     Vec3 scaled(
  142.         (this->elements[0]*scale),
  143.         (this->elements[1]*scale),
  144.         (this->elements[2]*scale)
  145.         );
  146.     return scaled;
  147. }
  148.  
  149. Vec3 Vec3::add(const Vec3& v1, int scale){
  150.     Vec3 sum(
  151.         (this->elements[0] + v1[0]*scale),
  152.         (this->elements[1] + v1[1]*scale),
  153.         (this->elements[2] + v1[2]*scale)
  154.         );
  155.     return sum;
  156. }
  157.  
  158.  
  159. float Vec3::operator[](int pos) const{
  160.     return elements[pos];
  161. }
  162.  
  163. Vec3 Vec3::operator+(const Vec3& v2) {
  164.     return this->add(v2);
  165. }
  166.  
  167. Vec3 Vec3::operator-(const Vec3& v2) {
  168.     return this->substract(v2);
  169. }
  170.  
  171. Vec3& Vec3::operator+=(const Vec3& v2) {
  172.         this->elements[0] += v2[0];
  173.         this->elements[1] += v2[1];
  174.         this->elements[2] += v2[2];
  175.         return *this;
  176. }
  177.  
  178. Vec3& Vec3::operator-=(const Vec3& v2) {
  179.         this->elements[0] -= v2[0];
  180.         this->elements[1] -= v2[1];
  181.         this->elements[2] -= v2[2];
  182.         return *this;
  183. }
  184.  
  185. /*
  186. Please contact me if you find any way to improve this code at  (Loïc Faure-Lacroix <loicfl@gmail.com>)
  187. Have fun!!
  188. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement