103 lines
2.0 KiB
C++
103 lines
2.0 KiB
C++
|
///
|
||
|
/// \file Cursor.cc
|
||
|
/// \author kyle
|
||
|
/// \created 10/11/23
|
||
|
/// \brief
|
||
|
///
|
||
|
/// \section COPYRIGHT
|
||
|
/// Copyright 2023 K. Isom <kyle@imap.cc>
|
||
|
///
|
||
|
/// Permission to use, copy, modify, and/or 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.
|
||
|
///
|
||
|
/// 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.
|
||
|
///
|
||
|
|
||
|
#include "Cursor.h"
|
||
|
#include "Defs.h"
|
||
|
|
||
|
|
||
|
/// \todo kyle Can cursors be replaced with std::pair?
|
||
|
|
||
|
|
||
|
static std::pair<size_t, size_t>
|
||
|
orderedPair(size_t a, size_t b)
|
||
|
{
|
||
|
if (a > b) {
|
||
|
return {b, a};
|
||
|
}
|
||
|
|
||
|
return {a, b};
|
||
|
}
|
||
|
|
||
|
|
||
|
static size_t
|
||
|
isqrt(size_t n)
|
||
|
{
|
||
|
if (n < 2) {
|
||
|
return n;
|
||
|
}
|
||
|
|
||
|
size_t start = 0;
|
||
|
size_t end = n / 2;
|
||
|
size_t result = 0;
|
||
|
|
||
|
while (start <= end) {
|
||
|
auto middle = (start + end) >> 1;
|
||
|
result = middle * middle;
|
||
|
if (result == n) {
|
||
|
return middle;
|
||
|
}
|
||
|
|
||
|
if (result < n) {
|
||
|
start = middle + 1;
|
||
|
result = middle;
|
||
|
} else {
|
||
|
end = middle - 1;
|
||
|
result = middle;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
|
||
|
Cursor Cursor::Update(const Cursor &c)
|
||
|
{
|
||
|
Cursor prev(this->x, this->y);
|
||
|
this->X(c.X());
|
||
|
this->Y(c.Y());
|
||
|
return prev;
|
||
|
}
|
||
|
|
||
|
|
||
|
size_t Cursor::Distance(const Cursor &c) const
|
||
|
{
|
||
|
auto xPair = orderedPair(this->x, c.X());
|
||
|
auto yPair = orderedPair(this->y, c.Y());
|
||
|
|
||
|
auto x0 = xPair.second - xPair.first;
|
||
|
x0 *= x0;
|
||
|
|
||
|
auto y0 = yPair.second - xPair.first;
|
||
|
y0 *= y0;
|
||
|
|
||
|
auto dist = x0 + y0;
|
||
|
return isqrt(dist);
|
||
|
}
|
||
|
|
||
|
|
||
|
std::ostream &operator<<(std::ostream &os, const Cursor &cursor)
|
||
|
{
|
||
|
os << cursor.X() << "," << cursor.Y();
|
||
|
return os;
|
||
|
}
|