From e3cd5e1bb7161410730f686db70f5394ab5bb712 Mon Sep 17 00:00:00 2001 From: James Turk Date: Sun, 27 Feb 2005 09:00:13 +0000 Subject: [PATCH] initial import of math:: --- include/math/Circle.hpp | 142 +++++++++++++++++++++++ include/math/Rect.hpp | 239 +++++++++++++++++++++++++++++++++++++++ include/math/Vector2.hpp | 227 +++++++++++++++++++++++++++++++++++++ include/math/math.hpp | 96 ++++++++++++++++ makefile | 6 +- src/math/Circle.cpp | 139 +++++++++++++++++++++++ src/math/Rect.cpp | 176 ++++++++++++++++++++++++++++ src/math/Vector2.cpp | 166 +++++++++++++++++++++++++++ src/math/math.cpp | 42 +++++++ 9 files changed, 1230 insertions(+), 3 deletions(-) create mode 100644 include/math/Circle.hpp create mode 100644 include/math/Rect.hpp create mode 100644 include/math/Vector2.hpp create mode 100644 include/math/math.hpp create mode 100644 src/math/Circle.cpp create mode 100644 src/math/Rect.cpp create mode 100644 src/math/Vector2.cpp create mode 100644 src/math/math.cpp diff --git a/include/math/Circle.hpp b/include/math/Circle.hpp new file mode 100644 index 0000000..20011d9 --- /dev/null +++ b/include/math/Circle.hpp @@ -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. +// +// 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 ¢er, 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 ¢er); + + // 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 diff --git a/include/math/Rect.hpp b/include/math/Rect.hpp new file mode 100644 index 0000000..f82b7ec --- /dev/null +++ b/include/math/Rect.hpp @@ -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. +// +// 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 . + // + // Parameters: + // 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 - 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 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 diff --git a/include/math/Vector2.hpp b/include/math/Vector2.hpp new file mode 100644 index 0000000..2d94ee5 --- /dev/null +++ b/include/math/Vector2.hpp @@ -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 + +#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: + // + 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: + // + 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: + // + 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: + // + 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 diff --git a/include/math/math.hpp b/include/math/math.hpp new file mode 100644 index 0000000..341e8ae --- /dev/null +++ b/include/math/math.hpp @@ -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 + +#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 +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 +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 diff --git a/makefile b/makefile index 4530368..d4e1d86 100644 --- a/makefile +++ b/makefile @@ -5,13 +5,13 @@ # James Turk (jpt2433@rit.edu) # # 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 SRC_DIR = ./src LIB_DIR = ./lib -SUBDIRS = util util/filesys audio +SUBDIRS = audio math util util/filesys INCLUDE_DIRS = $(INCLUDE_DIR) $(foreach dir,$(SUBDIRS),$(INCLUDE_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)) OBJS = $(SRCS:.cpp=.o) -LIBS = -lphysfs -lglfw -lOpenGL32 -lOpenAL +LIBS = -lphysfs -lglfw -lOpenGL32 -lOpenAL32 LIBNAME = photon LIBFILE = $(LIB_DIR)/lib$(LIBNAME).a diff --git a/src/math/Circle.cpp b/src/math/Circle.cpp new file mode 100644 index 0000000..a262e7b --- /dev/null +++ b/src/math/Circle.cpp @@ -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 ¢er, 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 ¢er) +{ + 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_ << " }"; +} + +} +} diff --git a/src/math/Rect.cpp b/src/math/Rect.cpp new file mode 100644 index 0000000..30fcfeb --- /dev/null +++ b/src/math/Rect.cpp @@ -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 + +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() << " }"; +} + +} +} diff --git a/src/math/Vector2.cpp b/src/math/Vector2.cpp new file mode 100644 index 0000000..3111122 --- /dev/null +++ b/src/math/Vector2.cpp @@ -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 + +#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 << ")"; +} + +} +} diff --git a/src/math/math.cpp b/src/math/math.cpp new file mode 100644 index 0000000..4f96675 --- /dev/null +++ b/src/math/math.cpp @@ -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; +} + +} +}