Alex Allen / vectr

vectr.cpp

Committer:
AlexAllen
Date:
2012-10-13
Revision:
0:55dbdc9614f8

File content as of revision 0:55dbdc9614f8:

/* Copyright (c) 2011 Alex Allen, MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
#include "vectr.h"
#include <cmath>
#include <iostream>
using namespace std;


vectr::vectr() 
{ 
    x=y=z=0; 
}

vectr::vectr(double num) 
{ 
    x=y=z=num; 
}

vectr::vectr(double a, double b)
{
    x = a;
    y = b;
    z = 0;
}

vectr::vectr(double a, double b, double c)
{
    x = a;
    y = b;
    z = c;
}

void vectr::set(double a, double b, double c)
{
    x = a;
    y = b;
    z = c;
}

void vectr::setx(double a)
{
    x = a;
}

void vectr::sety(double b)
{
    y = b;
}

void vectr::setz(double c)
{
    z = c;
}

double vectr::getx()
{
    return x;
}

double vectr::gety()
{
    return y;
}

double vectr::getz()
{
    return z;
}

double vectr::mag()
{
    return sqrt(x*x + y*y + z*z);
}

double vectr::mag2()
{
    return x*x + y*y + z*z;
}

vectr vectr::unit()
{
    double size = sqrt(x*x+y*y+z*z);
    vectr temp;

    temp.x = x/size;
    temp.y = y/size;
    temp.z = z/size;

    return temp;
}

vectr vectr::operator+(vectr ob2)
{
    vectr temp;

    temp.x = x + ob2.x;
    temp.y = y + ob2.y;
    temp.z = z + ob2.z;

    return temp;
}

vectr vectr::operator-(vectr ob2)
{
    vectr temp;

    temp.x = x - ob2.x;
    temp.y = y - ob2.y;
    temp.z = z - ob2.z;

    return temp;
}

double vectr::operator*(vectr ob2)
{
    return x*ob2.x + y*ob2.y + z*ob2.z;
}

vectr vectr::operator*(double num)
{
    vectr temp;

    temp.x = x * num;
    temp.y = y * num;
    temp.z = z * num;

    return temp;
}

vectr operator*(double num, vectr ob)
{
    vectr temp;

    temp.x = ob.x * num;
    temp.y = ob.y * num;
    temp.z = ob.z * num;

    return temp;
}

vectr vectr::operator%(vectr ob2)
{
    vectr temp;

    temp.x = y * ob2.z - z * ob2.y;
    temp.y = z * ob2.x - x * ob2.z;
    temp.z = x * ob2.y - y * ob2.x;

    return temp;
}

vectr vectr::operator/(double num)
{
    vectr temp;

    temp.x = x / num;
    temp.y = y / num;
    temp.z = z / num;

    return temp;
}

vectr operator/(double num, vectr ob)
{
    vectr temp;

    temp.x = num / ob.x;
    temp.y = num / ob.y;
    temp.z = num / ob.z;

    return temp;
}

double vectr::operator/(vectr ob2)
{
    ob2.x /= 1.0;
    ob2.y /= 1.0;
    ob2.z /= 1.0;

    return x*ob2.x + y*ob2.y + z*ob2.z;
}

vectr vectr::operator+=(vectr ob2)
{
    x += ob2.x;
    y += ob2.y;
    z += ob2.z;

    return *this;
}

vectr vectr::operator-=(vectr ob2)
{
    x -= ob2.x;
    y -= ob2.y;
    z -= ob2.z;

    return *this;
}

vectr vectr::operator*=(double num)
{
    x *= num;
    y *= num;
    z *= num;

    return *this;
}

vectr vectr::operator/=(double num)
{
    x /= num;
    y /= num;
    z /= num;

    return *this;
}

ostream &operator<<(ostream &stream, vectr ob)
{
    stream << "(" << ob.x << ", ";
    stream << ob.y << ", ";
    stream << ob.z << ")";

    return stream;
}