- SEPTiC DEMO'S COLLEGE -- 

                                 Lesson 13 
                                 ?????????? 
                         Written by Vicious / Septic 

                                 Mar 11 94 



                                  Introduction 
                                  ????????? 
Hello again! Now you can quickly mostly in the demo-programming, but we have 
few things to go through. In this lesson should we treat the 
last in the vector-block, namely 2d-cutting. Although there is a 
whole bunch of things you could go into, so that inkonvexa, complex 
vectors, 3d-clipping, glenz / RGB / shadow-vectors and so on. but they are really 
rather simple once you master the basic vector operations 
I wrote about in school. 
   However, we have some interesting things remain as spheres, and bitmap - 
rotation / zoom to pick up, so stick to steel! Now it's 2d-cutting as 
case ... 

                                    Theory 
                                    ????? 
Cutting the two-dimensional - as the name fortaljer - a way to 
limit vektorobjektets two-dimensional image of a window 
special coordinates. This is particularly useful to be able to let a 
Items slip into the screen from one edge, or to have an item that is 
much larger than the screen, without the screen needs to be huge, to 
prevent the vector is drawn off the screen (and destroying other important 
information in memory). 
   This is achieved by cutting each new line drawn in a 
polygon. One must also take into account how blitterns fill feature works; 
and add extra lines where it is needed to fill to be 
correctly (provided that you should use the filled vectors, of course ...) 
   2d-cutting is performed just before the line to be drawn out on the screen, thus 
after all rotation and 3d-> 2d-conversion, and clip-routine is to be 
treat all potential cases that may arise. For example, the line can be picked QUITE 
away, partly cut once or twice, or not cut at all. 
   Here are all kinds of clips situations may occur: 

      ____________________________________ 
     | | 
     | | <-Screen 
     | | 
     | / | \ 
     | / | \ 
     | A / | \ D 
     | / | \ 
     | / | / \ 
     | / | / C 
     | / 
     | \ / | 
     |___________ \ ______________________/_| 
                 B \ / 
                   \ / 
                    \ 

Line A is located entirely within the borders of the screen and should not be cut 
at all. Line B is partially outside the screen, then should we mow it, and 
the same goes for the line C, but with the difference to break the boundaries TWO 
times. Finally, the line D, to be removed, because it is not 
visible at any one spot on the screen. 
   After trimming will we get the following screen appearance: 

      ____________________________________ 
     | | 
     | | <-Screen 
     | | 
     | / | 
     | / | 
     | A / | 
     | / | 
     | / | 
     | / | 
     | | 
     | \ B C / | 
     |___________ \ ______________________/_| 

When we are rock-filled polygons, we have said that taking into account 
blitterns filling requirements, and by that I mean that the surface must be completely 
Closed to be completed properly. If then some parts of it are outside 
the screen needs to automatically connect places where the shearing took place. 
   Consider this picture to understand better: 

      ____________________________________ 
     | | 
     | | <-Screen 
     | | 
     | ____|___ 
     | / | F \ <-Polygon 
     | A / | \ E 
     | / | \ 
     | \ | / 
     | \ | / 
     | B \ | / D 
     | \ ___|__/ 
     |____________________________________| C 

If we are to cut the above polygon, we must do it as follows: 

1.Rita the line A (without cutting as it is within the screen boundaries) 
2.Rita out line B (also without cutting ...) 
3.Rita out line C, and the cutting line. Remember the clip-coordinate. 
4.Linje D is outside, and therefore we do not draw it out. 
5.Samma goes for the line E. 
6.Linje F cut that line C, but instead of remembering a new 
Clip-coordinate, we notice that there is already an old clip-coordinate 
stored. So we are drawing a line between these two coordinates and clears 
Then the stored clip-coordinate. 

If we have a different case, involving a part of the polygon goes off the screen 
both in the bottom edge and the right hand, we do much the same way ... We 
reminds us just to blittern only need extra lines of right and 
Left-wing (as it turns between the points from right to left). 

      ____________________________________ 
     | | 
     | | <-Screen 
     | | 
     | | 
     | | 
     | | 
     | __|__ 
     | / | F \ 
     | A / | \ E 
     | / | \ 
     | \ | \ 
     |________________________________ \ ___ | / 
                                      B \ / D 
                                        \ _____/ 
                                           C 
1.Linje A plotted without shearing. 
2.Linje B cut and clip-coordinate stored. Since we just want to draw out 
lines of right / left edge, we see that we do not need X-coordinate. 
Y-coordinate suffices. 
3.Linje C, D and E are not drawn out. 
4.Linje F cut and we see that we have a cutting-coordinate stored. So 
we are drawing out an extra line of F, straight down the screen edge to the 
bottom edge. 
   Finally, to handle the case in which a line intersects a flush twice 
makes it so that it is cutting to one edge first, and then the other 
edge. In this way, divide the whole and there will be no more difficult than one 
normal "a cutting-spectrum"-shearing. 

This can certainly seem a bit difficult and obegripbart to start with, but I 
think you will understand everything soon. 
   Okay, now the real question: How to cut a line? 

Yes, it is not all that difficult. It is based simply on a little uniformity 
again. We watch a shearing cases: 

               Cutting Border 
              | 
              | 
              | X1, y1 
              | / <-Line 
              | / 
              | / 
              | 
             / | 
            / | 
           / | 
          / | 
      x2, y2 | 

We know the line start and end coordinates, ie x1, y1 and x2, y2, and we 
feel even to clip the border as we sat on himself. 
   We have received two triangles are uniform and only one length is 
Unknown: 

               Cutting Border 
              | 
              | 
              | _ 
              | / | | A - Large triangle height 
              | / | | B - Store triangle width 
              | / | | C - Little triangle height 
              | | | - A = y2-y1 d - Little triangle width 
             / | | | 
            / | C | | 
           / | | | 
          /___|___| _ | 
              | 

          |___| 
            | 
            d = Cut Border-x2 
          |_______| 
              | 
              b = x2-x1 

We know a, b and d, but not c. And what indicates c? Well, it indicates thus 
how far from y2 as the new y1 should be located. We have thus created new 
values for x1, y1, where x1 = Cut the limit and y1 = y2-c. 
   The value of c determined by the uniformity: 

    a c 
    - = -- 
    b d 

        a * d 
    c = --- 
         b 

This formula can of course also be used in cutting against the other 
edges too! The coordinates need only replace the current 
instead. How it should be done, you can see in the sample application ... 

Well, that was all I was thinking about cutting explain. Remember that it is not 
JUST need to be used to cut the screen's outskirts. One example can do 
nice things by cutting in a small window on the screen while 
other things occurring there. It thus becomes easier to design something 
cool if you can cut the vectors ... 


                                  Register 
                                  ???????? 
No new records in this lesson. Mowing may be carried out completely and 
direction of the processor. 


                               Program example 
                               ??????????????? 
Today's program includes examples, not unexpectedly, a vektorkub which are cut 
the screen edges! 
   As you can see cutting me a bit on the screen for you to see how 
mowing goes to. Normally, it is customary to cut the screen in the outskirts 
and when it is not visible. 
   Well, the only new clip is really routine (strangely enough :-) and the 
Clip2d units. We invokes the right before us to draw out our routine, 
because it modifies the start line and / or end point so that the line 
cut to the limits we set. It can also provide a return on -6000 
and that means that the line is completely outside the screen and should not be 
plotted at all. 
   Okay, I can go through what the routine is a little more step by step maybe ... 
First and foremost, I store the line color value that we may need it when 
we are drawing out an extra line. 
   Then I make a little special thing that might not be so obvious. 
By assure me that X1 is less than X2, it is sufficient then to test 
on the X1 will be cut to the left edge or on the X2 to be cut toward the right - 
edge! There is no need to test both values against both wings with 
this method. Either is doing just X1 to the left of the clip border 
or so is both outside and then the line is not plotted at all. 
   Now, if only X1 is outside time, so will we have a bit further down in the code, 
to labeln. CutLeft. 
   Here am I doing various calculations under the formula above, to finally get 
until the new Y coordinate in the shortened line. I also store it here 
value if it is not already an old value. Is there an old 
value stored so it means that we cut once and then walked away 
off the screen with our polygon. Now when we come back in need 
polygon tied together with an extra line, and therefore I hope to 
XtraLineLeft routine. 
   The other cutting-makers is much the same way, and noting also 
I do the same track with Y1 and Y2 as I did at the beginning of X1 and X2, 
everything to avoid having to look twice. 
   If we telescopic on routine XtraLineLeft now, so you find that there 
is some real funny here. I just check so that the extra 
Line ports inside the borders and then I draw it out in the right bitplan. 
   Down among the variables we find ClipLeft, ClipRight, and ClipUp 
ClipDown. These are, which reveals the names, clip-coordinates that determines 
window size. Try happy to change them, but watch you for putting them 
larger than the screen size, because it certainly leads to the PC 
crashes. 
   There, more difficult, it was not!