HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Bresenhams line algorithm optimization

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
algorithmbresenhamsoptimizationline

Problem

Based on these guidelines I optimised Bresenhams line algorithm, it now fits for all kinds of line cases like images a, b, c, d, e, f, g, h show in those guidelines.

This is my test.txt input file for all line cases:

x1 y1 x2 y2

10 10 50 10
10 20 50 20
60 10 60 50
70 60 70 10
80 30 100 50
100 60 80 40
110 60 130 40
130 50 110 70
140 30 200 10
140 10 200 30
210 10 230 60
240 60 260 10


My code:

```
#include
#include
#include
#include
#include
#include
#include
#include

using namespace std;

int main()
{

//Get a console handle
HWND myconsole = GetConsoleWindow();
//Get a handle to device context
HDC mydc = GetDC(myconsole);
//color
COLORREF COLOR = RGB(255, 255, 255);

int x1, y1, x2, y2;
ifstream fin("test.txt");
string line;

if (fin.is_open())
{
while (getline(fin, line))
{
stringstream S;
S thisLine(4, 0); //to save the numbers
for (int c(0); c> thisLine[c];
}

for (int c(0); c x2) {int x = x2; x2 = x1; x1 = x;}
for(x1; x1 y2) { int y = y2; y2 = y1; y1 = y; }
for (y1; y1 x2) //switch numbers so line begin from left to right side
{
int x = x2;
x2 = x1;
x1 = x;
int y = y2;
y2 = y1;
y1 = y;
}

if (m == -1)
{
for (x1; x1 -1 && m0){
SetPixel(mydc, x1, y1, COLOR);
y1 = y1 - 1;
D = D + D1;
}
else{ SetPixel(mydc, x1, y1, COLOR); D = D + D2; }
}
}

if (m>0 && m0){
SetPixel(mydc, x1, y1, COLOR);
y1 = y1 + 1;
D = D + D1;

Solution

Is there any reason you are storing temporary those values in vector on start?

for (int c(0); c> thisLine[c];
        }

        for (int c(0); c<4; c++) //set value for each variable
        {   
            if (c == 0) { x1 = thisLine[c]; };
            if (c == 1) { y1 = thisLine[c]; };
            if (c == 2) { x2 = thisLine[c]; };
            if (c == 3) { y2 = thisLine[c]; };
        }


Can be simply replaced with

S >> x1 >> y1 >> x2 >> y2;


You are also have in code

if (y1 == y2)       //horizontal line
        ...;
    if (x1 == x2)       //vertical line


I would replace this with

if (y1 == y2)
        ...;
    else if (x1 == x2)


The only case where whose 2 samples behaves differently is y1 == y1 && x1 == x2, which is simply point and will be handled by first if. And with if ...; if you're printing this point twice. Later on you also have if (m==-1) ...; if (m==1) ...; - same thing.

if (x1 > x2) {int x = x2; x2 = x1; x1 = x;}


Simply use std::swap - more readable.

Code Snippets

for (int c(0); c<4; c++) 
        {
            //use the string stream as a new input to put the data into a vector of int
            S >> thisLine[c];
        }

        for (int c(0); c<4; c++) //set value for each variable
        {   
            if (c == 0) { x1 = thisLine[c]; };
            if (c == 1) { y1 = thisLine[c]; };
            if (c == 2) { x2 = thisLine[c]; };
            if (c == 3) { y2 = thisLine[c]; };
        }
S >> x1 >> y1 >> x2 >> y2;
if (y1 == y2)       //horizontal line
        ...;
    if (x1 == x2)       //vertical line
if (y1 == y2)
        ...;
    else if (x1 == x2)
if (x1 > x2) {int x = x2; x2 = x1; x1 = x;}

Context

StackExchange Code Review Q#77460, answer score: 4

Revisions (0)

No revisions yet.