Advertisement
KeinMitleid

VMF Base

Jun 29th, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.49 KB | None | 0 0
  1. // ===========
  2. // objects.h
  3. // ===========
  4.  
  5. #ifndef OBJECTS_H_INCLUDED
  6. #define OBJECTS_H_INCLUDED
  7.  
  8. #include <iostream>
  9. using namespace std;
  10.  
  11. class Vector
  12. {
  13. public:
  14.     double x, y, z, d = 0;
  15.     Vector (const double& = 0, const double& = 0, const double& = 0);
  16.     bool operator == (const Vector&);
  17.     Vector operator + (const Vector&);
  18.     Vector operator - (const Vector&);
  19.     Vector operator * (const double&);
  20.     Vector operator / (const double&);
  21.     Vector operator ^ (const Vector&);
  22.     double operator * (const Vector&);
  23. };
  24.  
  25. class Plane
  26. {
  27. public:
  28.     Vector a, b, c, n;
  29.     Plane (const Vector&, const Vector&, const Vector&);
  30.  
  31. private:
  32.     void getNormal ();
  33. };
  34.  
  35. ostream& operator << (ostream&, const Vector&);
  36. Vector norm (Vector&);
  37.  
  38. #endif // OBJECTS_H_INCLUDED
  39.  
  40.  
  41.  
  42.  
  43.  
  44. // =============
  45. // objects.cpp
  46. // =============
  47.  
  48. #include <cmath>
  49. #include <iostream>
  50. #include "objects.h"
  51. using namespace std;
  52.  
  53. // With a Vector type, you can initialize, compare, add and subtract
  54. // other Vectors, multiply and divide by scalars, do a cross product (^)
  55. // and dot product (*) between two Vectors, normalize a Vector, and
  56. // print out a Vector directly to the console.
  57.  
  58. Vector::Vector (const double& a, const double& b, const double& c) {
  59.     x = a; y = b; z = c; }
  60.  
  61. bool Vector::operator == (const Vector& param) {
  62.     return ((x == param.x) && (y == param.y) && (z == param.z)); }
  63.  
  64. ostream& operator << (ostream& out, const Vector& param) {
  65.     return out << "(" << param.x << ", " << param.y << ", " << param.z << ")"; }
  66.  
  67. Vector Vector::operator + (const Vector& param) {
  68.     return Vector (x + param.x, y + param.y, z + param.z); }
  69.  
  70. Vector Vector::operator - (const Vector& param) {
  71.     return Vector (x - param.x, y - param.y, z - param.z); }
  72.  
  73. Vector Vector::operator * (const double& param) {
  74.     return Vector (x * param, y * param, z * param); }
  75.  
  76. Vector Vector::operator / (const double& param) {
  77.     return Vector (x / param, y / param, z / param); }
  78.  
  79. double Vector::operator * (const Vector& param) {
  80.         return (x * param.x) + (y * param.y) + (z * param.z); }
  81.  
  82. Vector Vector::operator ^ (const Vector& param)
  83. {
  84.     Vector temp;
  85.     temp.x = (y * param.z) - (z * param.y);
  86.     temp.y = (z * param.x) - (x * param.z);
  87.     temp.z = (x * param.y) - (y * param.x);
  88.     return temp;
  89. }
  90.  
  91. Vector norm (Vector& param) {
  92.         return param / sqrt(param * param); }
  93.  
  94. // A Plane type holds the Vectors of three points that make up
  95. // the Plane. It then calculates its normal and assigns that to d.
  96.  
  97. Plane::Plane (const Vector& d, const Vector& e, const Vector& f) {
  98.     a = d; b = e; c = f; getNormal (); }
  99.  
  100. void Plane::getNormal ()
  101. {
  102.     Vector temp =
  103.     (
  104.                 Vector (b.x - a.x, b.y - a.y, b.z - a.z) ^
  105.                 Vector (c.x - b.x, c.y - b.y, c.z - b.z)
  106.         );
  107.  
  108.     n = norm(temp);
  109.     n.d = n * a;
  110. }
  111.  
  112.  
  113.  
  114.  
  115.  
  116. // ==========
  117. // main.cpp
  118. // ==========
  119.  
  120. #include <iostream>
  121. #include "objects.h"
  122. using namespace std;
  123.  
  124. // A set of planes used for debugging. They come from a VMF file.
  125.  
  126. Plane plane[7] =
  127. {
  128.     Plane ({-801, 220, -192}, {-568, -460, -192}, {184, -460, -192}),
  129.     Plane ({-192, 640, 576}, {417, 220, 576}, {184, -460, 576}),
  130.     Plane ({-192, 640, -192}, {417, 220, -192}, {417, 220, 576}),
  131.     Plane ({417, 220, -192}, {184, -460, -192}, {184, -460, 576}),
  132.     Plane ({184, -460, -192}, {-568, -460, -192}, {-568, -460, 576}),
  133.     Plane ({-568, -460, -192}, {-801, 220, -192}, {-801, 220, 576}),
  134.     Plane ({-801, 220, -192}, {-192, 640, -192}, {-192, 640, 576})
  135. };
  136.  
  137. int main ()
  138. {
  139.         return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement