61 lines
1.6 KiB
C++
61 lines
1.6 KiB
C++
#include "pgm_drawing_context.hpp"
|
|
#include <cmath>
|
|
|
|
PgmDrawingContext::PgmDrawingContext(const std::filesystem::path& path, size_t width, size_t height)
|
|
: m_bitmap(width, height, Color{0xFF})
|
|
{
|
|
}
|
|
|
|
void PgmDrawingContext::DrawLine(const math::Vector& p0, const math::Vector& p1)
|
|
{
|
|
RasterizeLine(static_cast<int>(p0.x), static_cast<int>(p0.y), static_cast<int>(p1.x), static_cast<int>(p1.y));
|
|
}
|
|
|
|
void PgmDrawingContext::DrawRectangle(const math::Vector& pos, const math::Vector& size, float angle)
|
|
{
|
|
const float sina = std::sin(angle);
|
|
const float cosa = std::cos(angle);
|
|
|
|
const math::Vector bX{cosa, sina};
|
|
const math::Vector bY{-sina, cosa};
|
|
|
|
const auto& pD = pos;
|
|
const auto pA = pD + bY * size.y;
|
|
const auto pB = pA + bX * size.x;
|
|
const auto pC = pD + bX * size.x;
|
|
|
|
PgmDrawingContext::DrawLine(pA, pB);
|
|
PgmDrawingContext::DrawLine(pB, pC);
|
|
PgmDrawingContext::DrawLine(pC, pD);
|
|
PgmDrawingContext::DrawLine(pD, pA);
|
|
}
|
|
|
|
void PgmDrawingContext::DrawCircle(const math::Vector& center, float radius)
|
|
{
|
|
}
|
|
|
|
void PgmDrawingContext::RasterizeLine(int x0, int y0, int x1, int y1)
|
|
{
|
|
int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
|
|
int dy = -abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
|
|
int err = dx + dy, e2; /* error value e_xy */
|
|
|
|
for (;;)
|
|
{ /* loop */
|
|
m_bitmap[y0][x0].l = 0x00;
|
|
if (x0 == x1 && y0 == y1)
|
|
break;
|
|
e2 = 2 * err;
|
|
if (e2 >= dy)
|
|
{
|
|
err += dy;
|
|
x0 += sx;
|
|
} /* e_xy+e_x > 0 */
|
|
if (e2 <= dx)
|
|
{
|
|
err += dx;
|
|
y0 += sy;
|
|
} /* e_xy+e_y < 0 */
|
|
}
|
|
}
|