Aegisub/aegisub/vector2d.cpp
Rodrigo Braz Monteiro 1212de942b Added a 2D vector class from my old Allegro Wrapper, Halley.
Originally committed to SVN as r1369.
2007-07-05 13:53:10 +00:00

183 lines
4.1 KiB
C++

// Copyright (c) 2007, Rodrigo Braz Monteiro
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of the Aegisub Group nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// -----------------------------------------------------------------------------
//
// AEGISUB
//
// Website: http://aegisub.cellosoft.com
// Contact: mailto:zeratul@cellosoft.com
//
///////////
// Headers
#include "vector2d.h"
////////////////////
// Null constructor
Vector2D::Vector2D () {
x = y = 0;
}
////////////////////////
// Standard constructor
Vector2D::Vector2D (float _x,float _y) {
x = _x;
y = _y;
}
////////////////////////////////////
// Construction from another vector
Vector2D::Vector2D (const Vector2D &vec) {
x = vec.x;
y = vec.y;
}
//////////////
// Assignment
void Vector2D::operator = (const Vector2D param) {
x = param.x;
y = param.y;
}
//////////////
// Comparison
bool Vector2D::operator == (const Vector2D param) const {
return ((x == param.x) && (y == param.y));
}
bool Vector2D::operator != (const Vector2D param) const {
return ((x != param.x) || (y == param.y));
}
///////////
// Adition
Vector2D Vector2D::operator + (const Vector2D param) const {
return Vector2D(x + param.x,y + param.y);
}
Vector2D Vector2D::operator += (const Vector2D param) {
x += param.x;
y += param.y;
return *this;
}
///////////////
// Subtraction
Vector2D Vector2D::operator - (const Vector2D param) const {
return Vector2D(x - param.x,y - param.y);
}
Vector2D Vector2D::operator -= (const Vector2D param) {
x -= param.x;
y -= param.y;
return *this;
}
//////////
// Negate
Vector2D Vector2D::operator - () {
return Vector2D(-x,-y);
}
////////////////////////////
// Multiplication by scalar
Vector2D Vector2D::operator * (float param) {
return Vector2D(x * param,y * param);
}
Vector2D Vector2D::operator *= (float param) {
x *= param;
y *= param;
return *this;
}
Vector2D operator * (float f,const Vector2D &v) {
return Vector2D(v.x * f,v.y * f);
}
//////////////////////
// Division by scalar
Vector2D Vector2D::operator / (float param) {
return Vector2D(x / param,y / param);
}
Vector2D Vector2D::operator /= (float param) {
x /= param;
y /= param;
return *this;
}
Vector2D operator / (float f,const Vector2D &v) {
return Vector2D(v.x / f,v.y / f);
}
/////////////////
// Cross product
float Vector2D::Cross (const Vector2D param) const {
return x * param.y - y * param.x;
}
///////////////
// Dot product
float Vector2D::Dot (const Vector2D param) const {
return (x * param.x) + (y * param.y);
}
//////////
// Length
float Vector2D::Len () const {
return sqrt(x*x + y*y);
}
///////////
// Unitary
Vector2D Vector2D::Unit () {
float l = Len();
if (l != 0) {
Vector2D temp;
temp.x = x;
temp.y = y;
return temp / l;
}
else return Vector2D(0,0);
}