📄 point.cpp
字号:
/*+-------------------------------------------------------------------
Ben Landon
CSCI E-235
Point.cpp - Implementation file for the Point class. The
represents a Point with x, y, z, h coordinates.
*/
#include <assert.h>
#include "Point.hpp"
Point::Point (float x, float y, float z)
: m_x(x), m_y(y), m_z(z), m_w(1.0), m_rhw(1.0)
{
}
Point::Point (float x, float y, float z, float h)
: m_x(x), m_y(y), m_z(z), m_w(h)
{
if (m_w != 0.0)
{
m_rhw = 1.0f / m_w;
}
else
{
m_rhw = 1.0f;
// not correct, but what else can I do?
// Throw an exception maybe?
}
}
Point::Point (const Point& point)
{
m_x = point.m_x;
m_y = point.m_y;
m_z = point.m_z;
m_w = point.m_w;
m_rhw = point.m_rhw;
}
Point::Point (void)
: m_x (0.0), m_y(0.0), m_z(0.0), m_w(1.0), m_rhw(1.0f)
{
}
const Point& Point::operator= (const Point& pt)
{
m_x = pt.m_x;
m_y = pt.m_y;
m_z = pt.m_z;
m_w = pt.m_w;
m_rhw = pt.m_rhw;
return *this;
}
Point::~Point ()
{
// Nothing to do, there is no dynamic allocation
// in Point.
}
/*+-------------------------------------------------------------------
Point::set_coordinates (method)
This method simply assigns the x, y, z, and w parameters to the x,
y, z and w coordintes in this Point object.
w is an optional parameter. If w is not specified then w is assumed
to be 1.0.
*/
void Point::set_coordinates (float x, float y, float z, float w)
{
m_x = x;
m_y = y;
m_z = z;
m_w = w;
}
void Point::set_coordinates (float x, float y, float z, float w, float rhw)
{
m_x = x;
m_y = y;
m_z = z;
m_w = w;
m_rhw = rhw;
}
/*+----------------------------------------------------------------------------
Point::operator-
In affine spaces, subtracting two Points gives a Vector.
This method creates a new Vector object based on the difference
of two points.
*/
Vector Point::operator- (const Point& point) const
{
float dx = m_x - point.m_x;
float dy = m_y - point.m_y;
float dz = m_z - point.m_z;
// Vector has an implicit h = 0.0
return Vector(dx, dy, dz);
}
/*+-------------------------------------------------------------------
Add a Point and a Vector to get another Point.
In affine vector spaces it makes sense to add a Point and a Vector
together to get a resultant point.
*/
Point Point::operator+ (const Vector& vec) const
{
return Point(m_x + vec.x(), m_y + vec.y(), m_z + vec.z(), m_w);
}
Point Point::operator- (const Vector& vec) const
{
return Point(m_x - vec.x(), m_y - vec.y(), m_z - vec.z(), m_w);
}
/*+-------------------------------------------------------------------
Point::perspective_divide (method)
*/
void Point::perspective_divide (void)
{
assert(m_w != 0.0);
m_x /= m_w;
m_y /= m_w;
m_z /= m_w;
if (m_w != 1.0) // don't throw away m_rhw
m_rhw = 1.0f / m_w;
m_w = 1.0;
}
/*+-------------------------------------------------------------------
Point::interpolate (static method)
*/
Point Point::interpolate (const Point& A, float t, const Point& B)
{
float one_minus_t = 1 - t;
float x = (one_minus_t * (A.m_x)) + (t * (B.m_x));
float y = (one_minus_t * (A.m_y)) + (t * (B.m_y));
float z = (one_minus_t * (A.m_z)) + (t * (B.m_z));
float w = (one_minus_t * (A.m_w)) + (t * (B.m_w));
float rhw = (one_minus_t * (A.m_rhw)) + (t * (B.m_rhw));
Point result(x, y, z, w);
result.m_rhw = rhw;
return result;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -