3

There are 4 rules explained in Sutherland-Hodgman algorithm for clipping polygons:

  • If both vertices are inside the clipping area - save the second
  • If the first vertex is inside and the second is outside - compute the intersection with the border of the clipping area and save it
  • If the first vertex is outside and the second is inside - compute the intersection with the border of the clipping area and save it, and also save the second vertex
  • If both vertices are outside - save nothing

Due to this explanation, what will it do when a line formed by 2 vertices crosses the clipping area, like in the below image?

If I follow the algorithm steps I'll end up with no vertices at all... Is there no consideration for such a case? Maybe I should pre-calculate all the intersection areas and use also them?

ClippingProblem

2
  • Could you give a reference of the 4 rules? What are the two vertices? I am reading about this algorithm on wikipedia. It doesn't mentioned the two vertices at all. Commented May 9, 2015 at 20:33
  • In Wikipedia it is actually explained differently. If you look in other descriptions, or even in video guides in Youtube, you'll see these 4 rules. Commented May 9, 2015 at 20:40

1 Answer 1

3

I finally found the answer myself - I will share it here with everyone who likes to know...

You may see a description of the correct algorithm here: https://www.youtube.com/watch?v=Euuw72Ymu0M

It is wrongly described in many tutorials, that one should consider each vertex as inside/outside the clipping area, and then to apply the rules as described in the question above.

The correct algorithm - is to iterate over all the edges of the clipping area. Instead of considering a vertex inside/outside the clipping area - the correct thing to do is to consider the vertex as in the inner/outer side of the specific edge we iterate on. It means that for instance, for the example image in the question - the left edge of the clipping area will consider the up-right corner of the blue rectangle as inside - because although it is outside the clipping area, it is in the inner side of the left edge. The up-left corner for instance, will be considered as outside by this edge.

If the algorithm is done in 2D, we have 4 edges of the clipping area. For example, we first apply the algorithm on the left edge - then we use its output for running the algorithm on the right edge, then the same for the upper edge and the bottom edge. The final answer will be the the output of the last one.

I.e. If we used Left->Right->Up->Bottom, the final output will be the vertex list outputted by the bottom edge.

You can try running this algorithm, following for each edge the rules described in the question - you'll see that you're final polygon will be exactly the clipped one

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.