/******************************************
*
* Cedric Pradalier 2001
* mail : http://cedric.pradalier.free.fr/mail.html
*
*****************************************/
#include "vector2.h"
#include <stdio.h>
#include <math.h>
double Vector2::epsilon=1.0e-8;
Vector2 Vector2::operator+(const Vector2 & v) const
{
return Vector2(x+v.x,y+v.y);
}
Vector2 Vector2::operator-(const Vector2 & v) const
{
return Vector2(x-(v.x),y-(v.y));
}
Vector2 Vector2::operator*(const double & d) const
{
return Vector2(d * x , d * y);
}
Vector2 Vector2::operator/(const double & d) const
{
return Vector2(x/d , y/d);
}
double Vector2::operator*(const Vector2 & v) const
{
return (x * v.x) + (y * v.y);
}
bool Vector2::operator==(const Vector2 & v) const
{
return (fabs(v.x -x) < epsilon) && (fabs(v.y -y) < epsilon);
}
bool Vector2::operator!=(const Vector2 & v) const
{
return (fabs(v.x -x) > epsilon) || (fabs(v.y -y) > epsilon);
}
Vector2 Vector2::rotate(double theta) const
{
return Vector2(x * cos(theta) - y * sin(theta),
x * sin(theta) + y * cos(theta));
}
double Vector2::operator^(const Vector2 & v) const
{
return (x*v.y - y*v.x);
}
Vector2 Vector2::conj() const
{
return Vector2(x,-y);
}
double Vector2::angle() const
{
return atan2(y,x);
}
double Vector2::norme() const
{
return sqrt(x*x + y*y);
}
Vector2 Vector2::unitaire() const
{
return (*this) / norme();
}
void Vector2::Print(bool newline) const
{
if (newline)
printf("{%f,%f}\n",x,y);
else
printf("{%f,%f}",x,y);
}
void Vector2::Test()
{
Vector2 v1(3.0,5.0);
Vector2 v2(6.0,2.0);
Vector2 va( 1.0, 1.0);
Vector2 vb(-1.0, 1.0);
Vector2 vc(-1.0,-1.0);
Vector2 vd( 1.0, 1.0);
if (!(v1 == v1))
printf("== : probleme 1\n");
if (v1 == v2)
printf("== : probleme 2\n");
if (!(v1 != v2))
printf("!= : probleme\n");
printf("v1 : x : %f y: %f ",v1.getX(),v1.getY()); v1.Print();
printf("v2 : "); v2.Print();
printf("v1 + v2 : "); (v1+v2).Print();
printf("v1 - v2 : "); (v1 - v2).Print();
printf("5.0 * v1 : "); (v1 * 5.0).Print();
printf("(1/5) * v1 : "); (v1 / 5.0).Print();
printf("Produit scalaire : %e \n",(v1 * v2));
printf("Rotation Pi/2 : ");(v1.rotate(M_PI_2)).Print();
printf("Vecteur normal :");(v1.normal()).Print();
printf("Vecteur normé :");(v1.unitaire()).Print();
printf("Norme : %f \n",v1.norme());
va.Print(false);printf(" angle : %f \n",va.angle());
vb.Print(false);printf(" angle : %f \n",vb.angle());
vc.Print(false);printf(" angle : %f \n",vc.angle());
vd.Print(false);printf(" angle : %f \n",vd.angle());
}