2026-07-07 06:47:24 -07:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
2026-07-07 08:09:54 -07:00
|
|
|
#include "../datatypes.cpp"
|
|
|
|
|
#include "../helpers.cpp"
|
|
|
|
|
#include "../buffer.cpp"
|
2026-07-20 02:04:17 -07:00
|
|
|
#include "../shaders/shaders.h"
|
2026-07-07 06:47:24 -07:00
|
|
|
|
|
|
|
|
namespace UwU {
|
|
|
|
|
// line
|
|
|
|
|
class Line {
|
|
|
|
|
private:
|
|
|
|
|
// plot lines if m <= 1
|
2026-07-20 02:04:17 -07:00
|
|
|
template <class ShaderT>
|
|
|
|
|
void lineLow(int16_t ix0, int16_t iy0, int16_t ix1, int16_t iy1, Buffer buffer, ShaderBase<ShaderT>& shader) {
|
2026-07-07 06:47:24 -07:00
|
|
|
int16_t dx = ix1 - ix0;
|
|
|
|
|
int16_t dy = iy1 - iy0;
|
|
|
|
|
int16_t yi = 1;
|
|
|
|
|
if (dy < 0) {
|
|
|
|
|
yi = -1;
|
|
|
|
|
dy = -dy;
|
|
|
|
|
}
|
|
|
|
|
int16_t d = (2 * dy) - dx;
|
|
|
|
|
int16_t y = iy0;
|
2026-07-08 16:15:48 -07:00
|
|
|
|
2026-07-20 02:04:17 -07:00
|
|
|
for (int16_t x = ix0; x < ix1; x++) {
|
|
|
|
|
buffer.drawPixel((uint16_t)x, (uint16_t)y, shader.shade(x, y));
|
|
|
|
|
if (d > 0) {
|
|
|
|
|
y += yi;
|
|
|
|
|
d += (2 * (dy - dx));
|
|
|
|
|
} else {
|
|
|
|
|
d += (2 * dy);
|
2026-07-08 17:13:57 -07:00
|
|
|
}
|
2026-07-07 06:47:24 -07:00
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// plot lines if m >1
|
2026-07-20 02:04:17 -07:00
|
|
|
template <class ShaderT>
|
|
|
|
|
void lineHigh(int16_t ix0, int16_t iy0, int16_t ix1, int16_t iy1, Buffer buffer, ShaderBase<ShaderT>& shader) {
|
2026-07-07 06:47:24 -07:00
|
|
|
int16_t dx = ix1 - ix0;
|
|
|
|
|
int16_t dy = iy1 - iy0;
|
|
|
|
|
int16_t xi = 1;
|
|
|
|
|
if (dx < 0) {
|
|
|
|
|
xi = -1;
|
|
|
|
|
dx = -dx;
|
|
|
|
|
}
|
|
|
|
|
int16_t d = (2 * dx) - dy;
|
|
|
|
|
int16_t x = ix0;
|
2026-07-08 16:15:48 -07:00
|
|
|
|
2026-07-20 02:04:17 -07:00
|
|
|
for (int16_t y = iy0; y < iy1; y++) {
|
|
|
|
|
buffer.drawPixel((uint16_t)x, (uint16_t)y, shader.shade(x, y));
|
|
|
|
|
if (d > 0) {
|
|
|
|
|
x += xi;
|
|
|
|
|
d += (2 * (dx - dy));
|
|
|
|
|
} else {
|
|
|
|
|
d += (2 * dx);
|
2026-07-08 16:15:48 -07:00
|
|
|
}
|
2026-07-20 02:04:17 -07:00
|
|
|
}
|
|
|
|
|
return;
|
2026-07-07 06:47:24 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
Line(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) {
|
|
|
|
|
this->x0 = x0;
|
|
|
|
|
this->y0 = y0;
|
|
|
|
|
this->x1 = x1;
|
|
|
|
|
this->y1 = y1;
|
|
|
|
|
}
|
|
|
|
|
Line() {}
|
|
|
|
|
uint16_t x0;
|
|
|
|
|
uint16_t y0;
|
|
|
|
|
uint16_t x1;
|
|
|
|
|
uint16_t y1;
|
|
|
|
|
|
|
|
|
|
// Uses Bresenham's line algorithm to draw line of any slope
|
2026-07-20 02:04:17 -07:00
|
|
|
template <class ShaderT>
|
|
|
|
|
void draw (Buffer buffer, ShaderBase<ShaderT>& shader) {
|
2026-07-07 06:47:24 -07:00
|
|
|
if (x0 > buffer.width || y0 > buffer.height || x1 > buffer.width || y1 > buffer.height) {return;};
|
|
|
|
|
int16_t ix0 = x0;
|
|
|
|
|
int16_t iy0 = y0;
|
|
|
|
|
int16_t ix1 = x1;
|
|
|
|
|
int16_t iy1 = y1;
|
|
|
|
|
if (abs(iy1 - iy0) < abs(ix1 - ix0)) {
|
|
|
|
|
if (ix0 > ix1) {
|
2026-07-08 16:15:48 -07:00
|
|
|
lineLow(ix1, iy1, ix0, iy0, buffer, shader);
|
2026-07-07 06:47:24 -07:00
|
|
|
} else {
|
2026-07-08 16:15:48 -07:00
|
|
|
lineLow(ix0, iy0, ix1, iy1, buffer, shader);
|
2026-07-07 06:47:24 -07:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (iy0 > iy1) {
|
2026-07-08 16:15:48 -07:00
|
|
|
lineHigh(ix1, iy1, ix0, iy0, buffer, shader);
|
2026-07-07 06:47:24 -07:00
|
|
|
} else {
|
2026-07-08 16:15:48 -07:00
|
|
|
lineHigh(ix0, iy0, ix1, iy1, buffer, shader);
|
2026-07-07 06:47:24 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|