home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / TUT1-9.ZIP / TUT3.TXT < prev    next >
Text File  |  1993-07-27  |  13KB  |  350 lines

  1.                    ╒═══════════════════════════════╕
  2.                    │         W E L C O M E         │
  3.                    │  To the VGA Trainer Program   │ │
  4.                    │              By               │ │
  5.                    │      DENTHOR of ASPHYXIA      │ │ │
  6.                    ╘═══════════════════════════════╛ │ │
  7.                      ────────────────────────────────┘ │
  8.                        ────────────────────────────────┘
  9.  
  10.                            --==[ PART 3 ]==--
  11.  
  12.  
  13.  
  14. ■ Introduction
  15.  
  16.  
  17.  
  18. Greetings! This is the third part of the VGA Trainer series! Sorry it 
  19. took so long to get out, but I had a running battle with the traffic
  20. department for three days to get my car registered, and then the MailBox
  21. went down. Ahh, well, life stinks. Anyway, today will do some things
  22. vital to most programs : Lines and circles.
  23.  
  24. Watch out for next week's part : Virtual screens. The easy way to
  25. eliminate flicker, "doubled sprites", and subjecting the user to watch
  26. you building your screen. Almost every ASPHYXIA demo has used a virtual
  27. screen (with the exception of the SilkyDemo), so this is one to watch out
  28. for. I will also show you how to put all of these loose procedures into
  29. units.
  30.  
  31. If you would like to contact me, or the team, there are many ways you 
  32. can do it : 1) Write a message to Grant Smith in private mail here on
  33.                   the Mailbox BBS.
  34.             2) Write a message here in the Programming conference here
  35.                   on the Mailbox (Preferred if you have a general
  36.                   programming query or problem others would benefit from)
  37.             3) Write to ASPHYXIA on the ASPHYXIA BBS.
  38.             4) Write to Denthor, Eze or Livewire on Connectix.
  39.             5) Write to :  Grant Smith
  40.                            P.O.Box 270 Kloof
  41.                            3640
  42.             6) Call me (Grant Smith) at 73 2129 (leave a message if you 
  43.                   call during varsity)
  44.                   
  45. NB : If you are a representative of a company or BBS, and want ASPHYXIA 
  46.        to do you a demo, leave mail to me; we can discuss it.
  47. NNB : If you have done/attempted a demo, SEND IT TO ME! We are feeling
  48.         quite lonely and want to meet/help out/exchange code with other demo
  49.         groups. What do you have to lose? Leave a message here and we can work
  50.         out how to transfer it. We really want to hear from you!
  51.  
  52.  
  53. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  54. ■  Circle Algorithim
  55.  
  56. You all know what a circle looks like. But how do you draw one on the
  57. computer?
  58.  
  59. You probably know circles drawn with the degrees at these points :
  60.  
  61.                                 0
  62.                               ▄█|█▄
  63.                              ███|███
  64.                         270 ----+---- 90
  65.                              ███|███
  66.                               ▀█|█▀
  67.                                180
  68.  
  69. Sorry about my ASCI ;-) ... anyway, Pascal doesn't work that way ... it
  70. works with radians instead of degrees. (You can convert radians to degrees,
  71. but I'm not going to go into that now. Note though that in pascal, the
  72. circle goes like this :
  73.  
  74.                                270
  75.                               ▄█|█▄
  76.                              ███|███
  77.                         180 ----+---- 0
  78.                              ███|███
  79.                               ▀█|█▀
  80.                                 90
  81.  
  82.  
  83. Even so, we can still use the famous equations to draw our circle ...
  84. (You derive the following by using the theorem of our good friend
  85. Pythagoras)
  86.                      Sin (deg) = Y/R
  87.                      Cos (deg) = X/R
  88. (This is standard 8(?) maths ... if you haven't reached that level yet,
  89. take this to your dad, or if you get stuck leave me a message and I'll
  90. do a bit of basic Trig with you. I aim to please ;-))
  91.  
  92. Where Y = your Y-coord
  93.       X = your X-coord
  94.       R = your radius (the size of your circle)
  95.       deg = the degree
  96.  
  97. To simplify matters, we rewrite the equation to get our X and Y values :
  98.  
  99.                      Y = R*Sin(deg)
  100.                      X = R*Cos(deg)
  101.  
  102. This obviousy is perfect for us, because it gives us our X and Y co-ords
  103. to put into our putpixel routine (see Part 1). Because the Sin and Cos
  104. functions return a Real value, we use a round function to transform it
  105. into an Integer.
  106.  
  107.      Procedure Circle (oX,oY,rad:integer;Col:Byte);
  108.      VAR deg:real;
  109.          X,Y:integer;
  110.      BEGIN
  111.        deg:=0;
  112.        repeat
  113.          X:=round(rad*COS (deg));
  114.          Y:=round(rad*sin (deg));
  115.          putpixel (x+ox,y+oy,Col);
  116.          deg:=deg+0.005;
  117.        until (deg>6.4);
  118.      END;
  119.  
  120. In the above example, the smaller the amount that deg is increased by,
  121. the closer the pixels in the circle will be, but the slower the procedure.
  122. 0.005 seem to be best for the 320x200 screen. NOTE : ASPHYXIA does not use
  123. this particular circle algorithm, ours is in assembly language, but this
  124. one should be fast enough for most. If it isn't, give us the stuff you are
  125. using it for and we'll give you ours.
  126.  
  127.  
  128. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  129. ■  Line algorithms
  130.  
  131. There are many ways to draw a line on the computer. I will describe one
  132. and give you two. (The second one you can figure out for yourselves; it
  133. is based on the first one but is faster)
  134.  
  135. The first thing you need to do is pass what you want the line to look
  136. like to your line procedure. What I have done is said that x1,y1 is the
  137. first point on the screen, and x2,y2 is the second point. We also pass the
  138. color to the procedure. (Remember the screens top left hand corner is (0,0);
  139. see Part 1)
  140.  
  141. Ie.            o  (X1,Y1)
  142.                 ooooooooo
  143.                          ooooooooo
  144.                                   oooooooo  (X2,Y2)
  145.  
  146. Again, sorry about my drawings ;-)
  147.  
  148. To find the length of the line, we say the following :
  149.  
  150.            XLength = ABS (x1-x2)
  151.            YLength = ABS (y1-y2)
  152.  
  153. The ABS function means that whatever the result, it will give you an
  154. absolute, or posotive, answer. At this stage I set a variable stating
  155. wheter the difference between the two x's are negative, zero or posotive.
  156. (I do the same for the y's) If the difference is zero, I just use a loop
  157. keeping the two with the zero difference posotive, then exit.
  158.  
  159. If neither the x's or y's have a zero difference, I calculate the X and Y
  160. slopes, using the following two equations :
  161.  
  162.            Xslope = Xlength / Ylength
  163.            Yslope = Ylength / Xlength
  164.  
  165. As you can see, the slopes are real numbers.
  166. NOTE : XSlope = 1 / YSlope
  167.  
  168. Now, there are two ways of drawing the lines :
  169.  
  170.            X = XSlope * Y
  171.            Y = YSlope * X
  172.  
  173. The question is, which one to use? if you use the wrong one, your line
  174. will look like this :
  175.  
  176.         o
  177.            o
  178.               o
  179.  
  180. Instead of this :
  181.  
  182.         ooo
  183.            ooo
  184.               ooo
  185.  
  186. Well, the solution is as follows :
  187.  
  188.                            *\``|``/*
  189.                            ***\|/***
  190.                            ----+----
  191.                            ***/|\***
  192.                            */``|``\*
  193.  
  194. If the slope angle is in the area of the stars (*) then use the first
  195. equation, if it is in the other section (`) then use the second one.
  196. What you do is you calculate the variable on the left hand side by
  197. putting the variable on the right hand side in a loop and solving. Below
  198. is our finished line routine :
  199.  
  200. Procedure Line (x1,y1,x2,y2:integer;col:byte);
  201. VAR x,y,xlength,ylength,dx,dy:integer;
  202.     xslope,yslope:real;
  203. BEGIN
  204.   xlength:=abs (x1-x2);
  205.   if (x1-x2)<0 then dx:=-1;
  206.   if (x1-x2)=0 then dx:=0;
  207.   if (x1-x2)>0 then dx:=+1;
  208.   ylength:=abs (y1-y2);
  209.   if (y1-y2)<0 then dy:=-1;
  210.   if (y1-y2)=0 then dy:=0;
  211.   if (y1-y2)>0 then dy:=+1;
  212.   if (dy=0) then BEGIN
  213.     if dx<0 then for x:=x1 to x2 do
  214.       putpixel (x,y1,col);
  215.     if dx>0 then for x:=x2 to x1 do
  216.       putpixel (x,y1,col);
  217.     exit;
  218.   END;
  219.   if (dx=0) then BEGIN
  220.     if dy<0 then for y:=y1 to y2 do
  221.       putpixel (x1,y,col);
  222.     if dy>0 then for y:=y2 to y1 do
  223.       putpixel (x1,y,col);
  224.     exit;
  225.   END;
  226.   xslope:=xlength/ylength;
  227.   yslope:=ylength/xlength;
  228.   if (yslope/xslope<1) and (yslope/xslope>-1) then BEGIN
  229.     if dx<0 then for x:=x1 to x2 do BEGIN
  230.                    y:= round (yslope*x);
  231.                    putpixel (x,y,col);
  232.                  END;
  233.     if dx>0 then for x:=x2 to x1 do BEGIN
  234.                    y:= round (yslope*x);
  235.                    putpixel (x,y,col);
  236.                  END;
  237.   END
  238.   ELSE
  239.   BEGIN
  240.     if dy<0 then for y:=y1 to y2 do BEGIN
  241.                    x:= round (xslope*y);
  242.                    putpixel (x,y,col);
  243.                  END;
  244.     if dy>0 then for y:=y2 to y1 do BEGIN
  245.                    x:= round (xslope*y);
  246.                    putpixel (x,y,col);
  247.                  END;
  248.   END;
  249. END;
  250.  
  251. Quite big, isn't it? Here is a much shorter way of doing much the same
  252. thing :
  253.  
  254. function sgn(a:real):integer;
  255. begin
  256.      if a>0 then sgn:=+1;
  257.      if a<0 then sgn:=-1;
  258.      if a=0 then sgn:=0;
  259. end;
  260.  
  261. procedure line(a,b,c,d,col:integer);
  262. var u,s,v,d1x,d1y,d2x,d2y,m,n:real;
  263.     i:integer;
  264. begin
  265.      u:= c - a;
  266.      v:= d - b;
  267.      d1x:= SGN(u);
  268.      d1y:= SGN(v);
  269.      d2x:= SGN(u);
  270.      d2y:= 0;
  271.      m:= ABS(u);
  272.      n := ABS(v);
  273.      IF NOT (M>N) then
  274.      BEGIN
  275.           d2x := 0 ;
  276.           d2y := SGN(v);
  277.           m := ABS(v);
  278.           n := ABS(u);
  279.      END;
  280.      s := INT(m / 2);
  281.      FOR i := 0 TO round(m) DO
  282.      BEGIN
  283.           putpixel(a,b,col);
  284.           s := s + n;
  285.           IF not (s<m) THEN
  286.           BEGIN
  287.                s := s - m;
  288.                a:= a +round(d1x);
  289.                b := b + round(d1y);
  290.           END
  291.           ELSE
  292.           BEGIN
  293.                a := a + round(d2x);
  294.                b := b + round(d2y);
  295.           END;
  296.      end;
  297. END;
  298.  
  299. This routine is very fast, and should meet almost all of your requirements
  300. (ASPHYXIA used it for quite a while before we made our new one.)
  301. In the end program, both the new line routine and the circle routine are
  302. tested. A few of the procedures of the first parts are also used.
  303.  
  304. Line and circle routines may seem like fairly trivial things, but they are
  305. a vital component of many programs, and you may like to look up other
  306. methods of drawing them in books in the library (I know that here at the
  307. varsity they have books for doing this kind of stuff all over the place)
  308. A good line routine to look out for is the Bressenhams line routine ...
  309. there is a Bressenhams circle routine too ... I have documentaiton for them
  310. if anybody is interested, they are by far some of the fastest routines
  311. you will use.
  312.  
  313. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  314. ■  In closing
  315.  
  316. Varsity has started again, so I am (shock) going to bed before three in
  317. the morning, so my quote this week wasn't written in the same wasted way
  318. my last weeks one was (For last week's one, I had gotten 8 hours sleep in
  319. 3 days, and thought up and wrote the quote at 2:23 am before I fell asleep.)
  320.  
  321.         [  "What does it do?" she asks.
  322.            "It's a computer," he replies.
  323.            "Yes, dear, but what does it do?"
  324.            "It ..er.. computes! It's a computer."
  325.            "What does it compute?"
  326.            "What? Er? Um. Numbers! Yes, numbers!" He smiles
  327.               worriedly.
  328.            "Why?"
  329.            "Why? Well ..um.. why?" He starts to sweat.
  330.            "I mean, is it just something to dust around, or does
  331.               it actually do something useful?"
  332.            "Um...you can call other computers with it!" Hope lights
  333.               up his eyes. "So you can get programs from other computers!"
  334.            "I see. Tell me, what do these programs do?"
  335.            "Do? I don't think I fol..."
  336.            "I see. They compute. Numbers. For no particular reason." He
  337.               withers under her gaze.
  338.            "Yes, but..."
  339.            She smiles, and he trails off, defeated. She takes another look
  340.                at the thing. "Although," she says, with a strange look in
  341.                her eyes. He looks up, an insane look of hope on his
  342.                face. "Does it come in pink?" she asks.
  343.                                                                            ]
  344.                                                      - Grant Smith
  345.                                                         Tue 27 July, 1993
  346.                                                          9:35 pm.
  347.  
  348. See you next time,
  349.     - Denthor
  350.