add: Circle
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
|
||||
#include "Size.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace frame
|
||||
{
|
||||
struct Vector
|
||||
@@ -59,4 +59,9 @@ namespace frame
|
||||
return os << "x: " << v.x << " y: " << v.y;
|
||||
}
|
||||
|
||||
inline Vector operator+(Vector const& a, Vector const& b)
|
||||
{
|
||||
return {a.x + b.x, a.y + b.y};
|
||||
}
|
||||
|
||||
} // namespace frame
|
||||
|
||||
@@ -1,20 +1,19 @@
|
||||
#include <cmath>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
#include "display/EPD_7in5_V2.hpp"
|
||||
#include "render/RenderTarget.hpp"
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
constexpr double pi = 3.14159;
|
||||
|
||||
|
||||
frame::display::EPD_7in5_V2 display{};
|
||||
|
||||
display.Init();
|
||||
@@ -25,7 +24,6 @@ int main()
|
||||
|
||||
frame::Vector const center = (display.getSize() / 2);
|
||||
|
||||
|
||||
target.DrawPixel({10, 10}, frame::BLACK);
|
||||
target.DrawLine({10, 12}, {100, 12}, frame::BLACK);
|
||||
|
||||
@@ -42,6 +40,8 @@ int main()
|
||||
target.DrawLine(center, end);
|
||||
}
|
||||
|
||||
target.DrawCircle(center, 210);
|
||||
|
||||
display.Display(target.getImage());
|
||||
|
||||
std::this_thread::sleep_for(10s);
|
||||
|
||||
@@ -55,8 +55,7 @@ namespace frame::render
|
||||
{
|
||||
pos.x += rX;
|
||||
pos.y += rY;
|
||||
}
|
||||
else
|
||||
} else
|
||||
{
|
||||
pos.y += rX;
|
||||
pos.x += rY;
|
||||
@@ -65,11 +64,42 @@ namespace frame::render
|
||||
E += 2 * deltaY - 2 * deltaX;
|
||||
}
|
||||
|
||||
if(pos.x < 0 || pos.y < 0 || pos.x >= image.getWidth() || pos.y >= image.getHeight())
|
||||
if(pos.x < 0 || pos.y < 0 || pos.x >= image.getWidth()
|
||||
|| pos.y >= image.getHeight())
|
||||
break;
|
||||
|
||||
DrawPixel(pos, color);
|
||||
}
|
||||
}
|
||||
|
||||
void RenderTarget::DrawCircle(Vector const& center,
|
||||
int32_t radius,
|
||||
Color color)
|
||||
{
|
||||
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;
|
||||
|
||||
while(pos.x <= radius / std::sqrt(2.f))
|
||||
{
|
||||
if(E < 0)
|
||||
{
|
||||
pos.x += 1;
|
||||
|
||||
E += 3;
|
||||
} else
|
||||
{
|
||||
pos.x += 1;
|
||||
pos.y -= 1;
|
||||
|
||||
E -= 2 * (-radius) + 5;
|
||||
}
|
||||
|
||||
DrawPixel(center + pos, color);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace frame::render
|
||||
|
||||
@@ -20,6 +20,10 @@ namespace frame::render
|
||||
Vector const& end,
|
||||
Color color = BLACK);
|
||||
|
||||
void DrawCircle(Vector const& center,
|
||||
int32_t radius,
|
||||
Color color = BLACK);
|
||||
|
||||
Image const& getImage()
|
||||
{
|
||||
return image;
|
||||
|
||||
Reference in New Issue
Block a user