[idlescreen commit] r204 - Added Point2D and Vector2D classes. Have not been tested! Need to also

0 views
Skip to first unread message

codesite...@google.com

unread,
Apr 27, 2009, 6:19:32 PM4/27/09
to dev-idl...@googlegroups.com
Author: jeff.backus
Date: Mon Apr 27 14:19:22 2009
New Revision: 204

Added:
branches/backus_dev/src/common/utility/Point2D.cpp
branches/backus_dev/src/common/utility/Point2D.h
branches/backus_dev/src/common/utility/Vector2D.cpp
branches/backus_dev/src/common/utility/Vector2D.h

Log:
Added Point2D and Vector2D classes. Have not been tested! Need to also
create unit tests for them. Which means creating a structure for unit
tests.
Should use Qt's unit test framework (one less set of libraries to install).


Added: branches/backus_dev/src/common/utility/Point2D.cpp
==============================================================================
--- (empty file)
+++ branches/backus_dev/src/common/utility/Point2D.cpp Mon Apr 27 14:19:22
2009
@@ -0,0 +1,79 @@
+/**
+ * Constructor.
+ *
+ * @param x The initial x value of the vector.
+ * @param y The initial y value of the vector.
+ */
+Point2D::Point2D(const double x=0.0, const double y=0.0) {
+ _x = x;
+ _y = y;
+}
+
+/**
+ * Deconstructor
+ */
+Point2D::~Point2D() {
+}
+
+/**
+ * Gets the x value.
+ */
+double Point2D::getX() {
+ return _x;
+}
+/**
+ * Sets the x value.
+ */
+void Point2D::setX(const double x=0.0) {
+ _x = x;
+}
+
+/**
+ * Gets the y value.
+ */
+double Point2D::getY() {
+ return _y;
+}
+/**
+ * Sets the y value.
+ */
+void Point2D::setY(const double y=0.0) {
+ _y = y;
+}
+
+Point2D& Point2D::operator=(const Point2D& other) {
+ // no need to check for self-assignment, since we're not
+ // allocating/deallocating memory.
+ _x = other._x;
+ _y = other._y;
+
+ return *this;
+}
+Point2D& Point2D::operator==(const Point2D& other) {
+ return (_x == other._x && _y == other._y);
+}
+Point2D& Point2D::operator!=(const Point2D& other) {
+ return !(*this == other);
+}
+Point2D& Point2D::operator*(const double val) {
+ Point2D retVal = *this;
+ retVal *= val;
+ return retVal;
+}
+Point2D& Point2D::operator/(const double val) {
+ Point2D retVal = *this;
+ retVal /= val;
+ return retVal;
+}
+Point2D& Point2D::operator*=(const double val) {
+ Point2D retVal = *this;
+ retVal._x *= val;
+ retVal._y *= val;
+ return retVal;
+}
+Point2D& Point2D::operator/=(const double val) {
+ Point2D retVal = *this;
+ retVal._x /= val;
+ retVal._y /= val;
+ return retVal;
+}

Added: branches/backus_dev/src/common/utility/Point2D.h
==============================================================================
--- (empty file)
+++ branches/backus_dev/src/common/utility/Point2D.h Mon Apr 27 14:19:22
2009
@@ -0,0 +1,99 @@
+/**
+ * Copyright (c) 2009 Jeff Backus.
+ *
+ * Licensed under the GNU General Public License, Version 2.0
(the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Additionally, this code makes heavy use of the Qt libraries from
+ * Trolltech. These libraries are released under the GNU
+ * General Public License, Version 2.0, as well. Please contact
+ * Trolltech for more information.
+ *
+ * http://trolltech.com/
+ *
+ */
+
+/**
+ * This class represents a generic point in two dimensions. All internal
+ * values are stored as doubles.
+ */
+
+#include <iostream>
+#include <meth.h>
+
+using namespace std;
+
+#include "misc_funcs.h"
+
+#ifndef __POINT2D_H__
+#define __POINT2D_H__
+
+class Point2D {
+
+ public:
+
+ /**
+ * Constructor.
+ *
+ * @param x The initial x value of the vector.
+ * @param y The initial y value of the vector.
+ */
+ Point2D(const double x=0.0, const double y=0.0);
+
+ /**
+ * Deconstructor
+ */
+ ~Point2D();
+
+ /**
+ * Gets the x value.
+ */
+ double getX();
+ /**
+ * Sets the x value.
+ */
+ void setX(const double x=0.0);
+
+ /**
+ * Gets the y value.
+ */
+ double getY();
+ /**
+ * Sets the y value.
+ */
+ void setY(const double y=0.0);
+
+ Point2D& operator=(const Point2D& other);
+ Point2D& operator==(const Point2D& other);
+ Point2D& operator!=(const Point2D& other);
+ // these operations geometrically don't make sense, but are for ease of
use.
+ Point2D& operator*(const double val);
+ Point2D& operator/(const double val);
+ Point2D& operator*=(const double val);
+ Point2D& operator/=(const double val);
+
+ private:
+ double _x;
+ double _y;
+};
+
+// **** Begin Friend definitions ****
+
+/**
+ * friend vector to dump to an ostream
+ */
+friend ostream& operator<<(ostream& os, const Point2D& point) {
+ os<<"("<<point.getX()<<","<<point.getY()<<")";
+ return os;
+}
+
+#endif

Added: branches/backus_dev/src/common/utility/Vector2D.cpp
==============================================================================
--- (empty file)
+++ branches/backus_dev/src/common/utility/Vector2D.cpp Mon Apr 27 14:19:22
2009
@@ -0,0 +1,239 @@
+
+#include "Vector2D.h"
+
+/**
+ * Constructor.
+ *
+ * @param x The initial x value of the vector.
+ * @param y The initial y value of the vector.
+ */
+Vector2D::Vector2D(const double x=0.0, const double y=0.0) {
+ _x = x;
+ _y = y;
+ _bIsNormalized = false;
+}
+
+/**
+ * Deconstructor
+ */
+Vector2D::~Vector2D() {
+}
+
+/**
+ * Gets the x value.
+ */
+double Vector2D::getX() {
+ return _x;
+}
+/**
+ * Sets the x value.
+ */
+void Vector2D::setX(const double x=0.0) {
+ _x = x;
+ _bIsNormalized = false;
+}
+
+/**
+ * Gets the y value.
+ */
+double Vector2D::getY() {
+ return _y;
+}
+/**
+ * Sets the y value.
+ */
+void Vector2D::setY(const double y=0.0) {
+ _y = y;
+ _bIsNormalized = false;
+}
+
+/**
+ * Gets the magnitude of the vector.
+ */
+double Vector2D::magnitude() {
+ if(_bIsNormalized) {
+ return 1.0;
+ } else {
+ return sqrt(_x*_x+_y*_y);
+ }
+}
+
+/**
+ * Gets the normal of this vector.
+ */
+Vector2D Vector2D::getNormal() {
+ Vector2D retVal;
+ // 2D case is easy!
+ retVal._x = -1.0*_y;
+ retVal._y = _x;
+
+ retVal.normalize();
+
+ return retVal;
+}
+
+/**
+ * Normalizes this vector.
+ */
+void Vector2D::normalize() {
+ if(!_bIsNormalized) {
+ double mag = magnitude();
+ _x = _x/mag;
+ _y = _y/mag;
+ _bIsNormalized = true;
+ }
+}
+
+/**
+ * Returns true if the specified vector is parallel to this vector.
+ */
+bool Vector2D::isParallelTo(const Vector2D other) {
+ Vector2D thisUnitV = *this;
+ Vector2D otherUnitV = other;
+ thisUnitV.normalize();
+ otherUnitV.normalize();
+
+ return
+ ( (thisUnitV._x == otherUnitV._x || thisUnitV._x ==
-1.0*otherUnitV._x) &&
+ (thisUnitV._y == otherUnitV._y || thisUnitV._y ==
-1.0*otherUnitV._y) );
+}
+
+/**
+ * Returns true if the specified vector is normal to this vector.
+ */
+bool Vector2D::isNormalTo(const Vector2D other) {
+ Vector2D otherNormV = other.getNormal();
+
+ return isParallel(otherNormV);
+}
+
+/**
+ * Returns the dot product of the specified vector with this vector.
+ */
+double Vector2D::dot(const Vector2D other) {
+ return (_x*other._x+_y*other._y);
+}
+
+/**
+ * Calculates the angle between the specified vector and this vector.
+ * Note: returns a value between [0, pi] or NaN if values are invalid.
+ */
+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 otherUnitV = other;
+ otherUnitV.normalize(); // dealing with normal vectors makes the math
easy.
+
+ return acos(norm.dot(otherUnitV));
+}
+
+/**
+ * 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 Vector2D::isOnLine(const Point2D a, const point2D b) {
+ // 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);
+}
+
+/**
+ * Finds the point of intersection of the line specified
+ * by this vector and point A and the line specified by
+ * vector V2 and point B. Returns false if the lines
+ * do not intersect. The resulting point is put into result.
+ * If result is NULL, the point will not be returned, but the
+ * function will still return true if the lines intersect or
+ * false if they do not.
+ *
+ * @param pA A point on the line specified by this vector.
+ * @param vB The vector describing the other line.
+ * @param pB A point on the line described by vector v.
+ * @param result The resulting point.
+ * @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;
+ if(denom != 0) {
+ if(result != NULL) {
+ Vector2D vF = (*this) * (numer / denom);
+ result = pA + vF;
+ }
+ }
+ return false;
+}
+
+Vector2D& Vector2D::operator=(const Vector2D& other) {
+ // no need to check for self-assignment, since we're not
+ // allocating/deallocating memory.
+ _x = other._x;
+ _y = other._y;
+ _bIsNormalized = other._bIsNormalized;
+
+ return *this;
+}
+Vector2D& Vector2D::operator+(const Vector2D& other) {
+ Vector2D retVal = *this;
+ retVal += other;
+ return retVal;
+}
+Vector2D& Vector2D::operator*(const double val) {
+ Vector2D retVal = *this;
+ retVal *= val;
+ return retVal;
+}
+Vector2D& Vector2D::operator*(const double val, Vector2D& v) {
+ return v*val;
+}
+Vector2D& Vector2D::operator/(const double val) {
+ Vector2D retVal = *this;
+ retVal /= val;
+ return retVal;
+}
+Vector2D& Vector2D::operator+=(const Vector2D& other) {
+ Vector2D retVal = *this;
+ retVal._x += other._x;
+ retVal._y += other._y;
+ retVal._bIsNormalized = false;
+ return retVal;
+}
+Vector2D& Vector2D::operator*=(const double val) {
+ Vector2D retVal = *this;
+ retVal._x *= val;
+ retVal._y *= val;
+ retVal._bIsNormalized = false;
+ return retVal;
+}
+Vector2D& Vector2D::operator/=(const double val) {
+ Vector2D retVal = *this;
+ retVal *= (1.0/val);
+ return retVal;
+}
+Vector2D& Vector2D::operator==(const Vector2D& other) {
+ return (_x == other._x && _y == other._y);
+}
+Vector2D& Vector2D::operator!=(const Vector2D& other) {
+ return !(*this == other);
+
+}
+Vector2D& Vector2D::operator-=(const Vector2D& other) {
+ Vector2D otherInv = other * -1.0;
+ Vector2D retVal = *this;
+ retVal += otherInv;
+ return retVal;
+}
+Vector2D& Vector2D::operator-(const Vector2D& other) {
+ Vector2D retVal = *this;
+ retVal -= other;
+ return retVal;
+}

Added: branches/backus_dev/src/common/utility/Vector2D.h
==============================================================================
--- (empty file)
+++ branches/backus_dev/src/common/utility/Vector2D.h Mon Apr 27 14:19:22
2009
@@ -0,0 +1,188 @@
+/**
+ * Copyright (c) 2009 Jeff Backus.
+ *
+ * Licensed under the GNU General Public License, Version 2.0
(the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Additionally, this code makes heavy use of the Qt libraries from
+ * Trolltech. These libraries are released under the GNU
+ * General Public License, Version 2.0, as well. Please contact
+ * Trolltech for more information.
+ *
+ * http://trolltech.com/
+ *
+ */
+
+/**
+ * This class represents a generic vector in two dimensions. All internal
+ * values are stored as doubles.
+ */
+
+#include <iostream>
+#include <math.h>
+
+using namespace std;
+
+#include "misc_funcs.h"
+#include "Point2D.h"
+
+#ifndef __VECTOR2D_H__
+#define __VECTOR2D_H__
+
+class Vector2D {
+
+ public:
+
+ /**
+ * Constructor.
+ *
+ * @param x The initial x value of the vector.
+ * @param y The initial y value of the vector.
+ */
+ Vector2D(const double x=0.0, const double y=0.0);
+
+ /**
+ * Deconstructor
+ */
+ ~Vector2D();
+
+ /**
+ * Gets the x value.
+ */
+ double getX();
+ /**
+ * Sets the x value.
+ */
+ void setX(const double x=0.0);
+ /**
+ * Gets the y value.
+ */
+ double getY();
+
+
+ /**
+ * Sets the y value.
+ */
+ void setY(const double y=0.0);
+
+ /**
+ * Gets the magnitude of the vector.
+ */
+ double magnitude();
+
+ /**
+ * Gets the normal of this vector.
+ */
+ Vector2D getNormal();
+
+ /**
+ * Normalizes this vector.
+ */
+ void normalize();
+
+ /**
+ * Returns true if the specified vector is parallel to this vector.
+ */
+ bool isParallelTo(const Vector2D other);
+
+ /**
+ * Returns true if the specified vector is normal to this vector.
+ */
+ bool isNormalTo(const Vector2D other);
+
+ /**
+ * Returns the dot product of the specified vector with this vector.
+ */
+ double dot(const Vector2D other);
+
+ /**
+ * 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);
+
+ /**
+ * Finds the point of intersection of the line specified
+ * by this vector and point A and the line specified by
+ * vector V2 and point B. Returns false if the lines
+ * do not intersect. The resulting point is put into result.
+ *
+ * @param a A point on the line specified by this vector.
+ * @param v2 The vector describing the other line.
+ * @param b A point on the line described by vector v.
+ * @param result The resulting point.
+ * @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);
+ 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 Vector2D& other);
+ Vector2D& operator!=(const Vector2D& other);
+ Vector2D& operator-=(const Vector2D& other);
+ Vector2D& operator-(const Vector2D& other);
+
+ private:
+ double _x;
+ double _y;
+
+ bool _bIsNormalized; //!< cuts down on excess calculations.
+};
+
+// **** Begin Friend definitions ****
+
+/**
+ * friend vector to dump to an ostream
+ */
+friend ostream& operator<<(ostream& os, const Vector2D& vector) {
+ os<<"("<<vector.getX()<<","<<vector.getY()<<")";
+ return os;
+}
+
+/**
+ * point subtraction results in a vector
+ */
+friend Vector2D& operator-(const Point2D& initial, const Point2D& final) {
+ double x = final.getX() - initial.getX();
+ double y = final.getY() - initial.getY();
+ Vector2D retVal(x, y);
+ return retVal;
+}
+
+/**
+ * A point plus a vector results in a new point.
+ */
+friend Point2D& operator+(const Point2D& p, const Vector2D& v) {
+ Point2D retVal;
+ retVal.setX(p.getX()+v.getX());
+ retVal.setY(p.getY()+v.getY());
+}
+friend Point2D& operator+(const Vector2D& v, const Point2D& p) {
+ return p+v;
+}
+
+#endif

Reply all
Reply to author
Forward
0 new messages