aocpp/lib/src/Coord.cpp
2024-09-02 15:20:25 -07:00

119 lines
2.1 KiB
C++

#include <aocpp/Coord.hpp>
#include <cstdlib>
namespace aocpp {
auto Draw(std::ostream & out, std::map<Coord, std::int64_t> const& image) -> void {
std::int64_t minX = 0, maxX = 0, minY = 0, maxY = 0;
for (auto&& [k, _] : image) {
auto [x,y] = k;
minX = std::min(minX, x);
maxX = std::max(maxX, x);
minY = std::min(minY, y);
maxY = std::max(maxY, y);
}
for (std::int64_t y = minY; y <= maxY; y++) {
for (std::int64_t x = minX; x <= maxX; x++) {
auto it = image.find({x,y});
if (image.end() == it) {
out << "";
} else switch (it->second) {
case 0: out << ""; break;
case 1: out << ""; break;
case 2: out << ""; break;
case 3: out << ""; break;
case 4: out << ""; break;
default: out << ""; break;
}
}
out << "\n";
}
}
auto Up(Coord c) -> Coord {
c.y -= 1;
return c;
}
auto Down(Coord c) -> Coord {
c.y += 1;
return c;
}
auto Left(Coord c) -> Coord {
c.x -= 1;
return c;
}
auto Right(Coord c) -> Coord {
c.x += 1;
return c;
}
auto Invert(Coord c) -> Coord {
return {c.y, c.x};
}
auto CW(Coord c) -> Coord {
c = Invert(c);
c.x = -c.x;
return c;
}
auto CCW(Coord c) -> Coord {
return CW(CW(CW(c)));
}
auto Turn180(Coord c) -> Coord {
return CW(CW(c));
}
auto Norm1(Coord c) -> std::int64_t {
return std::abs(c.x) + std::abs(c.y);
}
auto NormInf(Coord c) -> std::int64_t {
return std::max(std::abs(c.x), std::abs(c.y));
}
auto operator+(Coord a, Coord b) -> Coord {
return {a.x + b.x, a.y + b.y};
}
auto operator-(Coord a, Coord b) -> Coord {
return {a.x - b.x, a.y - b.y};
}
auto operator+=(Coord & a, Coord b) -> Coord & {
a = a + b;
return a;
}
auto operator-=(Coord & a, Coord b) -> Coord & {
a = a - b;
return a;
}
auto operator*(Coord a, std::int64_t b) -> Coord {
return a *= b;
}
auto operator*(std::int64_t a, Coord b) -> Coord {
b.x *= a;
b.y *= a;
return b;
}
auto operator*=(Coord & a, std::int64_t b) -> Coord & {
a.x *= b;
a.y *= b;
return a;
}
auto operator<<(std::ostream & out, Coord c) -> std::ostream & {
return out << "(" << c.x << "," << c.y << ")";
}
} // namespace