From 1212de942b0f930786da0dfa04360cdaea3e6493 Mon Sep 17 00:00:00 2001 From: Rodrigo Braz Monteiro Date: Thu, 5 Jul 2007 13:53:10 +0000 Subject: [PATCH] Added a 2D vector class from my old Allegro Wrapper, Halley. Originally committed to SVN as r1369. --- aegisub/Makefile.am | 1 + aegisub/vector2d.cpp | 183 +++++++++++++++++++++++++++++++++++++++++++ aegisub/vector2d.h | 76 ++++++++++++++++++ 3 files changed, 260 insertions(+) create mode 100644 aegisub/vector2d.cpp create mode 100644 aegisub/vector2d.h diff --git a/aegisub/Makefile.am b/aegisub/Makefile.am index 99836173e..1e790796f 100644 --- a/aegisub/Makefile.am +++ b/aegisub/Makefile.am @@ -205,6 +205,7 @@ aegisub_SOURCES = \ utils.cpp \ validators.cpp \ variable_data.cpp \ + vector2d.cpp \ version.cpp \ vfr.cpp \ video_box.cpp \ diff --git a/aegisub/vector2d.cpp b/aegisub/vector2d.cpp new file mode 100644 index 000000000..0fcce40c4 --- /dev/null +++ b/aegisub/vector2d.cpp @@ -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); +} diff --git a/aegisub/vector2d.h b/aegisub/vector2d.h new file mode 100644 index 000000000..076d022aa --- /dev/null +++ b/aegisub/vector2d.h @@ -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);