2015-12-10 157 views
0

I'm绘制圆的时候试图建立使用C.眼下I'm在屏幕上绘制圆工作256色VGA一个简单的图像编辑器(如油漆)。 I'm得到的问题是,当圆比屏幕不应该是绘制出现在屏幕的另一侧的部分大。我有一个if语句来验证像素是否在屏幕的绘图区域。但我不明白为什么像素会进入屏幕的另一侧。检查屏幕边界在VGA图形

这是我得到的问题:

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;  

    } 


} 
+0

使用'get_xy(长整型偏移...)'和'如果(偏移<0 ||偏移> SCREEN_SIZE){',以避免一个负数成为一些无符号值。 – chux

+0

谢谢。我提出了建议的更改,但我仍然遇到同样的问题。 –

+0

自信的代码有一个问题与类型的变化 - 地方。祝你好运想法:看看'offset + dy-dxoffset'和其他的计算方式。 – chux

回答

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()