http://code.google.com/p/idlescreen/source/detail?r=210
Modified:
/branches/backus_dev/src/common/utility/Point2D.h
/branches/backus_dev/src/common/utility/Vector2D.cpp
/branches/backus_dev/src/common/utility/Vector2D.h
/branches/backus_dev/src/common/utility/unit_tests/Test_Vector2D.cpp
/branches/backus_dev/src/common/utility/unit_tests/Test_Vector2D.h
=======================================
--- /branches/backus_dev/src/common/utility/Point2D.h Sun Feb 7 18:40:30
2010
+++ /branches/backus_dev/src/common/utility/Point2D.h Wed Feb 10 18:19:00
2010
@@ -57,7 +57,7 @@
/**
* Gets the x value.
*/
- double getX();
+ double getX();
/**
* Sets the x value.
*/
=======================================
--- /branches/backus_dev/src/common/utility/Vector2D.cpp Sun Feb 7
18:40:30 2010
+++ /branches/backus_dev/src/common/utility/Vector2D.cpp Wed Feb 10
18:19:00 2010
@@ -174,29 +174,56 @@
return *this;
}
-bool Vector2D::operator==(const Vector2D& other) {
- return (relativeCompare(_x, other._x, _epsilon) &&
- relativeCompare(_y, other._y, _epsilon));
-}
-bool Vector2D::operator!=(const Vector2D& other) {
- return !(*this == other);
-
-}
-#ifdef __NEEDS_DEBUGING__
+
/**
* Calculates the angle between the specified vector and this vector.
* Note: returns a value between [0, pi] or NaN if values are invalid.
+ * If one or both of the vectors has a length of zero, a value of
+ * 0.0 is returned.
*/
double Vector2D::getAngle(const Vector2D other) {
// we know that mag(v1)*mag(v2)*cos(theta)=normal(v1) dot v2
// thus theta = arccos( (normal(v1) dot v2) / ( mag(v1)*mag(v2) ) )
- Vector2D norm = getNormal();
+ Vector2D norm = *this;
Vector2D otherUnitV = other;
+ norm.normalize();
otherUnitV.normalize(); // dealing with normal vectors makes the math
easy.
+ if(norm.magnitude() != 1.0 || otherUnitV.magnitude() != 1.0) {
+ return 0.0;
+ }
+
return acos(norm.dot(otherUnitV));
}
+
+/**
+ * Rotates the vector by the angle radians.
+ */
+void Vector2D::rotate(const double angle) {
+ double newX = _x*cos(angle)-_y*sin(angle);
+ double newY = _x*sin(angle)+_y*cos(angle);
+
+ _x = newX;
+ _y = newY;
+}
+
+
+/**
+ * Returns the point that is described by traveling along a scaled
+ * version of this vector from the specified point. Note that
+ * this vector is not normalized beforehand.
+ *
+ * @param a Origination point.
+ * @param scalar The amount to scale this vector by first.
+ */
+Point2D Vector2D::getPoint(const Point2D a, const double scalar) {
+ Point2D retVal = a;
+ retVal.setX(retVal.getX()+scalar*_x);
+ retVal.setY(retVal.getY()+scalar*_y);
+
+ return retVal;
+}
/**
* Checks to see if point B is on the line specified
@@ -205,11 +232,23 @@
* @param a A point known to be on the line.
* @param b The point to check.
*/
-bool Vector2D::isOnLine(const Point2D a, const point2D b) {
+bool Vector2D::isOnLine(const Point2D a, const Point2D b) {
+ // check to see if the points are the same. If so, easy peasy!
+ Point2D copyA = a;
+ if(copyA == b) {
+ return true;
+ }
+
+ // create a replica and ensure that its magnitude isn't 0.0.
+ Vector2D replica = *this;
+ if(relativeCompare(replica.magnitude(),0.0, _epsilon)) {
+ return true;
+ }
// If the vector from point B to point A is parallel to vector V, then
- // point B is on the line.
- Vector2D v2 = b - a;
- return isParallel(v2);
+ // point B is on the line.
+ Vector2D v2;
+ v2 = b - a;
+ return replica.isParallelTo(v2);
}
/**
@@ -228,30 +267,43 @@
* @return True if the lines intersect, false if they do not.
*/
bool Vector2D::getIntersectingPt(const Point2D pA, const Vector2D vB,
- const Point2D pB, Vector2D* result) {
-
- double numer = vB._x*(pB.getY() - pA.getY()) -
- vB._y*(pB.getX() - pA.getX());
- double denom = vB._y*_x - vB._x*_y;
+ const Point2D pB, Point2D& result) {
+
+ Point2D rPA = pA;
+ Point2D rPB = pB;
+ Vector2D rVB = vB;
+ double numer = rVB._x*(rPB.getY() - rPA.getY()) -
+ rVB._y*(rPB.getX() - rPA.getX());
+ double denom = rVB._y*_x - rVB._x*_y;
if(denom != 0) {
- if(result != NULL) {
- Vector2D vF = (*this) * (numer / denom);
- result = pA + vF;
- }
+ Vector2D vF = (*this) * (numer / denom);
+ result = rPA + vF;
}
return false;
}
-Vector2D& Vector2D::operator+(const Vector2D& other) {
- *this += other;
- return *this;
+bool Vector2D::operator==(const Vector2D& other) {
+ return (relativeCompare(_x, other._x, _epsilon) &&
+ relativeCompare(_y, other._y, _epsilon));
+}
+bool Vector2D::operator!=(const Vector2D& other) {
+ return !(*this == other);
+
}
Vector2D& Vector2D::operator*(const double val) {
*this *= val;
return *this;
}
-Vector2D& Vector2D::operator*(const double val, Vector2D& v) {
- return v*val;
+Vector2D& Vector2D::operator*=(const double val) {
+ _x *= val;
+ _y *= val;
+ _bIsNormalized = false;
+ return *this;
+}
+
+Vector2D& Vector2D::operator+(const Vector2D& other) {
+ *this += other;
+ return *this;
}
Vector2D& Vector2D::operator/(const double val) {
*this /= val;
@@ -263,18 +315,15 @@
_bIsNormalized = false;
return *this;
}
-Vector2D& Vector2D::operator*=(const double val) {
- _x *= val;
- _y *= val;
- _bIsNormalized = false;
- return *this;
-}
Vector2D& Vector2D::operator/=(const double val) {
- *this *= (1.0/val);
+ _x /= val;
+ _y /= val;
+ _bIsNormalized = false;
return *this;
}
Vector2D& Vector2D::operator-=(const Vector2D& other) {
- Vector2D otherInv = other * -1.0;
+ Vector2D otherInv = other;
+ otherInv *= -1.0;
*this += otherInv;
return *this;
}
@@ -285,28 +334,41 @@
// **** Begin Friend definitions ****
+Vector2D operator*(const double val, const Vector2D& v) {
+ Vector2D tmp = v;
+ return tmp*val;
+}
+
/**
- * point subtraction results in a vector
+ * A point plus a vector results in a new point.
*/
-Vector2D& operator-(const Point2D& initial, const Point2D& final) {
- double x = final.getX() - initial.getX();
- double y = final.getY() - initial.getY();
- Vector2D retVal(x, y);
+Point2D operator+(const Point2D& p, const Vector2D& v) {
+ Vector2D repV = v;
+ Point2D retVal = repV.getPoint(p,1.0);
return retVal;
}
+
+Point2D operator+(const Vector2D& v, const Point2D& p) {
+ Vector2D repV = v;
+ Point2D retVal = repV.getPoint(p,1.0);
+ return retVal;
+}
+
+Point2D operator-(const Point2D& p, const Vector2D& v) {
+ Vector2D repV = v;
+ Point2D retVal = repV.getPoint(p,-1.0);
+ return retVal;
+}
/**
- * A point plus a vector results in a new point.
+ * point subtraction results in a vector
*/
-Point2D& operator+(const Point2D& p, const Vector2D& v) {
- Point2D retVal;
- retVal.setX(p.getX()+v.getX());
- retVal.setY(p.getY()+v.getY());
-}
-Point2D& operator+(const Vector2D& v, const Point2D& p) {
- return p+v;
-}
-#endif
+Vector2D operator-(const Point2D& final, const Point2D& initial) {
+ Point2D f = final;
+ Point2D i = initial;
+ Vector2D retVal(f.getX() - i.getX(),f.getY() - i.getY());
+ return retVal;
+}
/**
* friend vector to dump to an ostream
@@ -316,4 +378,3 @@
os<<"("<<tmp.getX()<<","<<tmp.getY()<<")";
return os;
}
-
=======================================
--- /branches/backus_dev/src/common/utility/Vector2D.h Sun Feb 7 18:40:30
2010
+++ /branches/backus_dev/src/common/utility/Vector2D.h Wed Feb 10 18:19:00
2010
@@ -113,6 +113,38 @@
*/
bool isNormalTo(const Vector2D other);
+ /**
+ * Calculates the angle between the specified vector and this vector.
+ * Note: returns a value between [0, pi] or NaN if values are invalid.
+ * If one or both of the vectors has a length of zero, a value of
+ * 0.0 is returned.
+ */
+ double getAngle(const Vector2D other);
+
+ /**
+ * Rotates the vector by the angle radians.
+ */
+ void rotate(const double angle);
+
+ /**
+ * Returns the point that is described by traveling along a scaled
+ * version of this vector from the specified point. Note that
+ * this vector is not normalized beforehand.
+ *a
+ * @param a Origination point.
+ * @param scalar The amount to scale this vector by first.
+ */
+ Point2D getPoint(const Point2D a, const double scalar=1.0);
+
+ /**
+ * Checks to see if point B is on the line specified
+ * by this vector and point A.
+ *
+ * @param a A point known to be on the line.
+ * @param b The point to check.
+ */
+ bool isOnLine(const Point2D a, const Point2D b);
+
/**
* Sets the default epsilon value.
*/
@@ -137,30 +169,6 @@
bool operator==(const Vector2D& other);
bool operator!=(const Vector2D& other);
-#ifdef __NEEDS_DEBUGGING__
- /**
- * Calculates the angle between the specified vector and this vector.
- */
- double getAngle(const Vector2D other);
-
- /**
- * Checks to see if point B is on the line specified
- * by this vector and point A.
- *
- * @param a A point known to be on the line.
- * @param b The point to check.
- */
- bool isOnLine(const Point2D a, const point2D b);
-
- /**
- * Checks to see if point B is on the line specified
- * by this vector and point A.
- *
- * @param a A point known to be on the line.
- * @param b The point to check.
- */
- Point2D getPoint(const Point2D a, const double distance=1.0);
-
/**
* Finds the point of intersection of the line specified
* by this vector and point A and the line specified by
@@ -174,22 +182,17 @@
* @return True if the lines intersect, false if they do not.
*/
bool getIntersectingPt(const Point2D pA, const Vector2D vB,
- const Point2D pB, Vector2D* result);
-
- Vector2D& operator+(const Vector2D& other);
- Vector2D& operator-(const Vector2D& other);
+ const Point2D pB, Point2D& result);
+
Vector2D& operator*(const double val);
- Vector2D& operator*(const double val, Vector2D& v);
- Vector2D& operator/(const double val);
- Vector2D& operator+=(const Vector2D& other);
Vector2D& operator*=(const double val);
+ Vector2D& operator/(const double val);
Vector2D& operator/=(const double val);
+ Vector2D& operator+(const Vector2D& other);
+ Vector2D& operator+=(const Vector2D& other);
+ Vector2D& operator-(const Vector2D& other);
Vector2D& operator-=(const Vector2D& other);
- friend Vector2D& operator-(const Point2D& initial, const Point2D& final);
- friend Point2D& operator+(const Point2D& p, const Vector2D& v);
- friend Point2D& operator+(const Vector2D& v, const Point2D& p);
-#endif
friend ostream& operator<<(ostream& os, const Vector2D& vector);
private:
@@ -202,4 +205,12 @@
static double DEFAULT_EPSILON;
};
+
+// global functions related to this class
+extern Vector2D operator*(const double val, const Vector2D& v);
+extern Point2D operator+(const Point2D& p, const Vector2D& v);
+extern Point2D operator+(const Vector2D& v, const Point2D& p);
+extern Point2D operator-(const Point2D& p, const Vector2D& v);
+extern Vector2D operator-(const Point2D& final, const Point2D& initial);
+
#endif
=======================================
--- /branches/backus_dev/src/common/utility/unit_tests/Test_Vector2D.cpp
Sun Feb 7 18:40:30 2010
+++ /branches/backus_dev/src/common/utility/unit_tests/Test_Vector2D.cpp
Wed Feb 10 18:19:00 2010
@@ -385,6 +385,195 @@
QVERIFY(!testVectorA.isNormalTo(testVectorB));
QVERIFY(!testVectorB.isNormalTo(testVectorA));
}
+
+void Test_Vector2D::Test_GetAngle() {
+ Vector2D testVectorA;
+ Vector2D testVectorB;
+
+ // Parallel vectors
+ double angle = 0.0;
+ double mag = 3.5;
+ testVectorA.setValue(1.0,0.0);
+ testVectorB.setValue(mag*cos(angle),mag*sin(angle));
+ QVERIFY(relativeCompare(testVectorA.getAngle(testVectorB),angle,
1.0e-6));
+ QVERIFY(relativeCompare(testVectorB.getAngle(testVectorA),angle,
1.0e-6));
+
+ // Perpendicular vectors
+ angle = 1.570763;
+ mag = 17.4;
+ testVectorA.setValue(1.0,0.0);
+ testVectorB.setValue(mag*cos(angle),mag*sin(angle));
+ QVERIFY(relativeCompare(testVectorA.getAngle(testVectorB),angle,
1.0e-6));
+ QVERIFY(relativeCompare(testVectorB.getAngle(testVectorA),angle,
1.0e-6));
+
+ // Acute vectors
+ angle = 0.478115;
+ mag = 0.653;
+ testVectorA.setValue(1.0,0.0);
+ testVectorB.setValue(mag*cos(angle),mag*sin(angle));
+ QVERIFY(relativeCompare(testVectorA.getAngle(testVectorB),angle,
1.0e-6));
+ QVERIFY(relativeCompare(testVectorB.getAngle(testVectorA),angle,
1.0e-6));
+
+ // Obtuse vectors
+ angle = 3.071099;
+ mag = 19273.1;
+ testVectorA.setValue(1.0,0.0);
+ testVectorB.setValue(mag*cos(angle),mag*sin(angle));
+ QVERIFY(relativeCompare(testVectorA.getAngle(testVectorB),angle,
1.0e-6));
+ QVERIFY(relativeCompare(testVectorB.getAngle(testVectorA),angle,
1.0e-6));
+
+ // both set to default
+ angle = 0.0;
+ testVectorA.setValue();
+ testVectorB.setValue();
+ QVERIFY(relativeCompare(testVectorA.getAngle(testVectorB),angle,
1.0e-6));
+ QVERIFY(relativeCompare(testVectorB.getAngle(testVectorA),angle,
1.0e-6));
+}
+
+void Test_Vector2D::Test_Rotate() {
+ Vector2D testVector;
+ double angle;
+ double x;
+ double y;
+ // Parallel vectors
+ angle = 0.0;
+ x = 1.345;
+ y = 2.714;
+ testVector.setValue(x,y);
+ testVector.rotate(angle);
+ QVERIFY(relativeCompare(testVector.getX(),x*cos(angle)-y*sin(angle),
1.0e-6));
+ QVERIFY(relativeCompare(testVector.getY(),x*sin(angle)+y*cos(angle),
1.0e-6));
+
+ // perpendicular vectors
+ angle = 1.570796;
+ x = 1.345;
+ y = 2.714;
+ testVector.setValue(x,y);
+ testVector.rotate(angle);
+ QVERIFY(relativeCompare(testVector.getX(),x*cos(angle)-y*sin(angle),
1.0e-6));
+ QVERIFY(relativeCompare(testVector.getY(),x*sin(angle)+y*cos(angle),
1.0e-6));
+
+ // 180 degrees
+ angle = 3.1415927;
+ x = 1.345;
+ y = 2.714;
+ testVector.setValue(x,y);
+ testVector.rotate(angle);
+ QVERIFY(relativeCompare(testVector.getX(),x*cos(angle)-y*sin(angle),
1.0e-6));
+ QVERIFY(relativeCompare(testVector.getY(),x*sin(angle)+y*cos(angle),
1.0e-6));
+
+ // some positive angle
+ angle = 0.558505;
+ x = 1.345;
+ y = 2.714;
+ testVector.setValue(x,y);
+ testVector.rotate(angle);
+ QVERIFY(relativeCompare(testVector.getX(),x*cos(angle)-y*sin(angle),
1.0e-6));
+ QVERIFY(relativeCompare(testVector.getY(),x*sin(angle)+y*cos(angle),
1.0e-6));
+
+ // some negative angle
+ angle = -0.558505;
+ x = 1.345;
+ y = 2.714;
+ testVector.setValue(x,y);
+ testVector.rotate(angle);
+ QVERIFY(relativeCompare(testVector.getX(),x*cos(angle)-y*sin(angle),
1.0e-6));
+ QVERIFY(relativeCompare(testVector.getY(),x*sin(angle)+y*cos(angle),
1.0e-6));
+
+ // set to default
+ angle = 1.954769;
+ testVector.setValue();
+ testVector.rotate(angle);
+ QVERIFY(relativeCompare(testVector.getX(),0.0, 1.0e-6));
+ QVERIFY(relativeCompare(testVector.getY(),0.0, 1.0e-6));
+}
+
+void Test_Vector2D::Test_GetPoint() {
+ Vector2D vector;
+ Point2D origin;
+ Point2D result;
+ double scalar;
+
+ // simple case
+ vector.setValue(1.0,1.0);
+ origin.setValue(2.0,5.0);
+ scalar = 12.0;
+ result = vector.getPoint(origin, scalar);
+ QVERIFY(relativeCompare(result.getX(),vector.getX()*scalar+origin.getX(),
+ 1.0e-6));
+ QVERIFY(relativeCompare(result.getY(),vector.getY()*scalar+origin.getY(),
+ 1.0e-6));
+
+ // fractional case
+ vector.setValue(10.0,10.0);
+ origin.setValue(17.3,91.4);
+ scalar = 0.5;
+ result = vector.getPoint(origin, scalar);
+ QVERIFY(relativeCompare(result.getX(),vector.getX()*scalar+origin.getX(),
+ 1.0e-6));
+ QVERIFY(relativeCompare(result.getY(),vector.getY()*scalar+origin.getY(),
+ 1.0e-6));
+
+ // negative case
+ vector.setValue(1.0,1.0);
+ vector.normalize();
+ origin.setValue(0.0,0.0);
+ scalar = -1914.1341;
+ result = vector.getPoint(origin, scalar);
+ QVERIFY(relativeCompare(result.getX(),vector.getX()*scalar+origin.getX(),
+ 1.0e-6));
+ QVERIFY(relativeCompare(result.getY(),vector.getY()*scalar+origin.getY(),
+ 1.0e-6));
+
+ // default case
+ vector.setValue();
+ origin.setValue();
+ scalar = 12.0;
+ result = vector.getPoint(origin, scalar);
+ QVERIFY(relativeCompare(result.getX(),0.0, 1.0e-6));
+ QVERIFY(relativeCompare(result.getY(),0.0, 1.0e-6));
+
+}
+
+void Test_Vector2D::Test_IsOnLine() {
+ Vector2D vector;
+ Point2D origin;
+ Point2D testPoint;
+
+ // positive slope
+ vector.setValue(1.0,1.0);
+ origin.setValue(2.0,5.0);
+ testPoint.setValue(4.7,7.7);
+ QVERIFY(vector.isOnLine(origin,testPoint));
+
+ // negative slope
+ vector.setValue(1.0,1.0);
+ origin.setValue(2.0,5.0);
+ testPoint.setValue(-1.3,1.7);
+ QVERIFY(vector.isOnLine(origin,testPoint));
+
+ // not on line
+ vector.setValue(1.0,1.0);
+ origin.setValue(2.0,5.0);
+ testPoint.setValue(4.7,5.7);
+ QVERIFY(!vector.isOnLine(origin,testPoint));
+
+ // same point
+ vector.setValue(1.0,1.0);
+ origin.setValue(2.0,5.0);
+ testPoint.setValue(2.0,5.0);
+ QVERIFY(vector.isOnLine(origin,testPoint));
+
+ // default vector
+ vector.setValue();
+ origin.setValue(2.0,5.0);
+ testPoint.setValue(2.0,5.0);
+ QVERIFY(vector.isOnLine(origin,testPoint));
+}
+
+void Test_Vector2D::Test_GetIntersectingPt() {
+ QFAIL("Not implemented yet!");
+}
void Test_Vector2D::Test_OperatorSet() {
Vector2D testVector;
@@ -449,6 +638,359 @@
QVERIFY(testVector != testVectorB);
QVERIFY(!(testVector != testVectorC));
}
+
+void Test_Vector2D::Test_OperatorScalarMultiplyEqual() {
+ Vector2D testVector;
+ double scalar;
+ double x;
+ double y;
+
+ // simple case
+ x = 17.234;
+ y = 1.1091;
+ scalar = 0.9324;
+ testVector.setValue(x,y);
+ testVector *= scalar;
+ QVERIFY(relativeCompare(testVector.getX(),x*scalar,1.0e-6));
+ QVERIFY(relativeCompare(testVector.getY(),y*scalar,1.0e-6));
+
+ // scalar is 0.0
+ x = 17.234;
+ y = 1.1091;
+ scalar = 0.0;
+ testVector.setValue(x,y);
+ testVector *= scalar;
+ QVERIFY(relativeCompare(testVector.getX(),0.0,1.0e-6));
+ QVERIFY(relativeCompare(testVector.getY(),0.0,1.0e-6));
+
+ // vector is default
+ scalar = 0.9324;
+ testVector.setValue();
+ testVector *= scalar;
+ QVERIFY(relativeCompare(testVector.getX(),0.0,1.0e-6));
+ QVERIFY(relativeCompare(testVector.getY(),0.0,1.0e-6));
+}
+
+void Test_Vector2D::Test_OperatorScalarMultiply() {
+ Vector2D testVector;
+ Vector2D result;
+ double scalar;
+ double x;
+ double y;
+
+ // Test V * S
+ // simple case
+ x = 17.234;
+ y = 1.1091;
+ scalar = 0.9324;
+ testVector.setValue(x,y);
+ result = testVector * scalar;
+ QVERIFY(relativeCompare(result.getX(),x*scalar,1.0e-6));
+ QVERIFY(relativeCompare(result.getY(),y*scalar,1.0e-6));
+
+ // scalar is 0.0
+ x = 17.234;
+ y = 1.1091;
+ scalar = 0.0;
+ testVector.setValue(x,y);
+ result = testVector * scalar;
+ QVERIFY(relativeCompare(result.getX(),x*scalar,1.0e-6));
+ QVERIFY(relativeCompare(result.getY(),y*scalar,1.0e-6));
+
+ // vector is default
+ scalar = 0.9324;
+ testVector.setValue();
+ result = testVector * scalar;
+ QVERIFY(relativeCompare(result.getX(),0.0,1.0e-6));
+ QVERIFY(relativeCompare(result.getY(),0.0,1.0e-6));
+
+ // Test S * V
+ // simple case
+ x = -0.9134;
+ y = 21.234;
+ scalar = 17.143;
+ testVector.setValue(x,y);
+ result = scalar * testVector;
+ QVERIFY(relativeCompare(result.getX(),x*scalar,1.0e-6));
+ QVERIFY(relativeCompare(result.getY(),y*scalar,1.0e-6));
+
+ // scalar is 0.0
+ x = -0.9134;
+ y = 21.234;
+ scalar = 0.0;
+ testVector.setValue(x,y);
+ result = scalar * testVector;
+ QVERIFY(relativeCompare(result.getX(),x*scalar,1.0e-6));
+ QVERIFY(relativeCompare(result.getY(),y*scalar,1.0e-6));
+
+ // vector is default
+ scalar = 17.143;
+ testVector.setValue();
+ result = scalar * testVector;
+ QVERIFY(relativeCompare(result.getX(),0.0,1.0e-6));
+ QVERIFY(relativeCompare(result.getY(),0.0,1.0e-6));
+}
+
+void Test_Vector2D::Test_OperatorScalarDivideEqual() {
+ Vector2D testVector;
+ double scalar;
+ double x;
+ double y;
+
+ // simple case
+ x = 17.234;
+ y = 1.1091;
+ scalar = 0.9324;
+ testVector.setValue(x,y);
+ testVector /= scalar;
+ QVERIFY(relativeCompare(testVector.getX(),x/scalar,1.0e-6));
+ QVERIFY(relativeCompare(testVector.getY(),y/scalar,1.0e-6));
+
+ // scalar is 0.0
+ x = 17.234;
+ y = 1.1091;
+ scalar = 0.0;
+ testVector.setValue(x,y);
+ testVector /= scalar;
+ QVERIFY(testVector.getX() == x/scalar);
+ QVERIFY(testVector.getY() == y/scalar);
+
+ // vector is default
+ scalar = 0.9324;
+ testVector.setValue();
+ testVector /= scalar;
+ QVERIFY(relativeCompare(testVector.getX(),0.0,1.0e-6));
+ QVERIFY(relativeCompare(testVector.getY(),0.0,1.0e-6));
+}
+
+void Test_Vector2D::Test_OperatorScalarDivision() {
+ Vector2D testVector;
+ Vector2D result;
+ double scalar;
+ double x;
+ double y;
+
+ // simple case
+ x = 17.234;
+ y = 1.1091;
+ scalar = 0.9324;
+ testVector.setValue(x,y);
+ result = testVector / scalar;
+ QVERIFY(relativeCompare(result.getX(),x/scalar,1.0e-6));
+ QVERIFY(relativeCompare(result.getY(),y/scalar,1.0e-6));
+
+ // scalar is 0.0
+ x = 17.234;
+ y = 1.1091;
+ scalar = 0.0;
+ testVector.setValue(x,y);
+ result = testVector / scalar;
+ QVERIFY(testVector.getX() == x/scalar);
+ QVERIFY(testVector.getY() == y/scalar);
+
+ // vector is default
+ scalar = 0.9324;
+ testVector.setValue();
+ result = testVector / scalar;
+ QVERIFY(relativeCompare(result.getX(),0.0,1.0e-6));
+ QVERIFY(relativeCompare(result.getY(),0.0,1.0e-6));
+}
+
+void Test_Vector2D::Test_VectorAddEqual() {
+ Vector2D testVectorA;
+ Vector2D testVectorB;
+ double x;
+ double y;
+
+ // simple case
+ x = 1.7324;
+ y = 9.1534;
+ testVectorA.setValue(x,y);
+ testVectorB.setValue(0.8234,17.2341);
+ testVectorA += testVectorB;
+ QVERIFY(relativeCompare(testVectorA.getX(),testVectorB.getX()+x,1.0e-6));
+ QVERIFY(relativeCompare(testVectorA.getY(),testVectorB.getY()+y,1.0e-6));
+
+ // rhs is default
+ x = 1.7324;
+ y = 9.1534;
+ testVectorA.setValue(x,y);
+ testVectorB.setValue();
+ testVectorA += testVectorB;
+ QVERIFY(relativeCompare(testVectorA.getX(),x,1.0e-6));
+ QVERIFY(relativeCompare(testVectorA.getY(),y,1.0e-6));
+
+ // lhs is default
+ testVectorA.setValue();
+ testVectorB.setValue(0.8234,17.2341);
+ testVectorA += testVectorB;
+ QVERIFY(relativeCompare(testVectorA.getX(),testVectorB.getX(),1.0e-6));
+ QVERIFY(relativeCompare(testVectorA.getY(),testVectorB.getY(),1.0e-6));
+}
+
+void Test_Vector2D::Test_VectorAddition() {
+ Vector2D testVectorA;
+ Vector2D testVectorB;
+ Vector2D result;
+ double x;
+ double y;
+
+ // simple case
+ x = 1.7324;
+ y = 9.1534;
+ testVectorA.setValue(x,y);
+ testVectorB.setValue(0.8234,17.2341);
+ result = testVectorA + testVectorB;
+ QVERIFY(relativeCompare(result.getX(),x+testVectorB.getX(),1.0e-6));
+ QVERIFY(relativeCompare(result.getY(),y+testVectorB.getY(),1.0e-6));
+
+ // rhs is default
+ x = 1.7324;
+ y = 9.1534;
+ testVectorA.setValue(x,y);
+ testVectorB.setValue();
+ result = testVectorA + testVectorB;
+ QVERIFY(relativeCompare(result.getX(),x+testVectorB.getX(),1.0e-6));
+ QVERIFY(relativeCompare(result.getY(),y+testVectorB.getY(),1.0e-6));
+
+ // lhs is default
+ testVectorA.setValue();
+ testVectorB.setValue(0.8234,17.2341);
+ result = testVectorA + testVectorB;
+ QVERIFY(relativeCompare(result.getX(),0.0+testVectorB.getX(),1.0e-6));
+ QVERIFY(relativeCompare(result.getY(),0.0+testVectorB.getY(),1.0e-6));
+}
+
+void Test_Vector2D::Test_VectorSubEqual() {
+ Vector2D testVectorA;
+ Vector2D testVectorB;
+ Vector2D result;
+ double x;
+ double y;
+
+ // simple case
+ x = 1.7324;
+ y = 9.1534;
+ testVectorA.setValue(x,y);
+ testVectorB.setValue(0.8234,17.2341);
+ testVectorA -= testVectorB;
+ QVERIFY(relativeCompare(testVectorA.getX(),x-testVectorB.getX(),1.0e-6));
+ QVERIFY(relativeCompare(testVectorA.getY(),y-testVectorB.getY(),1.0e-6));
+
+ // rhs is default
+ x = 1.7324;
+ y = 9.1534;
+ testVectorA.setValue(x,y);
+ testVectorB.setValue();
+ testVectorA -= testVectorB;
+ QVERIFY(relativeCompare(testVectorA.getX(),x-testVectorB.getX(),1.0e-6));
+ QVERIFY(relativeCompare(testVectorA.getY(),y-testVectorB.getY(),1.0e-6));
+
+ // lhs is default
+ testVectorA.setValue();
+ testVectorB.setValue(0.8234,17.2341);
+ testVectorA -= testVectorB;
+
QVERIFY(relativeCompare(testVectorA.getX(),0.0-testVectorB.getX(),1.0e-6));
+
QVERIFY(relativeCompare(testVectorA.getY(),0.0-testVectorB.getY(),1.0e-6));
+}
+
+void Test_Vector2D::Test_VectorSubtraction() {
+ Vector2D testVectorA;
+ Vector2D testVectorB;
+ Vector2D result;
+ double x;
+ double y;
+
+ // simple case
+ x = 1.7324;
+ y = 9.1534;
+ testVectorA.setValue(x,y);
+ testVectorB.setValue(0.8234,17.2341);
+ result = testVectorA - testVectorB;
+ QVERIFY(relativeCompare(result.getX(),x-testVectorB.getX(),1.0e-6));
+ QVERIFY(relativeCompare(result.getY(),y-testVectorB.getY(),1.0e-6));
+
+ // rhs is default
+ x = 1.7324;
+ y = 9.1534;
+ testVectorA.setValue(x,y);
+ testVectorB.setValue();
+ result = testVectorA - testVectorB;
+ QVERIFY(relativeCompare(result.getX(),x-testVectorB.getX(),1.0e-6));
+ QVERIFY(relativeCompare(result.getY(),y-testVectorB.getY(),1.0e-6));
+
+ // lhs is default
+ testVectorA.setValue();
+ testVectorB.setValue(0.8234,17.2341);
+ result = testVectorA - testVectorB;
+ QVERIFY(relativeCompare(result.getX(),0.0-testVectorB.getX(),1.0e-6));
+ QVERIFY(relativeCompare(result.getY(),0.0-testVectorB.getY(),1.0e-6));
+}
+
+void Test_Vector2D::Test_PointVectorAddition() {
+ Point2D p;
+ Point2D result;
+ Vector2D v;
+
+ // Test_GetPoint() tests most of the functionality...
+
+ // P+V case
+ p.setValue(27.123, 191.143);
+ v.setValue(13.2341, 17.0981);
+ result = p+v;
+ QVERIFY(relativeCompare(result.getX(),p.getX()+v.getX(), 1.0e-6));
+ QVERIFY(relativeCompare(result.getY(),p.getY()+v.getY(), 1.0e-6));
+
+ // V+P case
+ p.setValue(0.324, -0.4231);
+ v.setValue(1.723, 9.2341);
+ result = v+p;
+ QVERIFY(relativeCompare(result.getX(),p.getX()+v.getX(), 1.0e-6));
+ QVERIFY(relativeCompare(result.getY(),p.getY()+v.getY(), 1.0e-6));
+}
+
+void Test_Vector2D::Test_PointVectorSubtraction() {
+ Point2D p;
+ Point2D result;
+ Vector2D v;
+
+ // Test_GetPoint() tests most of the functionality...
+
+ // P-V case
+ p.setValue(27.123, 191.143);
+ v.setValue(13.2341, 17.0981);
+ result = p-v;
+ QVERIFY(relativeCompare(result.getX(),p.getX()-v.getX(), 1.0e-6));
+ QVERIFY(relativeCompare(result.getY(),p.getY()-v.getY(), 1.0e-6));
+
+}
+
+void Test_Vector2D::Test_PointSubtraction() {
+ Point2D a;
+ Point2D b;
+ Vector2D result;
+
+ // positive case
+ a.setValue(1.4, 5.7);
+ b.setValue(6.7, 23.9);
+ result = b - a;
+ QVERIFY(relativeCompare(result.getX(),b.getX()-a.getX(), 1.0e-6));
+ QVERIFY(relativeCompare(result.getY(),b.getY()-a.getY(), 1.0e-6));
+
+ // negative case
+ a.setValue(6.7, 23.9);
+ b.setValue(1.4, 5.7);
+ result = b - a;
+ QVERIFY(relativeCompare(result.getX(),b.getX()-a.getX(), 1.0e-6));
+ QVERIFY(relativeCompare(result.getY(),b.getY()-a.getY(), 1.0e-6));
+
+ // both defaults case
+ a.setValue();
+ b.setValue();
+ result = b - a;
+ QVERIFY(relativeCompare(result.getX(),b.getX()-a.getX(), 1.0e-6));
+ QVERIFY(relativeCompare(result.getY(),b.getY()-a.getY(), 1.0e-6));
+}
void Test_Vector2D::Test_OperatorOstream() {
double xVal;
@@ -489,158 +1031,3 @@
QVERIFY(testStrB == controlStrB);
}
-#ifdef __NEEDS_DEBUGGING__
-
-void Test_Vector2D::Test_OperatorMultiply() {
- double xVal = 5.4;
- double yVal = 7.2;
- double mul;
- Vector2D testVector(xVal, yVal);
-
- QVERIFY(testVector.getX() == xVal);
- QVERIFY(testVector.getY() == yVal);
-
- // Positive number
- mul = 4.91;
- testVector = testVector * mul;
- xVal *= mul;
- yVal *= mul;
- QVERIFY(testVector.getX() == xVal);
- QVERIFY(testVector.getY() == yVal);
-
- // Negative number
- mul = -0.15;
- testVector = testVector * mul;
- xVal *= mul;
- yVal *= mul;
- QVERIFY(testVector.getX() == xVal);
- QVERIFY(testVector.getY() == yVal);
-
- // Zero
- mul = 0.0;
- testVector = testVector * mul;
- xVal *= mul;
- yVal *= mul;
- QVERIFY(testVector.getX() == xVal);
- QVERIFY(testVector.getY() == yVal);
-}
-
-void Test_Vector2D::Test_OperatorDivide() {
- double xVal = 32.149;
- double yVal = 91.43;
- double div;
- Vector2D testVector(xVal, yVal);
-
- QVERIFY(testVector.getX() == xVal);
- QVERIFY(testVector.getY() == yVal);
-
- // Positive number
- div = 0.7291;
- testVector = testVector / div;
- xVal /= div;
- yVal /= div;
- QVERIFY(testVector.getX() == xVal);
- QVERIFY(testVector.getY() == yVal);
-
- // Negative number
- div = -15.941;
- testVector = testVector / div;
- xVal /= div;
- yVal /= div;
- QVERIFY(testVector.getX() == xVal);
- QVERIFY(testVector.getY() == yVal);
-
- // One
- div = 1.0;
- testVector = testVector / div;
- xVal /= div;
- yVal /= div;
- QVERIFY(testVector.getX() == xVal);
- QVERIFY(testVector.getY() == yVal);
-
- // Zero
- div = 0.0;
- testVector = testVector / div;
- xVal /= div;
- yVal /= div;
- QVERIFY(testVector.getX() == xVal);
- QVERIFY(testVector.getY() == yVal);
-}
-
-void Test_Vector2D::Test_OperatorMultiplyEqual() {
- double xVal = 1.19;
- double yVal = 910.14;
- double mul;
- Vector2D testVector(xVal, yVal);
-
- QVERIFY(testVector.getX() == xVal);
- QVERIFY(testVector.getY() == yVal);
-
- // Positive number
- mul = 0.1234613;
- testVector *= mul;
- xVal *= mul;
- yVal *= mul;
- QVERIFY(testVector.getX() == xVal);
- QVERIFY(testVector.getY() == yVal);
-
- // Negative number
- mul = -7234.19;
- testVector *= mul;
- xVal *= mul;
- yVal *= mul;
- QVERIFY(testVector.getX() == xVal);
- QVERIFY(testVector.getY() == yVal);
-
- // Zero
- mul = 0.0;
- testVector *= mul;
- xVal *= mul;
- yVal *= mul;
- QVERIFY(testVector.getX() == xVal);
- QVERIFY(testVector.getY() == yVal);
-}
-
-void Test_Vector2D::Test_OperatorDivideEqual() {
- double xVal = 12.1;
- double yVal = 0.7143;
- double div;
- Vector2D testVector(xVal, yVal);
-
- QVERIFY(testVector.getX() == xVal);
- QVERIFY(testVector.getY() == yVal);
-
- // Positive number
- div = 71.45;
- testVector /= div;
- xVal /= div;
- yVal /= div;
- QVERIFY(testVector.getX() == xVal);
- QVERIFY(testVector.getY() == yVal);
-
- // Negative number
- div = -0.941613;
- testVector /= div;
- xVal /= div;
- yVal /= div;
- QVERIFY(testVector.getX() == xVal);
- QVERIFY(testVector.getY() == yVal);
-
- // One
- div = 1.0;
- testVector /= div;
- xVal /= div;
- yVal /= div;
- QVERIFY(testVector.getX() == xVal);
- QVERIFY(testVector.getY() == yVal);
-
- // Zero
- div = 0.0;
- testVector /= div;
- xVal /= div;
- yVal /= div;
- QVERIFY(testVector.getX() == xVal);
- QVERIFY(testVector.getY() == yVal);
-}
-#endif
-
=======================================
--- /branches/backus_dev/src/common/utility/unit_tests/Test_Vector2D.h Sun
Feb 7 18:40:30 2010
+++ /branches/backus_dev/src/common/utility/unit_tests/Test_Vector2D.h Wed
Feb 10 18:19:00 2010
@@ -46,16 +46,26 @@
void Test_Dot();
void Test_IsParallelTo();
void Test_IsNormalTo();
+ void Test_GetAngle();
+ void Test_Rotate();
+ void Test_GetPoint();
+ void Test_IsOnLine();
+ void Test_GetIntersectingPt();
void Test_OperatorSet();
void Test_OperatorEqual();
void Test_OperatorNotEqual();
- /*
- void Test_OperatorMultiply();
- void Test_OperatorDivide();
- void Test_OperatorMultiplyEqual();
- void Test_OperatorDivideEqual();
- */
+ void Test_OperatorScalarMultiplyEqual();
+ void Test_OperatorScalarMultiply();
+ void Test_OperatorScalarDivideEqual();
+ void Test_OperatorScalarDivision();
+ void Test_VectorAddEqual();
+ void Test_VectorAddition();
+ void Test_VectorSubEqual();
+ void Test_VectorSubtraction();
+ void Test_PointVectorAddition();
+ void Test_PointVectorSubtraction();
+ void Test_PointSubtraction();
void Test_OperatorOstream();
private: