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)

View File

@@ -1,8 +1,8 @@
#include <cmath>
#include <chrono>
#include <iostream>
#include <thread>
#include <vector>
#include <cmath>
using namespace std::chrono_literals;
@@ -12,17 +12,8 @@ using namespace std::chrono_literals;
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{};
@@ -34,28 +25,20 @@ int main()
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.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;
double const cos_ = i * 0.01745329;
std::cout << i << " cos: " << cos_ << std::endl;
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;
double const x = std::cos(i * pi / 180.f) * 200.f;
double const y = std::sin(i * pi / 180.f) * 200.f;
end.x += (int)x;
end.y += (int)y;
std::cout << end << std::endl;
target.DrawLine(center, end);
}

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);
}
}