Checking screen bounds when drawing circle in VGA graphics

0

I´m trying to create a simple image editor ( like paint) on a 256-color VGA using C. Right now I´m working on drawing circles on the screen. The problem I´m getting is that when the circle is bigger than the screen the part that should not be drawn is appearing on the opposite side of the screen. I have an if statement verifying that the pixel is on the drawing area of the screen. But I can´t understand why the pixels are going to the opposite side of the screen.

This is the problem I get:

enter image description here

and this is the code for drawing the circle and checking bounds. I have a function get_xy() that is giving me the x,y coordinates of an offset for the video memory, I use this coordinates to check if the pixel is going to be plotted within the drawing area:

#define SCREEN_SIZE         (word)(SCREEN_WIDTH*SCREEN_HEIGHT)
typedef unsigned char  byte;
typedef unsigned short word;
typedef long           fixed16_16;

fixed16_16 SIN_ACOS[1024];
byte *VGA=(byte *)0xA0000000L;         /* this points to video memory. */
word *my_clock=(word *)0x0000046C;    /* this points to the 18.2hz system
                                             clock. */
 /**************************************************************************
 *  circle                                                           *
 *    Draw circle                                                             *
 **************************************************************************/

void circle(int x,int y, int radius, byte color)
{
  fixed16_16 n=0,invradius=(1/(float)radius)*0x10000L;
  long int dx=0,dy=radius-1;
  int t[2];


  long int dxoffset,dyoffset,offset = (y<<8)+(y<<6)+x;
    if(!(y>0 && y<180&&x>32 && x<=320)){return;}
  while (dx<=dy)
  {
    dxoffset = (dx<<8) + (dx<<6);
    dyoffset = (dy<<8) + (dy<<6);
    get_xy(offset+dy-dxoffset,t);

    if(t[1]>0 && t[1]<180&&t[0]>32 && t[0] <=320){ /*Checking if is inside drawing area*/
        VGA[offset+dy-dxoffset] = color;  /* octant 0 */
    }

    get_xy(offset+dx-dyoffset,t);
    //printf("offset: %u \n",offset+dx-dyoffset);
    if(t[1]>0 && t[1]<180&&t[0]>32 && t[0] <=320){ /*Checking if is inside drawing area*/
        VGA[offset+dx-dyoffset] = color;  /* octant 1 */
    }

    get_xy(offset-dx-dyoffset,t);
    if(t[1]>0 && t[1]<180&&t[0]>32 && t[0] <=320){ /*Checking if is inside drawing area*/
        VGA[offset-dx-dyoffset] = color;  /* octant 2 */
    }  


    get_xy(offset-dy-dxoffset,t);
    if(t[1]>0 && t[1]<180&&t[0]>32 && t[0] <=320){ /*Checking if is inside drawing area*/
        VGA[offset-dy-dxoffset] = color;  /* octant 3 */
    }  


    get_xy(offset-dy+dxoffset,t);
    if(t[1]>0 && t[1]<180&&t[0]>32 && t[0] <=320){ /*Checking if is inside drawing area*/
        VGA[offset-dy+dxoffset] = color;  /* octant 4 */
    }  


    get_xy(offset-dx+dyoffset,t);
    if(t[1]>0 && t[1]<180&&t[0]>32 && t[0] <=320){ /*Checking if is inside drawing area*/
        VGA[offset-dx+dyoffset] = color;  /* octant 5 */
    }  

    get_xy(offset+dx+dyoffset,t);
    if(t[1]>0 && t[1]<180&&t[0]>32 && t[0] <=320){ /*Checking if is inside drawing area*/
        VGA[offset+dx+dyoffset] = color;  /* octant 6 */
    } 


    get_xy(offset+dy+dxoffset,t);
    if(t[1]>0 && t[1]<180&&t[0]>32 && t[0] <=320){ /*Checking if is inside drawing area*/
        VGA[offset+dy+dxoffset] = color;  /* octant 7 */
    } 

    dx = dx+1;
    n+=invradius;
    dy = (long int)((radius * SIN_ACOS[(long int)(n>>6)]) >> 16);
  }
}

void get_xy(long int offset, int* a){
    int x,y;
    int r[2];
    if(offset<0||offset>SCREEN_SIZE){
        a[0]=-500;
        a[1]=-500;
        //printf("grande");
    }
    else{
        y = offset/((1<<8) + (1<<6)); 
        x = offset%((1<<8) + (1<<6)); 

        a[0] =x;
        a[1]=y;     

    }


}
c
vga
asked on Stack Overflow Dec 10, 2015 by Pablo Estrada • edited Dec 10, 2015 by Pablo Estrada

1 Answer

0

Hmmm

 dxoffset = (dx<<8) + (dx<<6);
 dyoffset = (dy<<8) + (dy<<6);
 get_xy(offset+dy-dxoffset,t);

is OK, but bounds checking needs to be done on (x +dx) & (y +dy) before forming the offset.

If bounds checking is done on x+dx and y+dy etc. (that's correct), get_xy() is not needed at all.

answered on Stack Overflow Sep 27, 2017 by Armali

User contributions licensed under CC BY-SA 3.0