gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:07:09 25 4
gpt4 key购买 nike

我正在尝试使用 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;

}


}

最佳答案

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.

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

关于在 VGA 图形中绘制圆圈时检查屏幕边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34193507/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com