fix midpoint

This commit is contained in:
2022-03-03 21:47:35 +01:00
parent 3e342396fe
commit 8a9d0286ef
3 changed files with 39 additions and 35 deletions

View File

@@ -1,7 +1,6 @@
#include "RenderTarget.hpp"
#include <cmath>
#include <iostream>
namespace frame::render
{
@@ -20,33 +19,55 @@ namespace frame::render
Vector const& end,
Color color)
{
auto const deltaX = std::abs(start.x - end.x);
auto const deltaY = std::abs(start.y - end.y);
auto deltaX = std::abs(start.x - end.x);
auto deltaY = std::abs(start.y - end.y);
auto const rX = start.x < end.x ? 1 : -1;
auto const rY = start.y < end.y ? 1 : -1;
auto rX = start.x < end.x ? 1 : -1;
auto rY = start.y < end.y ? 1 : -1;
bool swap = false;
if(deltaX < deltaY)
{
swap = true;
std::swap(deltaX, deltaY);
std::swap(rX, rY);
}
auto pos = start;
auto E = 2 * deltaY - deltaX;
std::cout << "Start: " << start << " End: " << end << std::endl;
DrawPixel(pos, color);
while(pos != end)
{
if(E <= 0)
{
pos.x += rX;
if(!swap)
pos.x += rX;
else
pos.y += rX;
E += 2 * deltaY;
} else
{
pos.x += rX;
pos.y += rY;
if(!swap)
{
pos.x += rX;
pos.y += rY;
}
else
{
pos.y += rX;
pos.x += rY;
}
E += 2 * deltaY - 2 * deltaX;
}
std::cout << pos << std::endl;
if(pos.x < 0 || pos.y < 0 || pos.x >= image.getWidth() || pos.y >= image.getHeight())
break;
DrawPixel(pos, color);
}
}