123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- /**
- * Copyright (c) 2006-2013 LOVE Development Team
- *
- * This software is provided 'as-is', without any express or implied
- * warranty. In no event will the authors be held liable for any damages
- * arising from the use of this software.
- *
- * Permission is granted to anyone to use this software for any purpose,
- * including commercial applications, and to alter it and redistribute it
- * freely, subject to the following restrictions:
- *
- * 1. The origin of this software must not be misrepresented; you must not
- * claim that you wrote the original software. If you use this software
- * in a product, an acknowledgment in the product documentation would be
- * appreciated but is not required.
- * 2. Altered source versions must be plainly marked as such, and must not be
- * misrepresented as being the original software.
- * 3. This notice may not be removed or altered from any source distribution.
- **/
- #ifndef LOVE_VECTOR_H
- #define LOVE_VECTOR_H
- // STD
- #include <cmath>
- // LOVE
- #include "Matrix.h"
- namespace love
- {
- /**
- * 2D Vector class.
- *
- * @author Anders Ruud
- * @date 2006-05-13
- **/
- class Vector
- {
- public:
- // The components.
- float x, y;
- /**
- * Creates a new (1,1) Vector.
- **/
- Vector();
- /**
- * Creates a new Vector.
- * @param x The x position/dimension.
- * @param y The y position/dimension.
- **/
- Vector(float x, float y);
- /**
- * Gets the length of the Vector.
- * @return The length of the Vector.
- *
- * This method requires sqrtf() and should be used
- * carefully.
- **/
- float getLength() const;
- /**
- * Normalizes the Vector.
- * @param length Desired length of the vector.
- * @return The old length of the Vector.
- **/
- float normalize(float length = 1.0);
- /**
- * Gets a vector perpendicular to the Vector.
- * To get the true (normalized) normal, use v.getNormal(1.0f / v.getLength())
- * @return A normal to the Vector.
- **/
- Vector getNormal() const;
- /**
- * Gets a vector perpendicular to the Vector.
- * To get the true (normalized) normal, use v.getNormal(1.0f / v.getLength())
- * @param Scale factor to apply.
- * @return A normal to the Vector.
- **/
- Vector getNormal(float scale) const;
- /**
- * Adds a Vector to this Vector.
- * @param v The Vector we want to add to this Vector.
- * @return The resulting Vector.
- **/
- Vector operator + (const Vector &v) const;
- /**
- * Substracts a Vector to this Vector.
- * @param v The Vector we want to subtract to this Vector.
- * @return The resulting Vector.
- **/
- Vector operator - (const Vector &v) const;
- /**
- * Resizes a Vector by a scalar.
- * @param s The scalar with which to resize the Vector.
- * @return The resulting Vector.
- **/
- Vector operator * (float s) const;
- /**
- * Resizes a Vector by a scalar.
- * @param s The scalar with which to resize the Vector.
- * @return The resulting Vector.
- **/
- Vector operator / (float s) const;
- /**
- * Reverses the Vector.
- * @return The reversed Vector.
- **/
- Vector operator - () const;
- /**
- * Adds a Vector to this Vector, and also saves changes in the first Vector.
- * @param v The Vector we want to add to this Vector.
- **/
- void operator += (const Vector &v);
- /**
- * Subtracts a Vector to this Vector, and also saves changes in the first Vector.
- * @param v The Vector we want to subtract to this Vector.
- **/
- void operator -= (const Vector &v);
- /**
- * Resizes the Vector, and also saves changes in the first Vector.
- * @param s The scalar by which we want to resize the Vector.
- **/
- void operator *= (float s);
- /**
- * Resizes the Vector, and also saves changes in the first Vector.
- * @param s The scalar by which we want to resize the Vector.
- **/
- void operator /= (float s);
- /**
- * Calculates the dot product of two Vectors.
- * @return The dot product of the two Vectors.
- **/
- float operator * (const Vector &v) const;
- /**
- * Calculates the cross product of two Vectors.
- * @return The cross product of the two Vectors.
- **/
- float operator ^ (const Vector &v) const;
- bool operator == (const Vector &v) const;
- bool operator < (const Vector &v) const;
- /**
- * Gets the x value of the Vector.
- * @return The x value of the Vector.
- **/
- float getX() const;
- /**
- * Gets the x value of the Vector.
- * @return The x value of the Vector.
- **/
- float getY() const;
- /**
- * Sets the x value of the Vector.
- * @param The x value of the Vector.
- **/
- void setX(float x);
- /**
- * Sets the x value of the Vector.
- * @param The x value of the Vector.
- **/
- void setY(float y);
- };
- inline float Vector::getLength() const
- {
- return sqrtf(x*x + y*y);
- }
- inline Vector Vector::getNormal() const
- {
- return Vector(-y, x);
- }
- inline Vector Vector::getNormal(float scale) const
- {
- return Vector(-y * scale, x * scale);
- }
- inline float Vector::normalize(float length)
- {
- float length_current = getLength();
- if (length_current > 0)
- (*this) *= length / length_current;
- return length_current;
- }
- /**
- * Inline methods must have body in header.
- **/
- inline Vector::Vector()
- {
- x = 1;
- y = 1;
- }
- inline Vector::Vector(float x, float y)
- {
- this->x = x;
- this->y = y;
- }
- inline Vector Vector::operator + (const Vector &v) const
- {
- return Vector(x + v.x, y + v.y);
- }
- inline Vector Vector::operator - (const Vector &v) const
- {
- return Vector(x - v.getX(), y - v.getY());
- }
- inline Vector Vector::operator * (float s) const
- {
- return Vector(x*s, y*s);
- }
- inline Vector Vector::operator / (float s) const
- {
- return Vector(x/s, y/s);
- }
- inline Vector Vector::operator - () const
- {
- return Vector(-x, -y);
- }
- inline void Vector::operator += (const Vector &v)
- {
- x += v.getX();
- y += v.getY();
- }
- inline void Vector::operator -= (const Vector &v)
- {
- x -= v.getX();
- y -= v.getY();
- }
- inline void Vector::operator *= (float s)
- {
- x *= s;
- y *= s;
- }
- inline void Vector::operator /= (float s)
- {
- x /= s;
- y /= s;
- }
- inline float Vector::operator * (const Vector &v) const
- {
- return x * v.getX() + y * v.getY();
- }
- inline float Vector::operator ^ (const Vector &v) const
- {
- return x * v.getY() - y * v.getX();
- }
- inline bool Vector::operator == (const Vector &v) const
- {
- return getLength() == v.getLength();
- }
- inline bool Vector::operator < (const Vector &v) const
- {
- return getLength() < v.getLength();
- }
- /**
- * Accessor methods
- **/
- inline float Vector::getX() const
- {
- return x;
- }
- inline float Vector::getY() const
- {
- return y;
- }
- inline void Vector::setX(float x)
- {
- this->x = x;
- }
- inline void Vector::setY(float y)
- {
- this->y = y;
- }
- } //love
- #endif// LOVE_VECTOR_H
|