/*1* Vector2.h2* RVO2 Library3*4* Copyright 2008 University of North Carolina at Chapel Hill5*6* Licensed under the Apache License, Version 2.0 (the "License");7* you may not use this file except in compliance with the License.8* You may obtain a copy of the License at9*10* http://www.apache.org/licenses/LICENSE-2.011*12* Unless required by applicable law or agreed to in writing, software13* distributed under the License is distributed on an "AS IS" BASIS,14* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15* See the License for the specific language governing permissions and16* limitations under the License.17*18* Please send all bug reports to <[email protected]>.19*20* The authors may be contacted via:21*22* Jur van den Berg, Stephen J. Guy, Jamie Snape, Ming C. Lin, Dinesh Manocha23* Dept. of Computer Science24* 201 S. Columbia St.25* Frederick P. Brooks, Jr. Computer Science Bldg.26* Chapel Hill, N.C. 27599-317527* United States of America28*29* <http://gamma.cs.unc.edu/RVO2/>30*/3132#ifndef RVO_VECTOR2_H_33#define RVO_VECTOR2_H_3435/**36* \file Vector2.h37* \brief Contains the Vector2 class.38*/3940#include <cmath>41#include <ostream>4243namespace RVO2D {44/**45* \brief Defines a two-dimensional vector.46*/47class Vector2 {48public:49/**50* \brief Constructs and initializes a two-dimensional vector instance51* to (0.0, 0.0).52*/53inline Vector2() : x_(0.0f), y_(0.0f) { }5455/**56* \brief Constructs and initializes a two-dimensional vector from57* the specified xy-coordinates.58* \param x The x-coordinate of the two-dimensional59* vector.60* \param y The y-coordinate of the two-dimensional61* vector.62*/63inline Vector2(float x, float y) : x_(x), y_(y) { }6465inline Vector2(const Vector2 &vector)66{67x_ = vector.x();68y_ = vector.y();69}7071/**72* \brief Returns the x-coordinate of this two-dimensional vector.73* \return The x-coordinate of the two-dimensional vector.74*/75inline float x() const { return x_; }7677/**78* \brief Returns the y-coordinate of this two-dimensional vector.79* \return The y-coordinate of the two-dimensional vector.80*/81inline float y() const { return y_; }8283/**84* \brief Computes the negation of this two-dimensional vector.85* \return The negation of this two-dimensional vector.86*/87inline Vector2 operator-() const88{89return Vector2(-x_, -y_);90}9192/**93* \brief Computes the dot product of this two-dimensional vector with94* the specified two-dimensional vector.95* \param vector The two-dimensional vector with which the96* dot product should be computed.97* \return The dot product of this two-dimensional vector with a98* specified two-dimensional vector.99*/100inline float operator*(const Vector2 &vector) const101{102return x_ * vector.x() + y_ * vector.y();103}104105/**106* \brief Computes the scalar multiplication of this107* two-dimensional vector with the specified scalar value.108* \param s The scalar value with which the scalar109* multiplication should be computed.110* \return The scalar multiplication of this two-dimensional vector111* with a specified scalar value.112*/113inline Vector2 operator*(float s) const114{115return Vector2(x_ * s, y_ * s);116}117118/**119* \brief Computes the scalar division of this two-dimensional vector120* with the specified scalar value.121* \param s The scalar value with which the scalar122* division should be computed.123* \return The scalar division of this two-dimensional vector with a124* specified scalar value.125*/126inline Vector2 operator/(float s) const127{128const float invS = 1.0f / s;129130return Vector2(x_ * invS, y_ * invS);131}132133/**134* \brief Computes the vector sum of this two-dimensional vector with135* the specified two-dimensional vector.136* \param vector The two-dimensional vector with which the137* vector sum should be computed.138* \return The vector sum of this two-dimensional vector with a139* specified two-dimensional vector.140*/141inline Vector2 operator+(const Vector2 &vector) const142{143return Vector2(x_ + vector.x(), y_ + vector.y());144}145146/**147* \brief Computes the vector difference of this two-dimensional148* vector with the specified two-dimensional vector.149* \param vector The two-dimensional vector with which the150* vector difference should be computed.151* \return The vector difference of this two-dimensional vector with a152* specified two-dimensional vector.153*/154inline Vector2 operator-(const Vector2 &vector) const155{156return Vector2(x_ - vector.x(), y_ - vector.y());157}158159/**160* \brief Tests this two-dimensional vector for equality with the161* specified two-dimensional vector.162* \param vector The two-dimensional vector with which to163* test for equality.164* \return True if the two-dimensional vectors are equal.165*/166inline bool operator==(const Vector2 &vector) const167{168return x_ == vector.x() && y_ == vector.y();169}170171/**172* \brief Tests this two-dimensional vector for inequality with the173* specified two-dimensional vector.174* \param vector The two-dimensional vector with which to175* test for inequality.176* \return True if the two-dimensional vectors are not equal.177*/178inline bool operator!=(const Vector2 &vector) const179{180return x_ != vector.x() || y_ != vector.y();181}182183/**184* \brief Sets the value of this two-dimensional vector to the scalar185* multiplication of itself with the specified scalar value.186* \param s The scalar value with which the scalar187* multiplication should be computed.188* \return A reference to this two-dimensional vector.189*/190inline Vector2 &operator*=(float s)191{192x_ *= s;193y_ *= s;194195return *this;196}197198/**199* \brief Sets the value of this two-dimensional vector to the scalar200* division of itself with the specified scalar value.201* \param s The scalar value with which the scalar202* division should be computed.203* \return A reference to this two-dimensional vector.204*/205inline Vector2 &operator/=(float s)206{207const float invS = 1.0f / s;208x_ *= invS;209y_ *= invS;210211return *this;212}213214/**215* \brief Sets the value of this two-dimensional vector to the vector216* sum of itself with the specified two-dimensional vector.217* \param vector The two-dimensional vector with which the218* vector sum should be computed.219* \return A reference to this two-dimensional vector.220*/221inline Vector2 &operator+=(const Vector2 &vector)222{223x_ += vector.x();224y_ += vector.y();225226return *this;227}228229/**230* \brief Sets the value of this two-dimensional vector to the vector231* difference of itself with the specified two-dimensional232* vector.233* \param vector The two-dimensional vector with which the234* vector difference should be computed.235* \return A reference to this two-dimensional vector.236*/237inline Vector2 &operator-=(const Vector2 &vector)238{239x_ -= vector.x();240y_ -= vector.y();241242return *this;243}244245inline Vector2 &operator=(const Vector2 &vector)246{247x_ = vector.x();248y_ = vector.y();249250return *this;251}252253private:254float x_;255float y_;256};257258/**259* \relates Vector2260* \brief Computes the scalar multiplication of the specified261* two-dimensional vector with the specified scalar value.262* \param s The scalar value with which the scalar263* multiplication should be computed.264* \param vector The two-dimensional vector with which the scalar265* multiplication should be computed.266* \return The scalar multiplication of the two-dimensional vector with the267* scalar value.268*/269inline Vector2 operator*(float s, const Vector2 &vector)270{271return Vector2(s * vector.x(), s * vector.y());272}273274/**275* \relates Vector2276* \brief Inserts the specified two-dimensional vector into the specified277* output stream.278* \param os The output stream into which the two-dimensional279* vector should be inserted.280* \param vector The two-dimensional vector which to insert into281* the output stream.282* \return A reference to the output stream.283*/284inline std::ostream &operator<<(std::ostream &os, const Vector2 &vector)285{286os << "(" << vector.x() << "," << vector.y() << ")";287288return os;289}290291/**292* \relates Vector2293* \brief Computes the length of a specified two-dimensional vector.294* \param vector The two-dimensional vector whose length is to be295* computed.296* \return The length of the two-dimensional vector.297*/298inline float abs(const Vector2 &vector)299{300return std::sqrt(vector * vector);301}302303/**304* \relates Vector2305* \brief Computes the squared length of a specified two-dimensional306* vector.307* \param vector The two-dimensional vector whose squared length308* is to be computed.309* \return The squared length of the two-dimensional vector.310*/311inline float absSq(const Vector2 &vector)312{313return vector * vector;314}315316/**317* \relates Vector2318* \brief Computes the determinant of a two-dimensional square matrix with319* rows consisting of the specified two-dimensional vectors.320* \param vector1 The top row of the two-dimensional square321* matrix.322* \param vector2 The bottom row of the two-dimensional square323* matrix.324* \return The determinant of the two-dimensional square matrix.325*/326inline float det(const Vector2 &vector1, const Vector2 &vector2)327{328return vector1.x() * vector2.y() - vector1.y() * vector2.x();329}330331/**332* \relates Vector2333* \brief Computes the normalization of the specified two-dimensional334* vector.335* \param vector The two-dimensional vector whose normalization336* is to be computed.337* \return The normalization of the two-dimensional vector.338*/339inline Vector2 normalize(const Vector2 &vector)340{341return vector / abs(vector);342}343}344345#endif /* RVO_VECTOR2_H_ */346347348