2011-11-06 18:18:20 +01:00
|
|
|
// Copyright (c) 2011, Thomas Goyne <plorkyeran@aegisub.org>
|
2007-07-05 15:53:10 +02:00
|
|
|
//
|
2011-11-06 18:18:20 +01:00
|
|
|
// Permission to use, copy, modify, and distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
2007-07-05 15:53:10 +02:00
|
|
|
//
|
2011-11-06 18:18:20 +01:00
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2007-07-05 15:53:10 +02:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// Aegisub Project http://www.aegisub.org/
|
2007-07-05 15:53:10 +02:00
|
|
|
//
|
2009-07-29 07:43:02 +02:00
|
|
|
// $Id$
|
|
|
|
|
|
|
|
/// @file vector2d.cpp
|
|
|
|
/// @brief 2D mathematical vector used in visual typesetting
|
|
|
|
/// @ingroup utility visual_ts
|
|
|
|
///
|
2007-07-05 15:53:10 +02:00
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#include "config.h"
|
|
|
|
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "vector2d.h"
|
2007-07-05 15:53:10 +02:00
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
#ifndef AGI_PRE
|
|
|
|
#include <limits>
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
#include <wx/numformatter.h>
|
|
|
|
#endif
|
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
|
|
|
|
2011-12-07 00:13:06 +01:00
|
|
|
Vector2D::Vector2D()
|
|
|
|
: x(std::numeric_limits<float>::min())
|
|
|
|
, y(std::numeric_limits<float>::min())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
Vector2D operator *(float f, Vector2D v) {
|
|
|
|
return Vector2D(v.X() * f, v.Y() * f);
|
2007-07-05 15:53:10 +02:00
|
|
|
}
|
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
Vector2D operator /(float f, Vector2D v) {
|
|
|
|
return Vector2D(f / v.X(), f / v.Y());
|
2007-07-05 15:53:10 +02:00
|
|
|
}
|
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
Vector2D operator +(float f, Vector2D v) {
|
|
|
|
return Vector2D(v.X() + f, v.Y() + f);
|
2007-07-05 15:53:10 +02:00
|
|
|
}
|
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
Vector2D operator -(float f, Vector2D v) {
|
|
|
|
return Vector2D(f - v.X(), f - v.Y());
|
2007-07-05 15:53:10 +02:00
|
|
|
}
|
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
Vector2D Vector2D::Unit() const {
|
|
|
|
float len = Len();
|
|
|
|
if (len == 0)
|
|
|
|
return Vector2D(0, 0);
|
|
|
|
return *this / len;
|
2007-07-05 15:53:10 +02:00
|
|
|
}
|
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
Vector2D Vector2D::SingleAxis() const {
|
|
|
|
if (abs(x) < abs(y))
|
|
|
|
return Vector2D(0, y);
|
|
|
|
else
|
|
|
|
return Vector2D(x, 0);
|
2007-07-05 15:53:10 +02:00
|
|
|
}
|
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
Vector2D Vector2D::Max(Vector2D param) const {
|
|
|
|
return Vector2D(std::max(x, param.x), std::max(y, param.y));
|
2007-07-05 15:53:10 +02:00
|
|
|
}
|
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
Vector2D Vector2D::Min(Vector2D param) const {
|
|
|
|
return Vector2D(std::min(x, param.x), std::min(y, param.y));
|
2007-07-05 15:53:10 +02:00
|
|
|
}
|
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
Vector2D Vector2D::Round(float step) const {
|
|
|
|
return Vector2D(floorf(x / step + .5f) * step, floorf(y / step + .5f) * step);
|
2007-07-05 15:53:10 +02:00
|
|
|
}
|
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
Vector2D::operator unspecified_bool_type() const {
|
2011-12-07 00:13:06 +01:00
|
|
|
return *this == Vector2D() ? 0 : &Vector2D::x;
|
2007-07-05 15:53:10 +02:00
|
|
|
}
|
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
wxString Vector2D::PStr(char sep) const {
|
|
|
|
return "(" + Str(sep) + ")";
|
2007-07-05 15:53:10 +02:00
|
|
|
}
|
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
wxString Vector2D::DStr(char sep) const {
|
|
|
|
return wxString::Format("%d%c%d", (int)x, sep, (int)y);
|
2007-07-07 10:53:11 +02:00
|
|
|
}
|
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
wxString Vector2D::Str(char sep) const {
|
|
|
|
return
|
|
|
|
wxNumberFormatter::ToString(x, 3, wxNumberFormatter::Style_NoTrailingZeroes) +
|
|
|
|
sep +
|
|
|
|
wxNumberFormatter::ToString(y, 3, wxNumberFormatter::Style_NoTrailingZeroes);
|
2007-07-05 15:53:10 +02:00
|
|
|
}
|