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