Topic : The VGA Training Program
Author : Grant Smith
Page : << Previous 5  Next >>
Go to page :


Even so, we can still use the famous equations to draw our circle ...
(You derive the following by using the theorem of our good friend
                     Sin (deg) = Y/R
                     Cos (deg) = X/R
(This is standard 8(?) maths ... if you haven't reached that level yet,
take this to your dad, or if you get stuck leave me a message and I'll
do a bit of basic Trig with you. I aim to please ;-))

Where Y = your Y-coord
      X = your X-coord
      R = your radius (the size of your circle)
      deg = the degree

To simplify matters, we rewrite the equation to get our X and Y values :

                     Y = R*Sin(deg)
                     X = R*Cos(deg)

This obviousy is perfect for us, because it gives us our X and Y co-ords to
put into our Putpixel routine (see Part 1). Because the Sin and Cos
functions return a Real [long] value, we use a round function to transform
it into an Integer.


     Procedure Circle (oX,oY,rad:integer;Col:Byte);
     VAR deg:real;
         X:=round(rad*COS (deg));
         Y:=round(rad*sin (deg));
         putpixel (x+ox,y+oy,Col);
       until (deg>6.4);


     void Circle(int X, int Y, int rad, int col) {

       float deg = 0;

       do {
         X = round(rad * cos(deg));
         Y = round(rad * sin(deg));
         Putpixel (X+160, Y+100, col);
         deg += 0.005;
       while (deg <= 6.4);


In the above example, the smaller the amount that deg is increased by,
the closer the pixels in the circle will be, but the slower the procedure.
0.005 seem to be best for the 320x200 screen. NOTE : ASPHYXIA does not use
this particular circle algorithm, ours is in assembly language, but this
one should be fast enough for most. If it isn't, give us the stuff you are
using it for and we'll give you ours.

  Line algorithms

There are many ways to draw a line on the computer. I will describe one
and give you two. (The second one you can figure out for yourselves; it
is based on the first one but is faster)

The first thing you need to do is pass what you want the line to look
like to your line procedure. What I have done is said that x1,y1 is the
first point on the screen, and x2,y2 is the second point. We also pass the
color to the procedure. (Remember the screens top left hand corner is (0,0);
see Part 1)

Ie.            o  (X1,Y1)
                                  oooooooo  (X2,Y2)

Again, sorry about my drawings ;-)

To find the length of the line, we say the following :


           XLength = ABS (x1-x2)
           YLength = ABS (y1-y2)


           xlength = abs(x1-x2);
           ylength = abs(y1-y2);

The ABS function means that whatever the result, it will give you an
absolute, or posotive, answer. At this stage I set a variable stating
wheter the difference between the two x's are negative, zero or posotive.
(I do the same for the y's) If the difference is zero, I just use a loop
keeping the two with the zero difference posotive, then exit.

If neither the x's or y's have a zero difference, I calculate the X and Y
slopes, using the following two equations :


           Xslope = Xlength / Ylength
           Yslope = Ylength / Xlength


[Note: C++ is significantly different here.  I had to add special divide
by zero checking.  C++ doesn't like x/0.  I also added in type-casting
which might not have been necessary.  :)]

           if ((xlength != 0) && (ylength != 0)) {
             xslope = (float)xlength/(float)ylength;
             yslope = (float)ylength/(float)xlength;
           else {
             xslope = 0.0;
             yslope = 0.0;

As you can see, the slopes are real numbers.
NOTE : XSlope = 1 / YSlope

Now, there are two ways of drawing the lines :

           X = XSlope * Y
           Y = YSlope * X

The question is, which one to use? if you use the wrong one, your line
will look like this :


Instead of this :


Well, the solution is as follows :


If the slope angle is in the area of the stars (*) then use the first
equation, if it is in the other section (`) then use the second one.
What you do is you calculate the variable on the left hand side by
putting the variable on the right hand side in a loop and solving. Below
is our finished line routine :


Procedure Line (x1,y1,x2,y2:integer;col:byte);
VAR x,y,xlength,ylength,dx,dy:integer;
  xlength:=abs (x1-x2);
  if (x1-x2)<0 then dx:=-1;
  if (x1-x2)=0 then dx:=0;
  if (x1-x2)>0 then dx:=+1;
  ylength:=abs (y1-y2);
  if (y1-y2)<0 then dy:=-1;
  if (y1-y2)=0 then dy:=0;
  if (y1-y2)>0 then dy:=+1;
  if (dy=0) then BEGIN
    if dx<0 then for x:=x1 to x2 do
      putpixel (x,y1,col);
    if dx>0 then for x:=x2 to x1 do

Page : << Previous 5  Next >>