Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ===========
- // objects.h
- // ===========
- #ifndef OBJECTS_H_INCLUDED
- #define OBJECTS_H_INCLUDED
- #include <iostream>
- using namespace std;
- class Vector
- {
- public:
- double x, y, z, d = 0;
- Vector (const double& = 0, const double& = 0, const double& = 0);
- bool operator == (const Vector&);
- Vector operator + (const Vector&);
- Vector operator - (const Vector&);
- Vector operator * (const double&);
- Vector operator / (const double&);
- Vector operator ^ (const Vector&);
- double operator * (const Vector&);
- };
- class Plane
- {
- public:
- Vector a, b, c, n;
- Plane (const Vector&, const Vector&, const Vector&);
- private:
- void getNormal ();
- };
- ostream& operator << (ostream&, const Vector&);
- Vector norm (Vector&);
- #endif // OBJECTS_H_INCLUDED
- // =============
- // objects.cpp
- // =============
- #include <cmath>
- #include <iostream>
- #include "objects.h"
- using namespace std;
- // With a Vector type, you can initialize, compare, add and subtract
- // other Vectors, multiply and divide by scalars, do a cross product (^)
- // and dot product (*) between two Vectors, normalize a Vector, and
- // print out a Vector directly to the console.
- Vector::Vector (const double& a, const double& b, const double& c) {
- x = a; y = b; z = c; }
- bool Vector::operator == (const Vector& param) {
- return ((x == param.x) && (y == param.y) && (z == param.z)); }
- ostream& operator << (ostream& out, const Vector& param) {
- return out << "(" << param.x << ", " << param.y << ", " << param.z << ")"; }
- Vector Vector::operator + (const Vector& param) {
- return Vector (x + param.x, y + param.y, z + param.z); }
- Vector Vector::operator - (const Vector& param) {
- return Vector (x - param.x, y - param.y, z - param.z); }
- Vector Vector::operator * (const double& param) {
- return Vector (x * param, y * param, z * param); }
- Vector Vector::operator / (const double& param) {
- return Vector (x / param, y / param, z / param); }
- double Vector::operator * (const Vector& param) {
- return (x * param.x) + (y * param.y) + (z * param.z); }
- Vector Vector::operator ^ (const Vector& param)
- {
- Vector temp;
- temp.x = (y * param.z) - (z * param.y);
- temp.y = (z * param.x) - (x * param.z);
- temp.z = (x * param.y) - (y * param.x);
- return temp;
- }
- Vector norm (Vector& param) {
- return param / sqrt(param * param); }
- // A Plane type holds the Vectors of three points that make up
- // the Plane. It then calculates its normal and assigns that to d.
- Plane::Plane (const Vector& d, const Vector& e, const Vector& f) {
- a = d; b = e; c = f; getNormal (); }
- void Plane::getNormal ()
- {
- Vector temp =
- (
- Vector (b.x - a.x, b.y - a.y, b.z - a.z) ^
- Vector (c.x - b.x, c.y - b.y, c.z - b.z)
- );
- n = norm(temp);
- n.d = n * a;
- }
- // ==========
- // main.cpp
- // ==========
- #include <iostream>
- #include "objects.h"
- using namespace std;
- // A set of planes used for debugging. They come from a VMF file.
- Plane plane[7] =
- {
- Plane ({-801, 220, -192}, {-568, -460, -192}, {184, -460, -192}),
- Plane ({-192, 640, 576}, {417, 220, 576}, {184, -460, 576}),
- Plane ({-192, 640, -192}, {417, 220, -192}, {417, 220, 576}),
- Plane ({417, 220, -192}, {184, -460, -192}, {184, -460, 576}),
- Plane ({184, -460, -192}, {-568, -460, -192}, {-568, -460, 576}),
- Plane ({-568, -460, -192}, {-801, 220, -192}, {-801, 220, 576}),
- Plane ({-801, 220, -192}, {-192, 640, -192}, {-192, 640, 576})
- };
- int main ()
- {
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement