• Advertisement


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

line drawing...

This topic is 5409 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

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
doh, why didnt I think of that... ehe... anyways thanks alot.

In the next 80 years 6 billion people will die...

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


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


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


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

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


// Check for negative directions

{x_adder = -x_adder;
x_direction = -1;
{x_direction = 1;

{y_adder = -y_adder;
y_direction = -PITCH_CONSTANT;
{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

// plot pixel

*(desired pointer type cast)offset = pixel_color;

// Update X movement for next pixel

x_accum += x_adder;

// Check X movement


// Update Y movement for the next pixel

y_accum += y_adder;

// Check Y movement


// end of drawline


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

  • Advertisement