Archived

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

Boops

Drawing a 2D line

Recommended Posts

Hi, I have a little problem with drawing 2D lines: I know how to do it with bresenham, but the problem is if one or two of the endpoints of the line are outside the screen, what''s the best way to find the points where the line enters the screen, IF it enters it? Thanks.

Share this post


Link to post
Share on other sites
I found something!

Thanks a lot, and I learned about the existance of new algorithms now too


I used one with "cohen sutherland" which appearantly isn't the fastest one, but I changed all the floats to ints and it still works perfectly and fast enough for me.

[edited by - Boops on September 3, 2003 11:35:49 AM]

Share this post


Link to post
Share on other sites
Hey, sorry for yet another post in here....

But I needed this 2D line drawing to draw 3D lines (software), and it works nicely except for one thing:


What do I have to do if one endpoint of the line is in front of the camera, while the other endpoint is behind the camera? Things seem to screw up :s

Share this post


Link to post
Share on other sites
You must clip the line to the near plane so that it both begins and ends in front of the camera. The 1/z you probably do to convert from 3d to screen coordinates is only valid when z>0 (and will crash if z==0).

For example, let''s assume point A is behind the camera (Az<0) and B is in front. Camera''s z coordinate is defined as zero, and near plane distance (Nz) is a positive non-zero. You need to create a new point A'' so that A''z == neardist.

A''x = Ax + (Bx-Ax) * (Nz-Az) / (Bz-Az)
A''y = Ay + (By-Ay) * (Nz-Az) / (Bz-Az)
A''z = Nz

Share this post


Link to post
Share on other sites