Fix circle

This commit is contained in:
2022-03-03 22:57:03 +01:00
parent cd309c04f2
commit 7fbad9a90d
2 changed files with 27 additions and 8 deletions

View File

@@ -12,6 +12,8 @@ namespace frame::render
void RenderTarget::DrawPixel(Vector const& position, Color color)
{
if(position.x < 0 || position.y < 0 || position.x >= image.getWidth() || position.y > image.getHeight())
return;
image.at(position.x, position.y) = color;
}
@@ -79,26 +81,42 @@ namespace frame::render
int32_t const r_2 = radius * radius;
Vector pos{0, -radius};
pos.x += 1;
float E =
pos.x * pos.x + ((float)pos.y - 0.5f) * ((float)pos.y - 0.5f) - r_2;
DrawPixel(center + pos, color);
DrawPixel(center + Vector{pos.x, pos.y * -1}, color);
DrawPixel(center + Vector{pos.x * -1, pos.y * -1}, color);
DrawPixel(center + Vector{pos.x * -1, pos.y}, color);
DrawPixel(center + Vector{pos.y, pos.x}, color);
DrawPixel(center + Vector{pos.y, pos.x * -1}, color);
DrawPixel(center + Vector{pos.y * -1, pos.x * -1}, color);
DrawPixel(center + Vector{pos.y * -1, pos.x}, color);
while(pos.x <= radius / std::sqrt(2.f))
{
if(E < 0)
float E = pos.x * pos.x + ((float)pos.y + 0.5f) * ((float)pos.y + 0.5f) - r_2;
if(E <= 0)
{
pos.x += 1;
E += 3;
E += 2 * pos.x + 3;
} else
{
pos.x += 1;
pos.y -= 1;
pos.y += 1;
E -= 2 * (-radius) + 5;
E += 2 * pos.x - 2 * pos.y + 5;
}
DrawPixel(center + pos, color);
DrawPixel(center + Vector{pos.x, pos.y * -1}, color);
DrawPixel(center + Vector{pos.x * -1, pos.y * -1}, color);
DrawPixel(center + Vector{pos.x * -1, pos.y}, color);
DrawPixel(center + Vector{pos.y, pos.x}, color);
DrawPixel(center + Vector{pos.y, pos.x * -1}, color);
DrawPixel(center + Vector{pos.y * -1, pos.x * -1}, color);
DrawPixel(center + Vector{pos.y * -1, pos.x}, color);
}
}