If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Cohen–Sutherland Algorithm Modification

Hi,
My question is regarding a modification I was considering making to the Cohen-Sutherland algorithm to remove the need for a loop, specifically I want to know if the performance benefits of such a modification would outweigh the downside of the extra calculations. The modification I am making is this:
In the original algorithm, lines are clipped first against the vertical and then against the horizontal edges, so in worst case a line may need to be clipped 4 times, twice on each end. I modified the algorithm to use two slopes -- the slope of the line itself, and the line between the outer endpoint and the corner of the clipping region, to determine whether to clip the line against the vertical or the horizontal boundary. The code (in Java) looks like this:
...

double x = p1.getX();
double y = p1.getY();
double cornerSlope = (y - ymax)/(x - xmax);
double lineSlope = (y - p2.getY()) / (x - p2.getX());
if(cornerSlope < lineSlope) {
// chop against top edge
double yClipped = ymax;
double xClipped = (y - ymax) / lineSlope;
return new Point2D.Double(xClipped, yClipped);
} else {
// chop against left edge
....
}
...
Above is just a snippet of a function that is part of the algorithm, it is called after trivial accept/reject cases have been handled and returns the intersect point given p1 as the outer point and p2 as either the inner or the outer point (outer/inner with respect to the clipping region). It's called twice with parameters flipped if the line sticks out on both ends.

So I was wondering whether it is better to clip twice on each end as in original algorithm or use this modification, calculate the slopes, and clip only once on each end? Also, is this something that maybe is well-known/has been done/documented/has a name/etc.? Please forgive /correct any mistakes as I'm a bit new to this and I just coded this tonight/this morning. Thanks for any replies.

Re: Cohen–Sutherland Algorithm Modification

To clarify my question here's the code, I finished it today and it seems to work, so if someone can please take a look at it and tell me if my approach to line clipping makes sense I'd really appreciate it. It's difficult to know if you're on the right track without feedback, this is part of my first big project and I've been working on it for a while and I was hoping someone here might know something about line clipping and whether it makes sense to get rid of the loop in the algorithm or not. So here's the code:

The Cohen-Sutherland algorithm has a max of 4 passes, where on each pass it mainly calculates

Code:

x = x0 + delx * (ymax - y0) / dely;

But since there is no consequence to the selection of which point goes first, it can be done in max of 2 parallel passes.
Since this algorithm is usually implemented in hardware pipeline, the loop is implemented in parallel, and you get better latency.

On the other hand, I think your code may not be parallelized very well since the decision where to clip is based on the slope check branch.