Archived

This topic is now archived and is closed to further replies.

pag

line drawing...

Recommended Posts

Could anyone direct me to a tutorial or algorithm that draws a line on the screen with the following parameters, origin(the lines position), length and angle... the function prototype should look like this:> void drawLine(int origin, int length, float angle); oh. and it should be really quick any help is greatly appreciated! / Patrick

Share this post


Link to post
Share on other sites
You can use the Bresenham algorithm, it is very fast, the only thing that you have to do is rotate your point in the desired angle using your first point as origin to rotate.

If God with me, Who against me?

Share this post


Link to post
Share on other sites
Here''s a start:


  
// Need to determine Lines end point

// using an angle, magnitude, and origin

radians = (angle * 180) / PI_CONSTANT; // convert to radians if angle is in degrees

end.x=(cos(radians)*length) + origin.x;
end.y=(sin(radians)*length) + origin.y;

// use a normal line plotter to plot the line between the points

line(origin,end);


I hope this isn''t homework??

Share this post


Link to post
Share on other sites
Okay, Here''s something I slapped together. Hope it works correctly. (beware of my mad skillz)

BTW there is no clipping performed in this function!!!
You should try to do clipping at a higher level for speed anyway.

It shouldn''t be too hard to add some clipping if you desire.


  
// input:

// origin - is a point structure

// lenght - is the number of pixels plotted

// angle - must be in radians


void drawline(point origin, int lenght, float angle)
{
int x_adder,y_adder;
int x_direction,y_direction;
char far *offset;

// Check for an invalid line length

if(lenght<1){return;}

// these two calculations could be made into an evil LUT (could speed it up nicely)

// MAX_VALUE_ACCUM_DATATYPE_WILL_ALLOW should be

// 32767 for 16 bit signed int or 2147483647 for 32 bit signed int

// this will provide the best accurracy I can think of

x_adder = cos(angle) * MAX_VALUE_ACCUM_DATATYPE_WILL_ALLOW;
y_adder = sin(angle) * MAX_VALUE_ACCUM_DATATYPE_WILL_ALLOW;

// Check for negative directions

if(x_adder<0)
{x_adder = -x_adder;
x_direction = -1;
}
else
{x_direction = 1;
}

if(y_adder<0)
{y_adder = -y_adder;
y_direction = -PITCH_CONSTANT;
}
else
{y_direction = PITCH_CONSTANT;
}

// initialize the accumulators

x_accum = 0;
y_accum = 0;

// calculate origin''s memory offset

offset = (char far *)&videobuffer[(origin.y*(pitch/bytes_per_pixel)+origin.x]

// Plotting the line

while(lenght--)
{
// plot pixel

*(desired pointer type cast)offset = pixel_color;

// Update X movement for next pixel

x_accum += x_adder;

// Check X movement

while(x_accum>=MAX_VALUE_ACCUM_DATATYPE_WILL_ALLOW)
{offset+=x_direction;
x_accum -= MAX_VALUE_ACCUM_DATATYPE_WILL_ALLOW;
}

// Update Y movement for the next pixel

y_accum += y_adder;

// Check Y movement

while(y_accum>=MAX_VALUE_ACCUM_DATATYPE_WILL_ALLOW)
{offset+=y_direction;
y_accum -= MAX_VALUE_ACCUM_DATATYPE_WILL_ALLOW;
}
}

// end of drawline

return;
}


Good Luck!

If there is anything wrong with this code feel free to insult me.
However, please point out why I messed up. And if anyone would like to show some better ways of doing this please show everyone.

Share this post


Link to post
Share on other sites