在VGA图形中绘制圆形时检查屏幕边界

时间:2015-12-10 04:06:08

标签: c vga

我正在尝试使用C在256色VGA上创建一个简单的图像编辑器(如油漆)。现在我正在绘制屏幕上的绘图圆圈。我得到的问题是,当圆圈大于屏幕时,不应绘制的部分出现在屏幕的另一侧。我有一个if语句验证像素是否在屏幕的绘图区域。但我不明白为什么像素会移动到屏幕的另一侧。

这是我得到的问题:

enter image description here

这是绘制圆圈和检查边界的代码。我有一个函数get_xy(),它给出了视频内存偏移的x,y坐标,我用这个坐标来检查像素是否将在绘图区域内绘制:

#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;     

    }


}

1 个答案:

答案 0 :(得分:0)

  

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

没问题,但需要在(x + dx)&amp;上进行边界检查。 (y + dy)   在形成偏移之前。

如果对x+dxy+dy等进行边界检查(这是正确的),则根本不需要get_xy()

相关问题