Drawing a 2D line

Started by
7 comments, last by Boops 20 years, 7 months ago
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.
Advertisement
Oh yeah the test should only have integers...
Look for "line clipping", "line intersection" or something like it at Google.
When I search for that all I find are assignements for students, which obviously don''t contain the answer
Try a "-edu" in your search query. Does wonders to avoid school stuff.
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]
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
When clip be sure what kind of clip you use.

Search about "Scisor clipping".
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

This topic is closed to new replies.

Advertisement