• Advertisement
Sign in to follow this  

draw line using two points

This topic is 4420 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I want to learn how to draw a line between two point.(or connect pixels which are between two points ). I am drawing yuv video data that comes from video capture card. When user click two points on image I want to draw a line between two points. Thanks,

Share this post


Link to post
Share on other sites
Advertisement
If you want an alternative to Bresenham's algorithm you can calculate the gradient using floating point numbers and then step past each pixel either horizontally or vertically, depending on whether the line is steep (gradient > 1) or not.

I wrote a function that draws a line between two 2D vertices/points on a DirectDraw surface a while ago, maybe it can be of some help, here it is:


HRESULT Graphics::DrawLine(VERTEX vertex1, VERTEX vertex2, UCHAR red, UCHAR green, UCHAR blue)
{
float gradient;
int x1, x2, x, xRight, y;
UINT offset;

if (!cpPixels)
return -1;

//exchange the vertices so vertex1 is the top vertex
if (vertex1.y > vertex2.y)
{
VERTEX tmp = vertex1;
vertex1 = vertex2;
vertex2 = tmp;
}

y = vertex1.y;

//calculate the gradient of the line if it's valid
if ((int) vertex1.y != (int) vertex2.y)
{
gradient = (vertex2.x - vertex1.x) / (vertex2.y - vertex1.y);

//calculate the horizontal coordinate
x2 = vertex1.x + (y - vertex1.y) * gradient;

//draw downwards
while (y <= vertex2.y && y < pixelsRect.bottom)
{
x1 = x2; //the horizontal coordinate of the last pixel
x2 = vertex1.x + (y - vertex1.y) * gradient; //the horizontal coordinate of the new pixel

//get the rightmost horizontal coordinate
if (x2 > x1)
{
xRight = x2;
x = x1;
}
else
{
xRight = x1;
x = x2;
}

//draw a line between the last and the new pixel
offset = (y * pixelsRect.right + x) * iBytesPerPixel + y * iPixelsPad;

if (gradient < 1 && gradient > -1)
{
//the line is near vertical, only one pixel needs to be filled on each scanline
Set24Bit(cpPixels + offset, red, green, blue);

offset += iBytesPerPixel; //the next pixel address
++x; //the next horizontal coordinate
}
else
{
//the line is near horizontal, several pixels must be filled on each scanline
//draw the pixels from left to right
while (x < xRight)
{
Set24Bit(cpPixels + offset, red, green, blue);

offset += iBytesPerPixel; //the next pixel address
++x;
}
}

++y;
}
}
else
{
//the line is horizontal, get the rightmost coordinate
if (vertex2.x > vertex1.x)
{
xRight = vertex2.x;
x = vertex1.x;
}
else
{
xRight = vertex1.x;
x = vertex2.x;
}

//draw the line from the left to the right
offset = (y * pixelsRect.right + x) * iBytesPerPixel + y * iPixelsPad;

while (x < xRight)
{
Set24Bit(cpPixels + offset, red, green, blue);

offset += iBytesPerPixel; //the next pixel address
++x;
}
}

return DD_OK;
}







If you want some part of it explained more feel free to ask.

Something that should be mentioned is that cpPixels is a member char pointer to the first pixel in the DirectDraw surface to draw the line on and it has been retrieved by locking the DirectDraw surface. When done drawing lines it must be unlocked. If you haven't yet, you can read about locking/unlocking here:

Lock
Unlock

Another note: the variable offset is the amount of offset bytes from cpPixels to the pixel to draw, it's calculated using pixelsRect.right (the width of the surface in pixels), iBytesPerPixel (the amount of bytes each pixel take in memory), iPixelsPad (the amount of pad bytes on each scanline).

A third (and highly probably the easiest) alternative to draw a line on a DirectDraw surface is to retrieve the GDI device context of the surface by calling GetDC and then using Win32 GDI functions like MoveToEx and LineTo to draw the line.

\Dim_Yimma_H

[Edited by - Dim_Yimma_H on January 19, 2006 4:00:41 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement