diff --git a/include/math/Vector2.hpp b/include/math/Vector2.hpp index cbb8ac6..094f911 100644 --- a/include/math/Vector2.hpp +++ b/include/math/Vector2.hpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: Vector2.hpp,v 1.3 2005/03/03 09:25:19 cozman Exp $ +// $Id: Vector2.hpp,v 1.4 2005/07/06 04:27:23 cozman Exp $ #ifndef PHOTON_MATH_VECTOR2_HPP #define PHOTON_MATH_VECTOR2_HPP @@ -146,13 +146,13 @@ public: // Angle of vector in degrees, angle is calculated with respect to positive // X axis. // - // | |90° + // | |90� // | | // | | - // |180°-----------------------0° or 360° + // |180�-----------------------0� or 360� // | | // | | - // | |270° + // | |270� // // Returns: // Angle of vector (in degrees). @@ -219,6 +219,17 @@ Vector2 operator*(scalar lhs, const Vector2 &rhs); // Length of the vector. scalar magnitude(const Vector2 &v); +// Function: distance +// Determine distance between two points. +// +// Parameters: +// v1 - First point. +// v2 - Second point. +// +// Returns: +// Scalar distance between the two points. +scalar distance(const Vector2& v1, const Vector2& v2); + // Group: Aliases // Typedef: Point2 diff --git a/include/math/math.hpp b/include/math/math.hpp index a0b1524..95fed8a 100644 --- a/include/math/math.hpp +++ b/include/math/math.hpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: math.hpp,v 1.4 2005/03/03 09:25:19 cozman Exp $ +// $Id: math.hpp,v 1.5 2005/07/06 04:27:23 cozman Exp $ #ifndef PHOTON_MATH_MATH_HPP #define PHOTON_MATH_MATH_HPP @@ -56,17 +56,6 @@ T clamp(T val, C low, C high); // 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(const Vector2& v1, const Vector2& v2); - // Group: Degrees/Radians // Function: degToRad diff --git a/photon.mm b/photon.mm index 541e63e..76aa75e 100644 --- a/photon.mm +++ b/photon.mm @@ -46,7 +46,16 @@ - + + + + + + + + + + @@ -54,12 +63,16 @@ - + + + + + - - + + @@ -86,7 +99,7 @@ - + diff --git a/src/math/Circle.cpp b/src/math/Circle.cpp index a01b972..c6c9070 100644 --- a/src/math/Circle.cpp +++ b/src/math/Circle.cpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: Circle.cpp,v 1.2 2005/03/03 09:25:47 cozman Exp $ +// $Id: Circle.cpp,v 1.3 2005/07/06 04:27:23 cozman Exp $ #include "math/Circle.hpp" @@ -129,13 +129,11 @@ scalar Circle::getCenterX() const return center_.x; } - scalar Circle::getCenterY() const { return center_.y; } - scalar Circle::getRadius() const { return radius_; diff --git a/src/math/Vector2.cpp b/src/math/Vector2.cpp index 3111122..3404180 100644 --- a/src/math/Vector2.cpp +++ b/src/math/Vector2.cpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: Vector2.cpp,v 1.1 2005/02/27 09:00:13 cozman Exp $ +// $Id: Vector2.cpp,v 1.2 2005/07/06 04:27:23 cozman Exp $ #include "math/Vector2.hpp" @@ -147,6 +147,11 @@ scalar Vector2::calcInnerAngleDeg(const Vector2 &rhs) const //non-members +std::ostream& operator<<(std::ostream &o, const Vector2 &v) +{ + return o << "(" << v.x << "," << v.y << ")"; +} + Vector2 operator*(scalar lhs, const Vector2 &rhs) { return Vector2(rhs.x*lhs,rhs.y*lhs); @@ -157,9 +162,9 @@ scalar magnitude(const Vector2 &v) return v.getMagnitude(); } -std::ostream& operator<<(std::ostream &o, const Vector2 &v) +scalar distance(const Vector2& v1, const Vector2& v2) { - return o << "(" << v.x << "," << v.y << ")"; + return magnitude(v1-v2); } } diff --git a/src/math/math.cpp b/src/math/math.cpp index 0a07c70..2aaae18 100644 --- a/src/math/math.cpp +++ b/src/math/math.cpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // Version: -// $Id: math.cpp,v 1.2 2005/03/03 09:25:47 cozman Exp $ +// $Id: math.cpp,v 1.3 2005/07/06 04:27:23 cozman Exp $ #include "math/math.hpp" @@ -21,11 +21,6 @@ bool scalarCompare(scalar val1, scalar val2, scalar epsilon) return std::fabs(val1-val2) < epsilon; } -scalar distance(const Vector2& v1, const Vector2& v2) -{ - return magnitude(v1-v2); -} - scalar degToRad(scalar degrees) { const scalar convFactor(0.0174532925); //pi/180 diff --git a/test/math_test.cpp b/test/math_test.cpp new file mode 100644 index 0000000..f42f94d --- /dev/null +++ b/test/math_test.cpp @@ -0,0 +1,75 @@ +//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_test.cpp,v 1.1 2005/07/06 04:27:23 cozman Exp $ + +// Tests almost everything within photon::math +// Doesn't test: +// -Circle-Rectangle intersection: hard to test without graphics +// -Accessors: so many of them, simple to detect problems, possibly add later +// -Rect: coming soon +// -Vector2: coming soon + +#include +#include +#include "photon.hpp" +using namespace photon; +using namespace std; + +int main() +{ + // Show sample usage of all functions in math.hpp + cout << "--General Math-------------------------------------------------\n"; + cout << "PI = " << setprecision(10) << math::PI << "\n"; + cout << "clamp(2.5, 0, 5) = " << math::clamp(2.5, 0, 5) << "\n"; + cout << "clamp(-1, 0, 5) = " << math::clamp(-1, 0, 5) << "\n"; + cout << "clamp(1000, 0, 5) = " << math::clamp(1000, 0, 5) << "\n"; + cout << "scalarCompare(3.0, 3.0000001) are " << + (math::scalarCompare(3.0, 3.0000001) ? "" : "not") << " equal\n"; + cout << "scalarCompare(3.0, 3.1) are " << + (math::scalarCompare(3.0, 3.1) ? "" : "not") << " equal\n"; + cout << "PI/6 radians = " << math::radToDeg(math::PI/6) << " degrees\n"; + cout << "45 degrees = " << math::degToRad(45) << " radians\n"; + cout << endl; + + // Demo Circle class + cout << "--Circles------------------------------------------------------\n"; + math::Circle a( math::Point2(0, 0), 1); // circle at origin w/ radius 5 + math::Circle b( math::Point2(10, 10), 25); // circle at 10,10 w/ radius 25 + math::Circle c( math::Point2(-10, -10), 1); // circle at -10,-10 w/ radius 1 + math::Point2 ori(0, 0); //origin + cout << "a: " << a << "\nb: " << b << "\n"; + cout << (a == a ? "a ==" : "a !=") << " a\n"; + cout << (a == b ? "a ==" : "a !=") << " b\n"; + cout << "Moving a to 100,100: "; + a.moveTo( math::Point2(100, 100) ); + cout << "a: " << a << "\n"; + cout << "Moving a by -100,-100: "; + a.moveRel( -100, -100 ); + cout << "a: " << a << "\n"; + cout << "Resizing a's radius to 2: "; + a.resize(2); + cout << "a: " << a << "\n"; + cout << "Resizing a's radius by -3 (can't have negative radius): "; + a.resizeRel(-3); + cout << "a: " << a << "\n"; + cout << "Resizing a's radius by +3: "; + a.resizeRel(3); + cout << "a: " << a << "\n"; + cout << "a and b " << (a.intersects(b) ? "" : "do not") << " intersect.\n"; + cout << "a and c " << (a.intersects(c) ? "" : "do not") << " intersect.\n"; + cout << "a " << (a.contains(ori) ? "contains " : "doesn't contain ") << ori + << ".\n"; + cout << "c " << (c.contains(ori) ? "contains " : "doesn't contain ") << ori + << ".\n"; + + cout << endl; + + // Demo Rect class + cout << "--Rects--------------------------------------------------------\n"; + +}