#### Archived

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

# Drawing a 2D line

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

## 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 on other sites
Oh yeah the test should only have integers...

##### Share on other sites
Look for "line clipping", "line intersection" or something like it at Google.

##### Share on other sites
When I search for that all I find are assignements for students, which obviously don''t contain the answer

##### Share on other sites
Try a "-edu" in your search query. Does wonders to avoid school stuff.

##### 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 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 on other sites
When clip be sure what kind of clip you use.

Search about "Scisor clipping".

##### 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

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631425
• Total Posts
3000014
×