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/
|
|
|
|
|
|
|
|
/// @file vector2d.cpp
|
|
|
|
/// @brief 2D mathematical vector used in visual typesetting
|
|
|
|
/// @ingroup utility visual_ts
|
|
|
|
///
|
2007-07-05 15:53:10 +02:00
|
|
|
|
2009-09-10 15:06:40 +02:00
|
|
|
#include "vector2d.h"
|
2007-07-05 15:53:10 +02:00
|
|
|
|
2013-07-31 05:31:10 +02:00
|
|
|
#include "utils.h"
|
|
|
|
|
2014-05-29 00:19:05 +02:00
|
|
|
#include <libaegisub/format.h>
|
|
|
|
|
2011-11-06 18:18:20 +01:00
|
|
|
#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-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 {
|
2014-12-29 04:29:02 +01:00
|
|
|
if (std::abs(x) < std::abs(y))
|
2011-11-06 18:18:20 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-08-20 03:17:42 +02:00
|
|
|
Vector2D::operator bool() const {
|
|
|
|
return *this != Vector2D();
|
2007-07-05 15:53:10 +02:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string Vector2D::PStr(char sep) const {
|
2011-11-06 18:18:20 +01:00
|
|
|
return "(" + Str(sep) + ")";
|
2007-07-05 15:53:10 +02:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string Vector2D::DStr(char sep) const {
|
2014-05-29 00:19:05 +02:00
|
|
|
return agi::format("%d%c%d", (int)x, sep, (int)y);
|
2007-07-07 10:53:11 +02:00
|
|
|
}
|
|
|
|
|
2013-01-04 16:01:50 +01:00
|
|
|
std::string Vector2D::Str(char sep) const {
|
2012-07-06 04:14:46 +02:00
|
|
|
return float_to_string(x) + sep + float_to_string(y);
|
2007-07-05 15:53:10 +02:00
|
|
|
}
|