#ifndef VECTOR_H
#define VECTOR_H
#include "point.h"
namespace MyOGLProg2
{
const double M_PI = 3.14159265358979323846;
const double M_HALF_PI = 1.57079632679489661923;
const double RAD = (M_PI/180.0);
const double PIOVER2 = (3.14159265358979323846 / 2);
#define DEG2RAD(x) ((x)*M_PI/180.0)
#define RAD2DEG(x) ((x)*180.0/M_PI)
}
class Vector
{
public:
Vector(void);
Vector(float x, float y, float z);
Vector(Position& start, Position& end);
~Vector(void){;}
float getMagnitude(void) const;
void setMagnitude(const float m);
Vector setX(float newX); //set parameters to specific value
Vector setY(float newY);
Vector setZ(float newZ);
float getAngleBetween2(const Vector& second);
bool checkDirection(const Vector& other); // check if vectors point the same direction
void normalise(void);
Vector addTo(const Vector &other) const; //adding and substraction with use of functions
Vector addToAndDraw(const Vector &other, bool showSteps, float startX, float startY) const;
Vector subtractFrom(const Vector &other) const;
Vector operator+ (const Vector&); //and operators
Vector operator- (const Vector&);
Vector operator* (const float &x); // scaling the vector
Vector crossProduct(const Vector& other);
float getDotProduct(const Vector &other) const;
float getDotProduct(float x, float y, float z) const;
friend ostream &operator<<(ostream & out, const Vector& v);
static Vector vectorFromPoints(Point& start, Point& end);
static Vector vectorFromPoints(Point& start, float endx, float endy, float endz);
static Vector vectorFromPoints(float startx, float starty, float startz, Point& end);
static Vector vectorFromPoints(float startx, float starty, float startz, float endx, float endy, float endz);
private:
float x;
float y;
float z;
};
#endif
#include "vector.h"
#include "math.h"
#include <iostream>
#include <math.h>
#include "windows.h"
#include <glut.h> // OpenGL utilties
using namespace MyOGLProg2;
using namespace std;
Vector::Vector(void)
{
this->x = this->y = this->z = 0.0;
}
Vector::Vector(float x, float y, float z)
{
this->x = x; this->y = y; this->z = z;
}
Vector::Vector(Position& start, Position& end)
{
this->x = end.x - start.x;
this->y = end.y - start.y;
this->z = end.z - start.z;
}
Vector Vector::addToAndDraw(const Vector &other, bool showSteps, float startX, float startY) const
{
Vector result;
//your code here -----------------
result.x = this->x + other.x;
result.y = this->y + other.y;
result.z = this->z + other.z;
this->drawArrow(startX,startY,0,0);
other.drawArrow(this->x,this->y,0,0);
glColor3f(1,1,0);
result.drawArrow(0,0,0,1,3);
return result;
}
Vector Vector::addTo(const Vector &other) const
{
Vector result;
//your code here -----------------
result.x = this->x + other.x;
result.y = this->x + other.y;
result.z = this->x + other.z;
return result;
}
Vector Vector::subtractFrom(const Vector &other) const
{
Vector result;
result.x = other.x - this->x;
result.y = other.y - this->y;
result.z = other.z - this->z;
return result;
}
float Vector::getMagnitude(void) const
{
float result = 0;
//your code here -----------------------
result = sqrt( (this->x * this->x) + (this->y * this->y) + (this->z * this->z) );
return result;
}
void Vector::setMagnitude(const float m)
{
//your code here ------------------------
this->x *= m / this->getMagnitude();
this->y *= m / this->getMagnitude();
this->z *= m / this->getMagnitude();
}
float Vector::getDotProduct(const Vector &other) const
{
float result;
//your code here -------------------------
result = (this->x * other.x) + (this->y * other.y) + (this->z * other.z);
return result;
}
float Vector::getDotProduct(float x, float y, float z) const
{
float result;
//your code here -------------------------
result = (this->x * x) + (this->y * y) + (this->z * z);
return result;
}
void Vector::normalise(void)
{
//your code here
this->x /= this->getMagnitude();
this->y /= this->getMagnitude();
this->z /= this->getMagnitude();
}
Vector Vector::operator+ (const Vector& other)
{
this->x += other.x;
this->y += other.y;
this->z += other.z;
return *this;
}
Vector Vector::operator- (const Vector& other)
{
this->x -= other.x;
this->y -= other.y;
this->z -= other.z;
return *this;
}
Vector Vector::operator* (const float &x)
{
this->x *= x;
this->y *= x;
this->z *= x;
return *this;
}
ostream & operator<<(ostream& out, const Vector& v)
{
out << "[" << v.x << "," << v.y << "," << v.z << "]" ;
return out;
}
Vector Vector::crossProduct(const Vector& other)
{
return Vector(this->y*other.z - this->z*other.y,this->z*other.x-this->x*other.z,this->x*other.y-this->y*other.x);
}
Vector Vector::setX(float newX)
{
this->x = newX;
return *this;
}
Vector Vector::setY(float newY)
{
this->y = newY;
return *this;
}
Vector Vector::setZ(float newZ)
{
this->z = newZ;
return *this;
}
float Vector::getAngleBetween2(const Vector& second)
{
float alpha = RAD2DEG(acos( this->getDotProduct(second) / ( this->getMagnitude()*second.getMagnitude() )));
return alpha;
}
bool Vector::checkDirection(const Vector& other)
{
float firstDirection = 0, secondDirection = 0;
if ( this->getDotProduct(1,0,0) >= 0 )
firstDirection = 1;
if ( other.getDotProduct(1,0,0) >= 0 )
secondDirection = 1;
if( firstDirection == secondDirection )
return 1;
else
return 0;
}
Vector Vector::vectorFromPoints(Point& start, Point& end)
{
Vector g(end.getX()-start.getX(),end.getY()-start.getY(),end.getZ()-start.getZ() );
return g;
}
Vector Vector::vectorFromPoints(Point& start, float endx, float endy, float endz)
{
Vector g(endx-start.getX(),endy-start.getY(),endz-start.getZ() );
return g;
}
Vector Vector::vectorFromPoints(float startx, float starty, float startz, Point& end)
{
Vector g(end.getX()-startx,end.getY()-starty,end.getZ()-startz );
return g;
}
Vector Vector::vectorFromPoints(float startx, float starty, float startz, float endx, float endy, float endz)
{
Vector g(endx-startx,endy-starty,endz-startz );
return g;
}
błędy zaczęły dopiero wyskakiwać jak dodałem operator << przed tym wszystko było OK.