Added a 2D vector class from my old Allegro Wrapper, Halley.

Originally committed to SVN as r1369.
This commit is contained in:
Rodrigo Braz Monteiro 2007-07-05 13:53:10 +00:00
parent 06c2a7f6c3
commit 1212de942b
3 changed files with 260 additions and 0 deletions

View File

@ -205,6 +205,7 @@ aegisub_SOURCES = \
utils.cpp \
validators.cpp \
variable_data.cpp \
vector2d.cpp \
version.cpp \
vfr.cpp \
video_box.cpp \

183
aegisub/vector2d.cpp Normal file
View File

@ -0,0 +1,183 @@
// 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);
}

76
aegisub/vector2d.h Normal file
View File

@ -0,0 +1,76 @@
// 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
//
#pragma once
//////////////////////////////
// Vector2D class declaration
class Vector2D {
public:
float x,y;
Vector2D ();
Vector2D (float _x,float _y);
Vector2D (const Vector2D &vec);
void operator = (const Vector2D param);
bool operator == (const Vector2D param) const;
bool operator != (const Vector2D param) const;
Vector2D operator - ();
Vector2D operator + (const Vector2D param) const;
Vector2D operator - (const Vector2D param) const;
Vector2D operator * (float param);
Vector2D operator / (float param);
Vector2D operator += (const Vector2D param);
Vector2D operator -= (const Vector2D param);
Vector2D operator *= (float param);
Vector2D operator /= (float param);
Vector2D Unit ();
float Cross (const Vector2D param) const;
virtual float Dot (const Vector2D param) const;
virtual float Len () const;
};
////////////////////
// Global operators
Vector2D operator * (float f,const Vector2D &v);
Vector2D operator / (float f,const Vector2D &v);