initial import of math::

This commit is contained in:
James Turk 2005-02-27 09:00:13 +00:00
parent 7b74df2d18
commit e3cd5e1bb7
9 changed files with 1230 additions and 3 deletions

142
include/math/Circle.hpp Normal file
View File

@ -0,0 +1,142 @@
//This file is part of Photon (http://photon.sourceforge.net)
//Copyright (C) 2004-2005 James Turk
//
// Author:
// James Turk (jpt2433@rit.edu)
//
// Version:
// $Id: Circle.hpp,v 1.1 2005/02/27 09:00:13 cozman Exp $
#ifndef PHOTON_MATH_CIRCLE_HPP
#define PHOTON_MATH_CIRCLE_HPP
#include "math/Vector2.hpp"
namespace photon
{
namespace math
{
class Rect;
// Class: Circle
// Defines geometric entity known as a circle.
//
// A plane curve everywhere equidistant from a given fixed point, the center. <http://dictionary.com>
//
// Operators:
// - Circle == Circle
// - ostream& <<
class Circle
{
// Group: (Con/De)structors
public:
// Function: Circle
// Default constructor for circle, sets position to (0,0) and radius to 0.
Circle();
// Function: Circle
// Constructs circle with a given radius and center point.
//
// Parameters:
// center - Center point for circle.
// radius - Radius of circle.
Circle(const Point2 &center, scalar radius);
// Function: ~Circle
// Destructor, does nothing, exists to make Circle inheritance safe.
virtual ~Circle();
bool operator==(const Circle &rhs) const;
// Group: Movement/Scaling
public:
// Function: moveTo
// Move the center of the Circle to a new location.
//
// Parameters:
// center - New center for circle.
void moveTo(const Point2 &center);
// Function: moveRel
// Move the center of the Circle a given x and y distance.
//
// Parameters:
// xMove - The amount to move in the x direction.
// yMove - The amount to move in the y direction.
void moveRel(scalar xMove, scalar yMove);
// Function: resize
// Change the size of the Circle.
//
// Parameters:
// radius - The new radius for the Circle.
void resize(scalar radius);
// Function: resizeRel
// Change the size of the Circle in relation to the current size.
//
// Parameters:
// radiusChange - The change to apply to the radius.
void resizeRel(scalar radiusChange);
// Group: Geometry
public:
// Function: intersects
// Check if circle intersects another.
//
// Parameters:
// circle - Circle to check for intersection with.
//
// Returns:
// True if the circles intersect, false otherwise.
bool intersects(const Circle &circle) const;
// Function: intersects
// Check if circle intersects rectangle.
//
// Parameters:
// rect - Rectangle to check for intersection with.
//
// Returns:
// True if circle/rect intersect, false otherwise.
bool intersects(const Rect &rect) const;
// Function: contains
// Check if circle contains a given point.
//
// Parameters:
// point - Point to check for containment of.
//
// Returns:
// True if circle contains point, false otherwise.
bool contains(const Point2 &point) const;
// Group: Accessors
public:
// Function: getCenter
// Get center coordinate.
//
// Returns:
// Center coordinate.
Point2 getCenter() const;
// Function: getRadius
// Get radius of Circle.
//
// Returns:
// Radius of circle.
scalar getRadius() const;
friend std::ostream& operator<<(std::ostream &o, const Circle &circle);
// Data members
private:
Point2 center_;
scalar radius_;
};
}
}
#endif //PHOTON_MATH_CIRCLE_HPP

239
include/math/Rect.hpp Normal file
View File

@ -0,0 +1,239 @@
//This file is part of Photon (http://photon.sourceforge.net)
//Copyright (C) 2004-2005 James Turk
//
// Author:
// James Turk (jpt2433@rit.edu)
//
// Version:
// $Id: Rect.hpp,v 1.1 2005/02/27 09:00:13 cozman Exp $
#ifndef PHOTON_MATH_RECT_HPP
#define PHOTON_MATH_RECT_HPP
#include "math/Vector2.hpp"
namespace photon
{
namespace math
{
// Class: Rect
// Defines geometric entity known as a rectangle.
//
// A four-sided plane figure with four right angles. <http://dictionary.com>
//
// Operators:
// - Rect == Rect
// - ostream& << Rect
class Rect
{
// Group: (Con/De)structors
public:
// Function: Rect
// Initializes rectangle at (0,0) with size of 0.
Rect();
// Function: Rect
// Initializing constructor, creates rectangle from top left point,
// width and height.
//
// Parameters:
// topleft - Point describing top left corner.
// width - Width of new rectangle.
// height - Height of new rectangle.
Rect(const Point2 &topleft, scalar width, scalar height);
// Function: Rect
// Initializing constructor, creates from top left and bottom right points.
//
// Parameters:
// topleft - Point describing top left corner.
// bottomright - Point describing bottom right corner.
Rect(const Point2 &topleft, const Point2 &bottomright);
// Function: ~Rect
// Default destructor, exists to make Rect inheritance safe.
virtual ~Rect();
bool operator==(const Rect &rhs) const;
// Group: Movement/Scaling
public:
// Function: moveTo
// move rectangle to new location.
//
// Parameters:
// topleft - Point describing new top left corner.
void moveTo(const Point2 &topleft);
// Function: moveRel
// move rectangle relative to current position.
//
// Parameters:
// xMove - Distance to move horizontally.
// yMove - Distance to move vertically.
void moveRel(scalar xMove, scalar yMove);
// Function: resize
// set new size for rectangle.
//
// Parameters:
// width - New width for rectangle.
// height - New height for rectangle.
void resize(scalar width, scalar height);
// Function: resizeRel
// set new size for rectangle, relative to current size.
//
// Parameters:
// widthDelta - Change to be applied to width of rectangle.
// heightDelta - Change to be applied to height of rectangle.
void resizeRel(scalar widthDelta, scalar heightDelta);
// Group: Geometry
public:
// Function: intersects
// Check for intersection between two <Rects>.
//
// Parameters:
// rect - <Rect> with which to check for intersection.
//
// Returns:
// True if Rect intersects rect, false otherwise.
bool intersects(const Rect &rect) const;
// Function: contains
// Check if a point is contained within the Rect.
//
// Parameters:
// point - Point to check containment of.
//
// Returns:
// True if Rect contains point, false if not.
bool contains(const Point2 &point) const;
// Function: contains
// Check if another rectangle is entirely contained within the Rect.
//
// Parameters:
// rect - <Rect> to check if the Rect contains.
//
// Returns:
// True if rect is entirely contained within, false if all or part is
// outside.
bool contains(const Rect &rect) const;
// Function: calcIntersection
// calculates & returns the intersection of two rectangles.
//
// Parameters:
// rect - Rectangle to find & calculate area of intersection with.
//
// Returns:
// Rectangle defining area of intersection between the two rectangles.
//
// If <intersects> is false, the rectangle will have a width & height of
// zero.
Rect calcIntersection(const Rect &rect) const;
// Group: Accessors
public:
// Function: getX
// Get x coord of rectangle. (top left corner)
//
// Returns:
// Returns x coord of top left corner of rectangle.
scalar getX() const;
// Function: getY
// Get y coord of rectangle. (top left corner)
//
// Returns:
// Returns y coord of top left corner of rectangle.
scalar getY() const;
// Function: getTop
// Get y coord of top side of rectangle.
//
// Returns:
// Returns y coord of top side of rectangle.
scalar getTop() const;
// Function: getLeft
// Get x coord of left side of rectangle.
//
// Returns:
// Returns x coord of left side rectangle.
scalar getLeft() const;
// Function: getBottom
// Get y coord of bottom side of rectangle.
//
// Returns:
// Returns y coord of bottom side of rectangle.
scalar getBottom() const;
// Function: getRight
// Get x coord of right side of rectangle.
//
// Returns:
// Returns x coord of right side rectangle.
scalar getRight() const;
// Function: getWidth
// Get width of rectangle.
//
// Returns:
// Returns width of rectangle.
scalar getWidth() const;
// Function: getHeight
// Get height of rectangle.
//
// Returns:
// Height of rectangle.
scalar getHeight() const;
// Function: getTopLeft
// Get top left corner of rectangle.
//
// Returns:
// Top left corner of rectangle.
Point2 getTopLeft() const;
// Function: getTopRight
// Get top right corner of rectangle.
//
// Returns:
// Top right corner of rectangle.
Point2 getTopRight() const;
// Function: getBottomLeft
// Get bottom left corner of rectangle.
//
// Returns:
// Bottom left corner of rectangle.
Point2 getBottomLeft() const;
// Function: getBottomRight
// Get bottom right corner of rectangle.
//
// Returns:
// Bottom right corner of rectangle.
Point2 getBottomRight() const;
friend std::ostream& operator<<(std::ostream &o, const Rect &rect);
private:
Point2 topLeft_;
Point2 bottomRight_;
};
}
}
#endif //PHOTON_MATH_RECT_HPP

227
include/math/Vector2.hpp Normal file
View File

@ -0,0 +1,227 @@
//This file is part of Photon (http://photon.sourceforge.net)
//Copyright (C) 2004-2005 James Turk
//
// Author:
// James Turk (jpt2433@rit.edu)
//
// Version:
// $Id: Vector2.hpp,v 1.1 2005/02/27 09:00:13 cozman Exp $
#ifndef PHOTON_MATH_VECTOR2_HPP
#define PHOTON_MATH_VECTOR2_HPP
#include <ostream>
#include "types.hpp"
namespace photon
{
namespace math
{
// Class: Vector2
// Represents a two-dimensional vector. A quantity specified by a magnitude
// and a direction.
//
// Operators:
// - Vector2 == Vector2
// - Vector2 != Vector2
// - Vector2 + Vector2
// - Vector2 - Vector2
// - Vector2 * scalar or scalar * Vector2
// - Vector2 / scalar
// - Vector2 += Vector2
// - Vector2 -= Vector2
// - Vector2 *= scalar
// - Vector2 /= scalar
// - ostream& << Vector2
class Vector2
{
// Group: Data Members
public:
// Variable: x
// x component of vector
scalar x;
// Variable: y
// y component of vector
scalar y;
// Group: (Con/De)structors
public:
// Function: Vector2
// Initializes zero length vector.
Vector2();
// Function: Vector2
// Initialize vector given an x and y component.
//
// Parameters:
// nx - X component of vector.
// ny - Y component of vector.
//
// See Also:
// <set>
Vector2(scalar nx, scalar ny);
// Group: General
public:
// Function: set
// Initialize vector given an x and y component.
//
// Parameters:
// nx - X component of vector.
// ny - Y component of vector.
//
// See Also:
// <set>
void set(scalar nx, scalar ny);
// Function: resolveDeg
// resolve vector, given a magnitude & angle in degrees.
//
// Parameters:
// magnitude - Magnitude of new vector.
// angle - Angle of new vector, in degrees.
//
// See Also:
// <resolveRad>
void resolveDeg(scalar magnitude, scalar angle);
// Function: resolveRad
// resolve vector, given a magnitude & angle in radians.
//
// Parameters:
// magnitude - Magnitude of new vector.
// angle - Angle of new vector, in radians.
//
// See Also:
// <resolveDeg>
void resolveRad(scalar magnitude, scalar angle);
// Function: normalize
// Normalizes the vector. (makes it length of 1.0 but leaves ratio of
// components intact)
void normalize();
// Function: dot
// Calculates dot product of two vectors.
//
// Parameters:
// rhs - Vector to calculate dot product with.
//
// Returns:
// Dot product of two vectors.
scalar dot(const Vector2 &rhs) const;
bool operator==(const Vector2 &rhs) const;
bool operator!=(const Vector2 &rhs) const;
Vector2 operator-() const;
Vector2 operator+(const Vector2 &rhs) const;
Vector2 operator-(const Vector2 &rhs) const;
Vector2 operator*(scalar rhs) const;
Vector2 operator/(scalar rhs) const;
Vector2& operator+=(const Vector2 &rhs);
Vector2& operator-=(const Vector2 &rhs);
// Group: Special Accessors
public:
// Function: getMagnitude
// Get length of vector.
//
// Returns:
// Length of the vector.
scalar getMagnitude() const;
// Function: getAngleDeg
// Angle of vector in degrees, angle is calculated with respect to positive
// X axis.
//
// | |90°
// | |
// | |
// |180°-----------------------0° or 360°
// | |
// | |
// | |270°
//
// Returns:
// Angle of vector (in degrees).
scalar getAngleDeg() const;
// Function: getAngleRad
// Angle of vector in radians, angle is calculated with respect to positive
// X axis.
//
// | | pi/2
// | |
// | |
// | pi-----------------------0 or 2pi
// | |
// | |
// | | 3pi/4
//
// Returns:
// Angle of vector (in radians).
scalar getAngleRad() const;
// Function: calcNormal
// Calculate the normal vector.
//
// Returns:
// Normal vector (vector w/ magnitude of 1) but same angle.
Vector2 calcNormal() const;
// Function: calcInnerAngleRad
// Calculates angle between two vectors.
//
// Parameters:
// vec - Vector to calculate angle between.
//
// Returns:
// Angle between the two vectors in radians.
scalar calcInnerAngleRad(const Vector2 &vec) const;
// Function: calcInnerAngleDeg
// Calculates angle between two vectors.
//
// Parameters:
// vec - Vector to calculate angle between.
//
// Returns:
// Angle between the two vectors in degrees.
scalar calcInnerAngleDeg(const Vector2 &vec) const;
friend std::ostream& operator<<(std::ostream &o, const Vector2 &v);
};
Vector2 operator*(scalar lhs, const Vector2 &rhs);
// Function: magnitude
// Find length of a vector.
//
// Parameters:
// v - Vector to calculate magnitude of
//
// Returns:
// Length of the vector.
scalar magnitude(const Vector2 &v);
// Typedef: Point2
// Alias for Vector2 type.
typedef Vector2 Point2;
}
}
#endif //PHOTON_MATH_VECTOR2_HPP

96
include/math/math.hpp Normal file
View File

@ -0,0 +1,96 @@
//This file is part of Photon (http://photon.sourceforge.net)
//Copyright (C) 2004-2005 James Turk
//
// Author:
// James Turk (jpt2433@rit.edu)
//
// Version:
// $Id: math.hpp,v 1.1 2005/02/27 09:00:13 cozman Exp $
#ifndef PHOTON_MATH_MATH_HPP
#define PHOTON_MATH_MATH_HPP
#include <cmath>
#include "types.hpp"
namespace photon
{
namespace math
{
class Vector2;
// Function: clamp
// Clamp a value between two boundaries.
//
// Parameters:
// val - value to clamp
// low - lower boundary
// high - upper boundary
//
// Returns:
// low if val < low, high if val > high, val otherwise
template<typename T, typename C>
T clamp(T val, C low, C high);
// Function: scalarCompare
// Check for equality, using an epsilon.
//
// Parameters:
// val1 - First value to compare.
// val2 - Second value to compare.
// epsilon - Epsilon value, defaults to 0.0001.
//
// Returns:
// true if |val1-val2| < epsilon
bool scalarCompare(scalar val1, scalar val2, scalar epsilon=0.000001);
// Function: distance
// Determine distance between two points.
//
// Parameters:
// v1 - First point.
// v2 - Second point.
//
// Returns:
// Scalar distance between the two points.
scalar distance(Vector2 v1, Vector2 v2);
// Function: degToRad
// Convert degrees to radians.
//
// Parameters:
// degrees - Degree value to convert to radians.
//
// Returns:
// Radian equivalent of 'degrees'.
scalar degToRad(scalar degrees);
// Function: radToDeg
// Convert radians to degrees.
//
// Parameters:
// radians - Radian value to convert to degrees.
//
// Returns:
// Degree equivalent of 'radians'
scalar radToDeg(scalar radians);
//template implementation
template<typename T, typename C>
T clamp(T val, C low, C high)
{
if(val < low)
return low;
else if(val > high)
return high;
else
return val;
}
}
}
#endif //PHOTON_MATH_MATH_HPP

View File

@ -5,13 +5,13 @@
# James Turk (jpt2433@rit.edu) # James Turk (jpt2433@rit.edu)
# #
# Version: # Version:
# $Id: makefile,v 1.3 2005/02/27 05:52:22 cozman Exp $ # $Id: makefile,v 1.4 2005/02/27 09:00:13 cozman Exp $
INCLUDE_DIR = ./include INCLUDE_DIR = ./include
SRC_DIR = ./src SRC_DIR = ./src
LIB_DIR = ./lib LIB_DIR = ./lib
SUBDIRS = util util/filesys audio SUBDIRS = audio math util util/filesys
INCLUDE_DIRS = $(INCLUDE_DIR) $(foreach dir,$(SUBDIRS),$(INCLUDE_DIR)/$(dir)) INCLUDE_DIRS = $(INCLUDE_DIR) $(foreach dir,$(SUBDIRS),$(INCLUDE_DIR)/$(dir))
SRC_DIRS = $(SRC_DIR) $(foreach dir,$(SUBDIRS),$(SRC_DIR)/$(dir)) SRC_DIRS = $(SRC_DIR) $(foreach dir,$(SUBDIRS),$(SRC_DIR)/$(dir))
@ -19,7 +19,7 @@ SRCS = $(foreach dir,$(SRC_DIRS),$(wildcard $(dir)/*.cpp))
INCLUDES = $(foreach dir,$(INCLUDE_DIRS),$(wildcard $(dir)/*.hpp)) INCLUDES = $(foreach dir,$(INCLUDE_DIRS),$(wildcard $(dir)/*.hpp))
OBJS = $(SRCS:.cpp=.o) OBJS = $(SRCS:.cpp=.o)
LIBS = -lphysfs -lglfw -lOpenGL32 -lOpenAL LIBS = -lphysfs -lglfw -lOpenGL32 -lOpenAL32
LIBNAME = photon LIBNAME = photon
LIBFILE = $(LIB_DIR)/lib$(LIBNAME).a LIBFILE = $(LIB_DIR)/lib$(LIBNAME).a

139
src/math/Circle.cpp Normal file
View File

@ -0,0 +1,139 @@
//This file is part of Photon (http://photon.sourceforge.net)
//Copyright (C) 2004-2005 James Turk
//
// Author:
// James Turk (jpt2433@rit.edu)
//
// Version:
// $Id: Circle.cpp,v 1.1 2005/02/27 09:00:13 cozman Exp $
#include "math/Circle.hpp"
#include "math/Rect.hpp"
#include "math/math.hpp"
namespace photon
{
namespace math
{
Circle::Circle() :
radius_(0)
{
}
Circle::Circle(const Point2 &center, scalar radius) :
center_(center), radius_(std::max(radius,0.))
{
}
Circle::~Circle()
{
}
bool Circle::operator==(const Circle &rhs) const
{
return center_ == rhs.center_ && scalarCompare(radius_,rhs.radius_);
}
void Circle::moveTo(const Point2 &center)
{
center_ = center;
}
void Circle::moveRel(scalar xMove, scalar yMove)
{
center_.x += xMove;
center_.y += yMove;
}
void Circle::resize(scalar radius)
{
radius_ = std::max(radius,0.);
}
void Circle::resizeRel(scalar radiusChange)
{
radius_ += radiusChange;
radius_ = std::max(radius_,0.);
}
bool Circle::intersects(const Circle &circle) const
{
return distance(center_,circle.center_) < radius_+circle.radius_;
}
bool Circle::intersects(const Rect &rect) const
{
scalar newX;
scalar newY;
Rect circBound(center_-Vector2(radius_,radius_),2*radius_,2*radius_);
//reference of rects, 'rect' is shown as rect 5
// _____________
// | | | |
// | 1 | 2 | 3 |
// |___|___|___|
// | | | |
// | 4 | 5 | 6 |
// |___|___|___|
// | | | |
// | 7 | 8 | 9 |
// |___|___|___|
if(rect.intersects(circBound)) //within bounding rect
{
if(center_.x < rect.getLeft()) //narrowed down to 1,4,7
newX = center_.x+radius_;
else if(center_.x > rect.getRight()) //narrowed down to 3,6,9
newX = center_.x-radius_;
else //narrowed down to 2,5,8
newX = center_.x;
if(center_.y < rect.getTop()) //3
newY = center_.y+radius_;
else if(center_.y > rect.getBottom()) //9
newY = center_.y-radius_;
else
newY = center_.y;
if(scalarCompare(newX,center_.x) ||
scalarCompare(newY,center_.y)) //2,4,5,6,8
return true;
else //1,3,7,9
{
if(contains(rect.getTopLeft()) || contains(rect.getBottomLeft()) ||
contains(rect.getTopRight()) || contains(rect.getBottomRight()))
return true;
else
return false;
}
}
else
return false;
}
bool Circle::contains(const Point2 &point) const
{
return distance(center_,point) < radius_;
}
Point2 Circle::getCenter() const
{
return center_;
}
scalar Circle::getRadius() const
{
return radius_;
}
std::ostream& operator<<(std::ostream &o, const Circle &circle)
{
return o << "Circle { Center: " << circle.center_
<< " Radius: " << circle.radius_ << " }";
}
}
}

176
src/math/Rect.cpp Normal file
View File

@ -0,0 +1,176 @@
//This file is part of Photon (http://photon.sourceforge.net)
//Copyright (C) 2004-2005 James Turk
//
// Author:
// James Turk (jpt2433@rit.edu)
//
// Version:
// $Id: Rect.cpp,v 1.1 2005/02/27 09:00:13 cozman Exp $
#include "math/Rect.hpp"
#include <algorithm>
namespace photon
{
namespace math
{
Rect::Rect()
{
}
Rect::Rect(const Point2 &topleft, scalar width, scalar height) :
topLeft_(topleft),
bottomRight_(topleft.x+std::max(width,0.),topleft.y+std::max(height,0.))
{
}
Rect::Rect(const Point2 &topleft, const Point2 &bottomright) :
topLeft_(topleft),bottomRight_(bottomright)
{
}
Rect::~Rect()
{
}
bool Rect::operator==(const Rect &rhs) const
{
return topLeft_ == rhs.topLeft_ && bottomRight_ == rhs.bottomRight_;
}
void Rect::moveTo(const Point2 &topleft)
{
bottomRight_ += (topleft-topLeft_);
topLeft_ = topleft;
}
void Rect::moveRel(scalar xMove, scalar yMove)
{
topLeft_.x += xMove;
topLeft_.y += yMove;
bottomRight_.x += xMove;
bottomRight_.y += yMove;
}
void Rect::resize(scalar width, scalar height)
{
bottomRight_.x = topLeft_.x+std::max(width,0.);
bottomRight_.y = topLeft_.y+std::max(height,0.);
}
void Rect::resizeRel(scalar widthDelta, scalar heightDelta)
{
bottomRight_.x += widthDelta;
bottomRight_.y += heightDelta;
}
bool Rect::intersects(const Rect &rect) const
{
return !(topLeft_.x > rect.bottomRight_.x ||
rect.topLeft_.x > bottomRight_.x ||
topLeft_.y > rect.bottomRight_.y ||
rect.topLeft_.y > bottomRight_.y);
}
bool Rect::contains(const Point2 &point) const
{
return point.x > topLeft_.x && point.x < bottomRight_.x &&
point.y > topLeft_.y && point.y < bottomRight_.y;
}
bool Rect::contains(const Rect &rect) const
{
return rect.topLeft_.x > topLeft_.x &&
rect.bottomRight_.x < bottomRight_.x &&
rect.topLeft_.y > topLeft_.y &&
bottomRight_.y < bottomRight_.y;
}
Rect Rect::calcIntersection(const Rect &rect) const
{
Rect temp;
//can only grab the intersection if they intersect
if(intersects(rect))
{
temp.topLeft_.x = std::max(topLeft_.x,rect.topLeft_.x);
temp.topLeft_.y = std::max(topLeft_.y,rect.topLeft_.y);
temp.bottomRight_.x = std::min(bottomRight_.x,rect.bottomRight_.x);
temp.bottomRight_.y = std::min(bottomRight_.y,rect.bottomRight_.y);
}
return temp;
}
scalar Rect::getX() const
{
return topLeft_.x;
}
scalar Rect::getY() const
{
return topLeft_.y;
}
scalar Rect::getTop() const
{
return topLeft_.y;
}
scalar Rect::getLeft() const
{
return topLeft_.x;
}
scalar Rect::getBottom() const
{
return bottomRight_.y;
}
scalar Rect::getRight() const
{
return bottomRight_.x;
}
scalar Rect::getWidth() const
{
return bottomRight_.x-topLeft_.x;
}
scalar Rect::getHeight() const
{
return bottomRight_.y-topLeft_.y;
}
Point2 Rect::getTopLeft() const
{
return topLeft_;
}
Point2 Rect::getTopRight() const
{
return Point2(bottomRight_.x,topLeft_.y);
}
Point2 Rect::getBottomLeft() const
{
return Point2(topLeft_.x,bottomRight_.y);
}
Point2 Rect::getBottomRight() const
{
return bottomRight_;
}
std::ostream& operator<<(std::ostream &o, const Rect &rect)
{
return o << "Rect { Top left: " << rect.topLeft_
<< " Width: " << rect.getWidth()
<< " Height: " << rect.getHeight() << " }";
}
}
}

166
src/math/Vector2.cpp Normal file
View File

@ -0,0 +1,166 @@
//This file is part of Photon (http://photon.sourceforge.net)
//Copyright (C) 2004-2005 James Turk
//
// Author:
// James Turk (jpt2433@rit.edu)
//
// Version:
// $Id: Vector2.cpp,v 1.1 2005/02/27 09:00:13 cozman Exp $
#include "math/Vector2.hpp"
#include <cmath>
#include "math/math.hpp"
namespace photon
{
namespace math
{
Vector2::Vector2() :
x(0), y(0)
{
}
Vector2::Vector2(scalar nx, scalar ny) :
x(nx), y(ny)
{
}
void Vector2::set(scalar nx, scalar ny)
{
x = nx;
y = ny;
}
void Vector2::resolveDeg(scalar magnitude, scalar angle)
{
angle = degToRad(clamp(angle,0,360));
x = magnitude*std::cos(angle);
y = magnitude*-std::sin(angle);
}
void Vector2::resolveRad(scalar magnitude, scalar angle)
{
x = magnitude*std::cos(angle);
y = magnitude*-std::sin(angle);
}
void Vector2::normalize()
{
scalar mag = std::sqrt(x*x+y*y);
if(mag != 0)
{
x /= mag;
y /= mag;
}
}
scalar Vector2::dot(const Vector2 &rhs) const
{
return x*rhs.x + y*rhs.y;
}
bool Vector2::operator==(const Vector2 &rhs) const
{
return scalarCompare(rhs.x,x) && scalarCompare(rhs.y,y);
}
bool Vector2::operator!=(const Vector2 &rhs) const
{
return !scalarCompare(rhs.x,x) || !scalarCompare(rhs.y,y);
}
Vector2 Vector2::operator-() const
{
return Vector2(-x,-y);
}
Vector2 Vector2::operator+(const Vector2 &rhs) const
{
return Vector2(x+rhs.x,y+rhs.y);
}
Vector2 Vector2::operator-(const Vector2 &rhs) const
{
return Vector2(x-rhs.x,y-rhs.y);
}
Vector2 Vector2::operator*(scalar rhs) const
{
return Vector2(x*rhs,y*rhs);
}
Vector2 Vector2::operator/(scalar rhs) const
{
return Vector2(x/rhs,y/rhs);
}
Vector2& Vector2::operator+=(const Vector2 &rhs)
{
x += rhs.x;
y += rhs.y;
return *this;
}
Vector2& Vector2::operator-=(const Vector2 &rhs)
{
x -= rhs.x;
y -= rhs.y;
return *this;
}
scalar Vector2::getMagnitude() const
{
return std::sqrt(x*x+y*y);
}
scalar Vector2::getAngleDeg() const
{
return radToDeg(std::atan2(y,x));
}
scalar Vector2::getAngleRad() const
{
return std::atan2(y,x);
}
Vector2 Vector2::calcNormal() const
{
scalar mag = std::sqrt(x*x+y*y);
return Vector2(x/mag,y/mag);
}
scalar Vector2::calcInnerAngleRad(const Vector2 &rhs) const
{
// Ux*Vx+Uy*Vy
// theta = arccos(-----------)
// |U|*|V|
return std::acos((rhs.x*x+rhs.y*y)/(rhs.getMagnitude()*getMagnitude()));
}
scalar Vector2::calcInnerAngleDeg(const Vector2 &rhs) const
{
return radToDeg(calcInnerAngleRad(rhs));
}
//non-members
Vector2 operator*(scalar lhs, const Vector2 &rhs)
{
return Vector2(rhs.x*lhs,rhs.y*lhs);
}
scalar magnitude(const Vector2 &v)
{
return v.getMagnitude();
}
std::ostream& operator<<(std::ostream &o, const Vector2 &v)
{
return o << "(" << v.x << "," << v.y << ")";
}
}
}

42
src/math/math.cpp Normal file
View File

@ -0,0 +1,42 @@
//This file is part of Photon (http://photon.sourceforge.net)
//Copyright (C) 2004-2005 James Turk
//
// Author:
// James Turk (jpt2433@rit.edu)
//
// Version:
// $Id: math.cpp,v 1.1 2005/02/27 09:00:13 cozman Exp $
#include "math/math.hpp"
#include "math/Vector2.hpp"
namespace photon
{
namespace math
{
bool scalarCompare(scalar val1, scalar val2, scalar epsilon)
{
return std::fabs(val1-val2) < epsilon;
}
scalar distance(Vector2 v1, Vector2 v2)
{
return magnitude(v1-v2);
}
scalar degToRad(scalar degrees)
{
const scalar convFactor(0.0174532925); //pi/180
return degrees * convFactor;
}
scalar radToDeg(scalar radians)
{
const scalar convFactor(57.2957795); //180/pi
return radians * convFactor;
}
}
}