math test (WIP)

This commit is contained in:
James Turk 2005-07-06 04:27:23 +00:00
parent 733450664e
commit dc663a4df1
7 changed files with 119 additions and 33 deletions

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // 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 #ifndef PHOTON_MATH_VECTOR2_HPP
#define 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 // Angle of vector in degrees, angle is calculated with respect to positive
// X axis. // X axis.
// //
// | |90° // | |90<EFBFBD>
// | | // | |
// | | // | |
// |180°-----------------------0° or 360° // |180<EFBFBD>-----------------------0<> or 360<36>
// | | // | |
// | | // | |
// | |270° // | |270<EFBFBD>
// //
// Returns: // Returns:
// Angle of vector (in degrees). // Angle of vector (in degrees).
@ -219,6 +219,17 @@ Vector2 operator*(scalar lhs, const Vector2 &rhs);
// Length of the vector. // Length of the vector.
scalar magnitude(const Vector2 &v); 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 // Group: Aliases
// Typedef: Point2 // Typedef: Point2

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // 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 #ifndef PHOTON_MATH_MATH_HPP
#define 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 // true if |val1-val2| < epsilon
bool scalarCompare(scalar val1, scalar val2, scalar epsilon=0.000001); 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 // Group: Degrees/Radians
// Function: degToRad // Function: degToRad

View File

@ -46,7 +46,16 @@
<node ID="Freemind_Link_288973656" TEXT="Texture"> <node ID="Freemind_Link_288973656" TEXT="Texture">
<icon BUILTIN="button_ok"/> <icon BUILTIN="button_ok"/>
</node> </node>
<node ID="Freemind_Link_1283736172" TEXT="Math Test(s)"/> <node ID="Freemind_Link_1283736172" TEXT="Math Test">
<node COLOR="#000000" ID="Freemind_Link_876352622" TEXT="General Math">
<icon BUILTIN="button_ok"/>
</node>
<node COLOR="#000000" ID="Freemind_Link_171161365" TEXT="Circle">
<icon BUILTIN="button_ok"/>
</node>
<node COLOR="#000000" ID="Freemind_Link_1333080570" TEXT="Rect"/>
<node COLOR="#000000" ID="Freemind_Link_63576276" TEXT="Vector2"/>
</node>
<node ID="Freemind_Link_279976301" TEXT="Pen"/> <node ID="Freemind_Link_279976301" TEXT="Pen"/>
<node ID="Freemind_Link_609153459" TEXT="Image"> <node ID="Freemind_Link_609153459" TEXT="Image">
<icon BUILTIN="button_ok"/> <icon BUILTIN="button_ok"/>
@ -54,12 +63,16 @@
<node ID="Freemind_Link_1961072093" TEXT="Font"> <node ID="Freemind_Link_1961072093" TEXT="Font">
<icon BUILTIN="button_ok"/> <icon BUILTIN="button_ok"/>
</node> </node>
<node ID="Freemind_Link_25725154" TEXT="Sound/Music"/> <node ID="Freemind_Link_25725154" TEXT="Audio">
<node COLOR="#000000" ID="Freemind_Link_1981834308" TEXT="integrate playing of &gt;1 sample"/>
<node COLOR="#000000" ID="Freemind_Link_212747291" TEXT="allow control via mouse or keyboard"/>
</node>
<node COLOR="#000000" ID="Freemind_Link_964892891" TEXT="Input"/>
</node> </node>
</node> </node>
<node ID="Freemind_Link_22783417" TEXT="Example Game"> <node ID="Freemind_Link_22783417" TEXT="Example Game">
<node ID="Freemind_Link_714736465" TEXT="Drawing Program?"/> <node ID="Freemind_Link_714736465" TEXT="Let&apos;s Draw"/>
<node ID="Freemind_Link_74598855" TEXT="Public Project?"/> <node COLOR="#000000" ID="Freemind_Link_597173063" TEXT="Asteroids"/>
</node> </node>
</node> </node>
<node ID="Freemind_Link_486829238" POSITION="right" TEXT="0.2 Release"> <node ID="Freemind_Link_486829238" POSITION="right" TEXT="0.2 Release">
@ -86,7 +99,7 @@
<font BOLD="true" NAME="SansSerif" SIZE="12"/> <font BOLD="true" NAME="SansSerif" SIZE="12"/>
<node ID="Freemind_Link_1080393911" TEXT="audio:: could using some cleaning"/> <node ID="Freemind_Link_1080393911" TEXT="audio:: could using some cleaning"/>
</node> </node>
<node COLOR="#147f1e" ID="Freemind_Link_438641521" POSITION="left" TEXT="Version: $Id: photon.mm,v 1.13 2005/07/06 02:10:06 cozman Exp $"> <node COLOR="#147f1e" ID="Freemind_Link_438641521" POSITION="left" TEXT="Version: $Id: photon.mm,v 1.14 2005/07/06 04:27:23 cozman Exp $">
<font ITALIC="true" NAME="SansSerif" SIZE="12"/> <font ITALIC="true" NAME="SansSerif" SIZE="12"/>
</node> </node>
</node> </node>

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // 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" #include "math/Circle.hpp"
@ -129,13 +129,11 @@ scalar Circle::getCenterX() const
return center_.x; return center_.x;
} }
scalar Circle::getCenterY() const scalar Circle::getCenterY() const
{ {
return center_.y; return center_.y;
} }
scalar Circle::getRadius() const scalar Circle::getRadius() const
{ {
return radius_; return radius_;

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // 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" #include "math/Vector2.hpp"
@ -147,6 +147,11 @@ scalar Vector2::calcInnerAngleDeg(const Vector2 &rhs) const
//non-members //non-members
std::ostream& operator<<(std::ostream &o, const Vector2 &v)
{
return o << "(" << v.x << "," << v.y << ")";
}
Vector2 operator*(scalar lhs, const Vector2 &rhs) Vector2 operator*(scalar lhs, const Vector2 &rhs)
{ {
return Vector2(rhs.x*lhs,rhs.y*lhs); return Vector2(rhs.x*lhs,rhs.y*lhs);
@ -157,9 +162,9 @@ scalar magnitude(const Vector2 &v)
return v.getMagnitude(); 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);
} }
} }

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // 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" #include "math/math.hpp"
@ -21,11 +21,6 @@ bool scalarCompare(scalar val1, scalar val2, scalar epsilon)
return std::fabs(val1-val2) < epsilon; return std::fabs(val1-val2) < epsilon;
} }
scalar distance(const Vector2& v1, const Vector2& v2)
{
return magnitude(v1-v2);
}
scalar degToRad(scalar degrees) scalar degToRad(scalar degrees)
{ {
const scalar convFactor(0.0174532925); //pi/180 const scalar convFactor(0.0174532925); //pi/180

75
test/math_test.cpp Normal file
View File

@ -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 <iostream>
#include <iomanip>
#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";
}