Page 1 of 1 [ 6 posts ]

#### Draw Line

 Print view Previous topic | Next topic

#### Draw Line

Author Message
 Post subject: Draw Line  Posted: September 12th, 2017, 5:00 pm

Joined: September 7th, 2014, 3:45 am
Posts: 15
In Chili's tutorial series years ago he taught us how to make a draw line function early on. I quit programming for quite and awhile and so I am going back through he new series again to brush up on my skills. I noticed that in this new series he does not show a draw line function. I've only made it to lesson 20 of the beginner series this may be something that he does indeed show later but I have yet to see it. I have found an excellent draw line function that works flawlessly(so far) and I just wanted to share it with the group.

Code:
void Graphics::DrawLine(float x1, float y1, float x2, float y2, Color & color)
{
const bool steep = (fabs(y2 - y1) > fabs(x2 - x1));
if (steep)
{
std::swap(x1, y1);
std::swap(x2, y2);
}

if (x1 > x2)
{
std::swap(x1, x2);
std::swap(y1, y2);
}

const float dx = x2 - x1;
const float dy = fabs(y2 - y1);

float error = dx / 2.0f;
const int ystep = (y1 < y2) ? 1 : -1;
int y = (int)y1;

const int maxX = (int)x2;

for (int x = (int)x1; x<maxX; x++)
{
if (steep)
{
PutPixel(y, x, color);
}
else
{
PutPixel(x, y, color);
}

error -= dy;
if (error < 0)
{
y += ystep;
error += dx;
}
}
}

 Post subject: Re: Draw Line  Posted: September 13th, 2017, 1:15 am

Joined: December 31st, 2011, 4:53 pm
Posts: 3514
Location: Japan
This looks like Bresenham. I won't do Bresenham when I teach straight line drawing (I like to teach something that is as close to y=mx+b as possible because it is the easiest for most to understand), but it is the way to go if you want the most efficient algorithm.

Still trying to figure out when I want to go over straight line. Will definitely do it though, not only for its own sake but also because linear math is essential to master for game dev.

_________________
Chili

 Post subject: Re: Draw Line  Posted: September 13th, 2017, 2:12 am

Joined: September 7th, 2014, 3:45 am
Posts: 15
It is Bresenham. I got it off rosettacode.com. I was trying to figure it out on my own but i could not for the life remember how to fix the problem when the line would not draw.

 Post subject: Re: Draw Line  Posted: September 13th, 2017, 2:19 am

Joined: December 31st, 2011, 4:53 pm
Posts: 3514
Location: Japan
There is another related algorithm for drawing thin line circles that doesn't need any sqrt or even floating point.

_________________
Chili

 Post subject: Re: Draw Line  Posted: September 13th, 2017, 2:28 am

Joined: September 7th, 2014, 3:45 am
Posts: 15
I got a quick question for you. I am trying to load my old projects in VS15 that I wrote in VS10. I am getting this error "LNK1104 cannot open file ' dxerr.lib' " any idea how to fix this?

 Post subject: Re: Draw Line  Posted: September 13th, 2017, 2:44 am

Joined: December 31st, 2011, 4:53 pm
Posts: 3514
Location: Japan
Go into the solution settings, linker settings, remove dxerr.lib from the list of libraries imported. It isn't actually needed.

_________________
Chili

Display posts from previous:  Sort by

 Page 1 of 1 [ 6 posts ]

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

 Search for: