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,4 +1,4 @@
cmake_minimum_required(VERSION 3.20) cmake_minimum_required(VERSION 3.18)
project(Frame) project(Frame)

View File

@@ -1,8 +1,8 @@
#include <cmath>
#include <chrono> #include <chrono>
#include <iostream> #include <iostream>
#include <thread> #include <thread>
#include <vector> #include <vector>
#include <cmath>
using namespace std::chrono_literals; using namespace std::chrono_literals;
@@ -12,17 +12,8 @@ using namespace std::chrono_literals;
int main() int main()
{ {
double const pi = 3.14159; constexpr double pi = 3.14159;
std::cout << "pi: " << pi << " " << 3.14159f << std::endl;
std::cout << "pi / 180;" << pi / 180.f << std::endl;
for(int i = 0; i < 360; i += 45)
{
std::cout << "(i): " << i << std::endl;
std::cout << "rad: " << (float)i * pi / 180.f << std::endl;
}
return 0;
frame::display::EPD_7in5_V2 display{}; frame::display::EPD_7in5_V2 display{};
@@ -34,28 +25,20 @@ int main()
frame::Vector const center = (display.getSize() / 2); frame::Vector const center = (display.getSize() / 2);
frame::Vector v = {10, 12};
std::cout << "Main: " << v << std::endl;
target.DrawPixel({10, 10}, frame::BLACK); target.DrawPixel({10, 10}, frame::BLACK);
target.DrawLine(v, {100, 12}, frame::BLACK); target.DrawLine({10, 12}, {100, 12}, frame::BLACK);
for(int i = 0; i < 360; i += 45) for(int i = 0; i < 360; i += 15)
{ {
auto end = center; auto end = center;
double const cos_ = i * 0.01745329; double const x = std::cos(i * pi / 180.f) * 200.f;
std::cout << i << " cos: " << cos_ << std::endl; double const y = std::sin(i * pi / 180.f) * 200.f;
double x = std::cos(cos_);
double y = std::sin(i * 0.01745329);
x *= 50.f;
y *= 50.f;
std::cout << x << " " << (int)x << " " << y << std::endl;
end.x += (int)x; end.x += (int)x;
end.y += (int)y; end.y += (int)y;
std::cout << end << std::endl;
target.DrawLine(center, end); target.DrawLine(center, end);
} }

View File

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