home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 8 / CDASC08.ISO / NEWS / 554 / JUIN / CIRCLE.PAS < prev    next >
Pascal/Delphi Source File  |  1993-10-07  |  4KB  |  123 lines

  1. {─ Fido Pascal Conference ────────────────────────────────────────────── PASCAL ─
  2. Msg  : 629 of 643
  3. From : Michael Nicolai                     2:2401/411.2         06 Jun 93  03:53
  4. To   : Wouter Goderis
  5. Subj : Circle 320x200x256
  6. ────────────────────────────────────────────────────────────────────────────────
  7. # Der User Wouter Goderis@2:291/1500.58 musste am Sonntag, dem 30.05.93 um
  8. 12:19 Uhr
  9. # in der Area PASCAL folgendes seiner Tastatur antun................
  10.  
  11. >         Hmm,
  12. >
  13. >         does someone have a circle routine for the 320x200x256 mode.
  14. >         I need one using the assembler...  (FAST) ( or isn't that possible)
  15. >         I doesn't need to be very perfect, if it has the shape of a circle,
  16. >         I'm satisfied.
  17. >         Can someone give me some hints ?
  18. >
  19. >                                                         \/\/ OUTER
  20.  
  21. I don't have any ASM-program yet but i got the same question some time ago.
  22.  
  23. Here ist what i answered then......
  24.  
  25.  
  26. Ok then, let's do some math:
  27.  
  28. The basic formula (and quickest?) for drawing a circle is: x^2 + y^2 = r^2.
  29. The r stands for radius (half the diameter). You know this formula, i am
  30. sure. A guy called Phytagoras set i up very long ago to calculate the
  31. hypotenuse of a given triangle.
  32.  
  33.  
  34.                    |\
  35.                    | \
  36.                  a |  \ c      c^2 = a^2 + b^2
  37.                    |   \
  38.                    |____\
  39.  
  40.                      b
  41.  
  42. Remember?
  43.  
  44. Now look at this:        ...|     a quater of the circle
  45.                        ..   |
  46.                       . ____|y
  47.                      . |\   |
  48.                     .  | \  |
  49.                     .  | r\ |
  50.                     .  |   \|
  51.                --------------------------
  52.                     r  x    |0
  53.                             |
  54.                             |
  55.  
  56.  
  57. r is given and take 0 - r as a starting point for x. Then all you have to do
  58. is to calculate y and plot the point.
  59.  
  60.     y = sqrt((r * r) - (x * x))      sqrt : square root
  61.  
  62. After each calculation x is increased until it has reached 0. Then one
  63. quarter of the circle is drawn. The other three quarters are symmetrical.
  64.  
  65. I have written a short program for you to draw a circle in 320x200x256
  66. graphics mode. When you key in some values please remember that NO error
  67. checking will be done. x has to be between 0 and 319, and y between 0 and
  68. 199. The radius must NOT be greater than x AND y.
  69.  
  70. Example: x : 160; y : 100; r : 90
  71.  
  72. When you start this program you will not get correct circles because in
  73. graphics mode ONE pixel is not square!!! You have to calculate an aspect
  74. ratio to get nice looking circles.}
  75.  
  76. program circle;
  77.  
  78. uses CRT, DOS;
  79.  
  80. var
  81.  
  82.  regs    : Registers;
  83.  x0, y0  : word;
  84.  x, y, R : real;
  85.  temp    : real;
  86.  c       : char;
  87.  
  88. procedure putpixel(x, y : word; color : byte);
  89. begin
  90.  mem[$A000: (y * 320 + x)] := color;
  91. end;
  92.  
  93. begin
  94.  clrscr;
  95.  writeln('Enter coordinates of middle-point :');
  96.  writeln;
  97.  write('x : '); readln(x0);
  98.  write('y : '); readln(y0);
  99.  writeln;
  100.  write('Enter radius :'); readln(R);
  101.  
  102.  { Switch to 320x200x256 }
  103.  
  104.  regs.ax := $0013;
  105.  intr($10, regs);
  106.  
  107.  x := (-1) * R;  { go from 0 - R to 0 }
  108.  temp := R * R;
  109.  repeat
  110.   y := sqrt(temp - (x * x));
  111.   putpixel((x0 + trunc(x)), (y0 - trunc(y)), 15); { 4.th quadrant }
  112.   putpixel((x0 - trunc(x)), (y0 - trunc(y)), 15); { 1.st quadrant }
  113.   putpixel((x0 + trunc(x)), (y0 + trunc(y)), 15); { 3.rd quadrant }
  114.   putpixel((x0 - trunc(x)), (y0 + trunc(y)), 15); { 2.nd quadrant }
  115.   x := x + 0.1; { change this if you want coarse or fine circle. }
  116.  until (x >= 0.0);
  117.  c := readkey;  { wait for keypress. }
  118.  
  119.  { Switch back to textmode. }
  120.  
  121.  regs.ax := $0003;
  122.  intr($10, regs);
  123. end.