0

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

这是我得到的问题:

在此处输入图像描述

这是绘制圆和检查边界的代码。我有一个函数 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;     

    }


}
4

1 回答 1

0

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

可以,但是在形成偏移之前需要对 (x +dx) & (y +dy) 进行边界检查。

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

于 2017-09-27T12:44:32.303 回答