I am trying to recreate a image like this
Using Bresenham line algorithm, but can't seem to able to do so.. The point i receive from my computation doesn't look like a like, and output from the different angles don't make sense for instance.
I start from the center (3,3), and want to draw with a certain angle with a given radius..
angle: 1
13,19
bressenham:
mark: 3,3
mark: 3,3
mark: 4,4
mark: 4,4
mark: 5,5
mark: 5,5
mark: 6,6
mark: 6,6
mark: 7,7
mark: 7,7
mark: 8,8
mark: 8,8
mark: 9,9
mark: 9,9
mark: 10,10
mark: 10,10
mark: 11,11
mark: 11,11
mark: 12,12
mark: 12,12
1319
angle: 2
-5,21
bressenham:
mark: -5,3
mark: -5,3
mark: -5,4
mark: -4,5
mark: -4,5
mark: -4,6
mark: -3,7
mark: -3,7
mark: -3,8
mark: -2,9
mark: -2,9
mark: -2,10
mark: -1,11
mark: -1,11
mark: -1,12
mark: 0,13
mark: 0,13
mark: 0,14
mark: 1,15
mark: 1,15
mark: 1,16
mark: 2,17
mark: 2,17
mark: 2,18
-521
For angle 1 => the angle is set to one.. (13,19) is the position of the endpoint located at the given length. and all the mark: are those pixels which are being marked by the algorithm.. That one makes sense, but, when i do the same for angle = 2.. something messes up.. I don't know why, but it would be very helpful if any of you could help me solve this problem.
Here is the code i used to compute these output.
#include <iostream>
#include <math.h>
using namespace std;
int sign(double x ){ return (x > 0) ? 1 : ((x < 0) ? -1 : 0); }
std::pair<int,int> endpoint(double angle, int x1 , int y1, int lenght)
{
if(angle == 90)
{
double y2 = y1+lenght;
return std::make_pair(x1, -y2);
}
if(angle == 180)
{
double x2 = x1+lenght;
return std::make_pair(-x2, y1);
}
if(angle == 270)
{
double y2 = y1+lenght;
return std::make_pair(x1, y2);
}
double x2 = x1 + (lenght * cos(angle));
double y2 = y1 + (lenght * sin(angle));
return std::make_pair(x2,y2);
}
void bresenham(int x0, int y0, int x1, int y1)
{
int dx = x1-x0;
int dy = y1 - y0;
if (abs(dx) > abs(dy))
{
// x-axis
int j = y0;
double e = dy - dx;
for (int i = x0; i<x1; i++)
{
std::cout << "marked: " << i << ","<< j << std::endl;
if( e >= 0)
{
j += 1;
e -= dx;
}
e += dy;
}
}
else if(abs(dx)<abs(dy))
{
// y-axis
int j = x0;
double e = dx - dy;
for (int i = y0; i<y1; i++)
{
std::cout << "marked: " << j<< ","<< i << std::endl;
if( e >= 0)
{
j += 1;
e -= dy;
}
e += dx;
}
}
}
void line(int x0, int y0, int x1, int y1)
{
if(x1 < x0)
{
swap(x1, x0);
}
if(y1 < y0)
{
swap(y1, y0);
}
int dx = x1-x0;
int dy = y1-y0;
double e = 0;
if(dx == 0)
{
if (sign(dy) == -1)
{
cout << "new" << endl;
for (int y = y0; y != y1; y--)
{
cout << "mark" << x1 << "," << y << endl;
}
}
else
{
for (int y = y0; y != y1; y++)
{
cout << "mark" << x1 << "," << y << endl;
}
}
return;
}
else if(dy == 0)
{
if (sign(dx) == -1)
{
cout << "new" << endl;
for (int x = x0; x != x1; x--)
{
cout << "mark" << x << "," << y1 << endl;
}
}
else
{
for (int y = y0; y != y1; y++)
{
cout << "mark" << x1 << "," << y << endl;
}
}
return;
}
cout << "bressenham: " << endl;
double de = abs(dy/dx);
int y = y0;
for (int x = x0; x != x1; x++)
{
cout << "mark: " << x << "," << y << endl;
e = e + de;
while (e >= 0.5)
{
cout << "mark: " << x << "," << y << endl;
y = y + sign(y1-y0);
e = e -1;
}
}
}
int main(int argc, const char * argv[])
{
for (int angle = 0; angle < 180; angle++)
{
cout << "angle: " << angle << endl;
pair<int, int> end = endpoint(angle, 3, 3, 20);
cout << end.first << end.second << endl;
line(3, 3, end.first, end.second);
cout << end.first << end.second << endl;
}
return 0;
}