【发布时间】:2017-09-27 20:44:32
【问题描述】:
我正在尝试使用 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;
}
}
【问题讨论】:
-
使用
get_xy(long int offset ...)和if(offset < 0 || offset>SCREEN_SIZE){避免负数变成无符号值。 -
谢谢。我做了建议的更改,但我仍然遇到同样的问题。
-
自信的代码在类型更改方面存在问题 - 某处。祝你好运的想法:看看
offset+dy-dxoffset和其他人是如何计算的。 -
Hmmm
dxoffset = (dx<<8) + (dx<<6); dyoffset = (dy<<8) + (dy<<6); get_xy(offset+dy-dxoffset,t);可以,但是在形成偏移之前需要对 (x +dx) & (y +dy) 进行边界检查。 -
感谢您的建议。我会做出改变,如果我取得了成就,我会告诉你。