PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Clipping problem with DrawLine

Ok. I'm trying to write a VRML viewer using PF and I get the following problem with the DrawLine() function:

Everytime the line starts/ends outside the window, it get's distorted, ie: the clipping code doesn't do what it's suppose to do.

This fenomenon only occurs at the top and bottom of the window. Lines that end/start outside the window to the left or right is calculated correctly and just get cut off.

If you look closely, you can figure out that the lines that end/start outside the top of the window are drawn in the wrong direction from the point not outside the window. That is: It starts in the endpoint inside the window, has the right angle, but wrong direction.

The lines outside the bottom have yet another fault that I can't think of an easy way to explain.

I've looked at the code for DrawLine and found that is has it's own clipping calculator code based on an algoritm called Cohen-Sutherland. Has this somehow been wrongly implemented or is the error someplace else??

I've tried to resize the window back to 320x240 (running a h/pc so I have the PF window in 400x400) but the same thing happens.

Has anyone stumbled across this before, and how did you get past it??

It shure would be nice if the clipping worked as it should. Keeping track of all the line endpoints in a big VRML graph is not something you want to worry about.
When I read this at first I was surprised, but there seem to be indeed a bug in there!

Easy to fix: go to "rasterizer.cpp" around line 568 and replace:

Code:

else if (code & 4)
{
// Top edge
x = p1.x + (p2.x - p1.x) * (ymax - p1.y) / (p2.y - p1.y);
y = ymax;
}
else if (code & 8)
{
// Bottom edge
x = p1.x + (p2.x - p1.x) * (ymin - p1.y) / (p2.y - p1.y);
y = ymax;
}


with:

Code:

else if (code & 4)
{
// Top edge
x = p1.x + (p2.x - p1.x) * (ymin - p1.y) / (p2.y - p1.y);
y = ymin;
}
else if (code & 8)
{
// Bottom edge
x = p1.x + (p2.x - p1.x) * (ymax - p1.y) / (p2.y - p1.y);
y = ymax;
}


Pfew! There was 3 bugs!

Thanks a lot for reporting this.
adde · 13 Nov 2002, 19:30 UTC — Strike two
Strike two!!

One more and I'm out.. I havn't had a good day today (those of you who read my note on STL strings know what I'm talking about). Thanks for pointing out the obvious Kzinti. The Top/Bottom (non-working) clipping code should have been like the Left/Right (working) clipping code, only switch x for y.

Can't believe I didn't crack that one myself, but if you're having one of those days you just ask first and think later. I didn't even look at the code, silly me, even though I knew it was wrong.

Anyways.. Works like a charm now.

Ps. Like I wrote in another note, this forum rocks. You post a question, and a few hours later you get an answer.
Kzinti · 13 Nov 2002, 21:04 UTC — Strike two
adde wrote:
Can't believe I didn't crack that one myself,


Can't believe I made that typical cut & paste mistake and never found out about it. I guess people (including me) don't use lines that much.

adde wrote:
Anyways.. Works like a charm now.


Glad to hear it... This fix will be in the next release.

adde wrote:
Ps. Like I wrote in another note, this forum rocks. You post a question, and a few hours later you get an answer.


Glad you like it! Thanks to all people who tries to help (Conan, BurningSheep, others).