added trapezoid drawing
This commit is contained in:
@@ -117,15 +117,20 @@ namespace UwU {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void bresenhamHigh(int16_t y0, uint16_t x1, int16_t y1, int16_t* LUT) {
|
void bresenhamHigh(uint16_t x0, int16_t y0, uint16_t x1, int16_t y1, int16_t* LUT) {
|
||||||
int16_t dx = (int16_t)x1;
|
int16_t dx = (int16_t)x1 - (int16_t)x0;
|
||||||
int16_t dy = y1 - y0;
|
int16_t dy = y1 - y0;
|
||||||
|
int16_t xi = 1;
|
||||||
|
if (dx < 0) {
|
||||||
|
xi = -1;
|
||||||
|
dx = -dx;
|
||||||
|
}
|
||||||
int16_t d = (2 * dx) - dy;
|
int16_t d = (2 * dx) - dy;
|
||||||
int16_t x = 0;
|
int16_t x = (int16_t)x0;
|
||||||
for (int16_t y = y0; y <= y1; y++) {
|
for (int16_t y = y0; y <= y1; y++) {
|
||||||
LUT[x] = y;
|
LUT[x] = y;
|
||||||
if (d > 0) {
|
if (d > 0) {
|
||||||
x++;
|
x += xi;
|
||||||
d += (2 * (dx - dy));
|
d += (2 * (dx - dy));
|
||||||
} else {
|
} else {
|
||||||
d += (2 * dx);
|
d += (2 * dx);
|
||||||
@@ -147,9 +152,9 @@ namespace UwU {
|
|||||||
bresenhamLow(y0, x1, y1, LUT);
|
bresenhamLow(y0, x1, y1, LUT);
|
||||||
} else {
|
} else {
|
||||||
if (y0 > y1) {
|
if (y0 > y1) {
|
||||||
bresenhamHigh(y1, x1, y0, LUT);
|
bresenhamHigh(x1, y1, 0, y0, LUT);
|
||||||
} else {
|
} else {
|
||||||
bresenhamHigh(y0, x1, y1, LUT);
|
bresenhamHigh(0, y0, x1, y1, LUT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
@@ -323,7 +328,19 @@ namespace UwU {
|
|||||||
// Draw a horizontally bounded trapezoid
|
// Draw a horizontally bounded trapezoid
|
||||||
// coords.y1 >= coords.y0
|
// coords.y1 >= coords.y0
|
||||||
void trapezoid (Trapezoid coords, Color color) {
|
void trapezoid (Trapezoid coords, Color color) {
|
||||||
|
uint16_t height = 1 + coords.y1 - coords.y0;
|
||||||
|
int16_t LeftBound[height];
|
||||||
|
int16_t RightBound[height];
|
||||||
|
|
||||||
|
this->bresenham(coords.xl0, coords.y1 - coords.y0, coords.xl1, LeftBound);
|
||||||
|
this->bresenham(coords.xr0, coords.y1 - coords.y0, coords.xr1, RightBound);
|
||||||
|
|
||||||
|
for (uint16_t i = 0; i <= height; i++) {
|
||||||
|
for (uint16_t x = LeftBound[i]; x <= RightBound[i]; x++) {
|
||||||
|
this->putPixel((uint32_t)x, (uint32_t)i + coords.y0, color.r, color.g, color.b, color.a);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -346,7 +363,7 @@ int main(void) {
|
|||||||
draw.rectangle(UwU::Coord2(69, 69), UwU::Coord2(420, 420), fuchsia);
|
draw.rectangle(UwU::Coord2(69, 69), UwU::Coord2(420, 420), fuchsia);
|
||||||
draw.rectangleGradient(UwU::Coord2(200, 200), UwU::Coord2(600, 400), red, blue, alpha, red);
|
draw.rectangleGradient(UwU::Coord2(200, 200), UwU::Coord2(600, 400), red, blue, alpha, red);
|
||||||
|
|
||||||
|
draw.trapezoid(UwU::Trapezoid(300, 400, 360, 360, 600, 401), green);
|
||||||
|
|
||||||
|
|
||||||
draw.point(UwU::Coord2(387, 43), green);
|
draw.point(UwU::Coord2(387, 43), green);
|
||||||
|
|||||||
Reference in New Issue
Block a user