219 lines
3.0 KiB
C++
219 lines
3.0 KiB
C++
#include "point3d.hh"
|
|
|
|
|
|
|
|
/*** Constructeurs ***/
|
|
|
|
|
|
Point3D::Point3D(const float &_x, const float &_y, const float &_z)
|
|
{
|
|
x = _x ;
|
|
y = _y ;
|
|
z = _z ;
|
|
}
|
|
|
|
|
|
Point3D::Point3D(const Point3D &p)
|
|
{
|
|
x = p.x ;
|
|
y = p.y ;
|
|
z = p.z ;
|
|
}
|
|
|
|
|
|
Point3D::Point3D(const float c[3])
|
|
{
|
|
x = c[0] ;
|
|
y = c[1] ;
|
|
z = c[2] ;
|
|
}
|
|
|
|
|
|
|
|
/*** Accesseurs lecture ***/
|
|
|
|
|
|
float Point3D::get_x() const
|
|
{
|
|
return x ;
|
|
}
|
|
|
|
|
|
float Point3D::get_y() const
|
|
{
|
|
return y ;
|
|
}
|
|
|
|
|
|
float Point3D::get_z() const
|
|
{
|
|
return z ;
|
|
}
|
|
|
|
|
|
|
|
/*** Accesseurs écriture ***/
|
|
|
|
|
|
void Point3D::set_x(const float &_x)
|
|
{
|
|
x = _x ;
|
|
}
|
|
|
|
|
|
void Point3D::set_y(const float &_y)
|
|
{
|
|
y = _y ;
|
|
}
|
|
|
|
|
|
void Point3D::set_z(const float &_z)
|
|
{
|
|
z = _z ;
|
|
}
|
|
|
|
|
|
/*
|
|
* Updates x, y, z by passing a Point3D
|
|
* This is useful for derivated classes, and different than a direct call to
|
|
* operator=().
|
|
*/
|
|
void Point3D::set_coordinates(const Point3D &p)
|
|
{
|
|
this->operator=(p) ;
|
|
}
|
|
|
|
|
|
|
|
/*** Calcul de distances ***/
|
|
|
|
|
|
/*
|
|
* Distance carrée à un point (Point3D)
|
|
*/
|
|
float Point3D::square_distance(const Point3D &p) const
|
|
{
|
|
return
|
|
(x - p.x) * (x - p.x) +
|
|
(y - p.y) * (y - p.y) +
|
|
(z - p.z) * (z - p.z) ;
|
|
}
|
|
|
|
|
|
/*
|
|
* Distance carrée à un point décrit par ses coordonnées
|
|
*/
|
|
float Point3D::square_distance(const float &_x,
|
|
const float &_y,
|
|
const float &_z) const
|
|
{
|
|
return
|
|
(x - _x) * (x - _x) +
|
|
(y - _y) * (y - _y) +
|
|
(z - _z) * (z - _z) ;
|
|
}
|
|
|
|
|
|
/*
|
|
* Distance à un point (Point3D)
|
|
* Note : pour comparer deux distances, utiliser de préférence
|
|
* square_distance, qui fait économiser une racine carrée.
|
|
*/
|
|
float Point3D::distance(const Point3D &p) const
|
|
{
|
|
return sqrt(square_distance(p)) ;
|
|
}
|
|
|
|
|
|
/*
|
|
* Distance à un point décrit par ses coordonnées
|
|
* Note : pour comparer deux distances, utiliser de préférence
|
|
* square_distance, qui fait économiser une racine carrée.
|
|
*/
|
|
float Point3D::distance(const float &_x,
|
|
const float &_y,
|
|
const float &_z) const
|
|
{
|
|
return sqrt(square_distance(_x, _y, _z)) ;
|
|
}
|
|
|
|
|
|
|
|
/*** Opérateurs ***/
|
|
|
|
|
|
Point3D Point3D::operator=(const Point3D &p)
|
|
{
|
|
if (this == &p)
|
|
return *this ;
|
|
|
|
x = p.x ;
|
|
y = p.y ;
|
|
z = p.z ;
|
|
|
|
return *this ;
|
|
}
|
|
|
|
|
|
bool Point3D::operator==(const Point3D &p) const
|
|
{
|
|
if (this == &p)
|
|
return true ;
|
|
|
|
return
|
|
x == p.x &&
|
|
y == p.y &&
|
|
z == p.z ;
|
|
}
|
|
|
|
|
|
bool Point3D::operator!=(const Point3D &p) const
|
|
{
|
|
return !(*this == p) ;
|
|
}
|
|
|
|
|
|
bool Point3D::operator<(const Point3D &p) const
|
|
{
|
|
if (x < p.x)
|
|
return true ;
|
|
if (x > p.x)
|
|
return false ;
|
|
|
|
if (y < p.y)
|
|
return true ;
|
|
if (y > p.y)
|
|
return false ;
|
|
|
|
if (z < p.z)
|
|
return true ;
|
|
|
|
return false ;
|
|
}
|
|
|
|
|
|
bool Point3D::operator>(const Point3D &p) const
|
|
{
|
|
return (p < *this) ;
|
|
}
|
|
|
|
|
|
bool Point3D::operator<=(const Point3D &p) const
|
|
{
|
|
return (*this < p || *this == p) ;
|
|
}
|
|
|
|
|
|
bool Point3D::operator>=(const Point3D &p) const
|
|
{
|
|
return (p <= *this) ;
|
|
}
|
|
|
|
|
|
|
|
std::ostream& operator<<(std::ostream &os, const Point3D &p)
|
|
{
|
|
os << "(" << p.x << ";" << p.y << ";" << p.z << ")" ;
|
|
return os ;
|
|
}
|