Determining if a line should be drawn on screen?

BlitzMax Forums/BlitzMax Programming/Determining if a line should be drawn on screen?

Sokurah(Posted 2012) [#1]
Heh, normally you'd say "if you draw it then it's there", but things aren't always that simple. ;-)

Say I have the coordinates for some lines in a Type. All lines has the format: from X1,Y1 to X2,Y2.

Some are drawn left to right, others from right to left. That goes for top to bottom and bottom to top, too. They're arranged a bit randomly.

Now, if all these are lines are small, then it's just a matter of checking if either X1,Y1 or X2,Y2 is on the screen, and then drawing them.

But what if a line is so long that both start and end points are outside the screen area? ...how do I determine that it goes across the screen so I can draw it even though both start and end points are outside the screen area?

..the whole idea is, of course, to not have to draw more lines than are needed.


Raz(Posted 2012) [#2]
Just check to see if the line intersects a rectangle

code arcs entry by warpy : http://www.blitzbasic.com/codearcs/codearcs.php?code=2448


Midimaster(Posted 2012) [#3]
As far as I know: drawing lines which are partially outside the screen will not consume time.

You cannot save a lot of time, if you find out which line has to be drawn and which not. BlitzMax's graphic engine has this algo too and does a good job!


But you also could use the command SETVIEWPORT


Sokurah(Posted 2012) [#4]
Oh, thanks Raz...I've come across it before, but I'd completely forgotten there was such a thing as a "line intersects rectangle" function around. D'oh! :-)

@Midimaster, I have considered that it may take longer to do all the calculations instead of just drawing everything every frame and hoping the graphics hardware would take care of it, but I guess I'd need to do a bit of testing to see if it's one or the other. ...and it may even work differently from card to card?

...however, SETVIEWPORT is of no use for a fullscreen game (unless there's something I'm missing?).

Last edited 2012