有没有好一点的画线算法...任意方向都可以画的.
如题... 貌似可以尝试用线段凑出来。 bresenham画线算法 看ZLGGUI void LCD_BufferDrawLine(u8 x1, u8 y1, u8 x2, u8 y2, DrawMode mode){
u8 x, y, t;
int dx, dy, d, incrE, incrNE;
if( y1 > y2)
{
t = x1, x1 = x2, x2 = t;
t = y1, y1 = y2, y2 = t;
}
dx = x2 - x1;
dy = y2 - y1;
x = x1, y = y1;
if( 0 == dx )
{
LCD_BufferDrawYLine(x1, y1, dy, mode);
}
else if( 0 == dy )
{
if( x1 > x2 )
LCD_BufferDrawXLine(x2, y2, dx, mode);
else
LCD_BufferDrawXLine(x1, y1, dx, mode);
}
else
{
// Bresenham Midpoint Line
if( abs(dx) > abs(dy) )
{
if( x2 > x1)
{
// 0 < k < 1
d = 2 * dy - dx;
incrE = 2 * dy;
incrNE = 2 * (dy - dx);
while( x < x2 )
{
LCD_BufferDrawPixel(x, y, mode);
x++;
if( d > 0 )
{
y++;
d += incrNE;
}
else
d += incrE;
}
LCD_BufferDrawPixel(x, y, mode);
}
else
{
// -1 < k < 0
d = -2 * dy - dx;
incrE = -2 * dy;
incrNE = -2 * (dy + dx);
while( x > x2)
{
LCD_BufferDrawPixel(x, y, mode);
x--;
if( d < 0 )
{
y++;
d += incrNE;
}
else
d += incrE;
}
LCD_BufferDrawPixel(x, y, mode);
}
}
else
{
if( x2 > x1)
{
// k > 1
d = dy - 2 * dx;
incrE = -2 * dx;
incrNE = 2 * (dy - dx);
while( y < y2 )
{
LCD_BufferDrawPixel(x, y, mode);
y++;
if( d < 0 )
{
x++;
d += incrNE;
}
else
d += incrE;
}
LCD_BufferDrawPixel(x, y, mode);
}
else
{
// k < -1
d = -dy - 2 * dx;
incrE = -2 * dx;
incrNE = -2 * (dy + dx);
while( y < y2 )
{
LCD_BufferDrawPixel(x, y, mode);
y++;
if( d > 0 )
{
x--;
d += incrNE;
}
else
d += incrE;
}
LCD_BufferDrawPixel(x, y, mode);
}
}
}
dirty = 1;
} 谢谢..试试.. 试试这个
/************************************************/
/*画线。任意方向的斜线,直线数学方程 aX+bY=1 */
/************************************************/
void Linexy(uchar x0,uchar y0,uchar xt,uchar yt,uchar s)
{
register uchar t;
int xerr=0,yerr=0,delta_x,delta_y,distance;
int incx,incy,uRow,uCol;
delta_x = xt-x0; //计算坐标增量
delta_y = yt-y0;
uRow = x0;
uCol = y0;
if(delta_x>0) incx=1; //设置单步方向
else if( delta_x==0 ) incx=0; //垂直线
else {incx=-1;delta_x=-delta_x;}
if(delta_y>0) incy=1;
else if( delta_y==0 ) incy=0; //水平线
else {incy=-1;delta_y=-delta_y;}
if( delta_x > delta_y ) distance=delta_x; //选取基本增量坐标轴
else distance=delta_y;
for( t=0;t <= distance+1; t++ )
{ //画线输出
point(uRow,uCol,s); //画点
xerr += delta_x ;
yerr += delta_y ;
if( xerr > distance )
{
xerr-=distance;
uRow+=incx;
}
if( yerr > distance )
{
yerr-=distance;
uCol+=incy;
}
}
} 建议楼主去书店买本《计算机图形学》
比在这里问强多了
PS:4楼的程序其实不怎样……虽然写得很工整…… 我其实已经买了一本《计算机图形学》 了.../emotion/em043.gif./emotion/em043.gif看来是要多看看书. 尝试下 计算机图形学,很好的东西。
看你画线的目的,其实很多应用是不需要斜线的,只是画画横线和竖线。 to watercat
真心请教下,如何改进。要考虑不同的斜率,写得可能是有点笨。 void LCD_DrawLine(unsigned int x1, unsignedint y1, unsignedint x2, unsignedint y2,unsignedint color)
{
unsignedint x, y, t;
if((x1 == x2) && (y1 == y2))
LCD_DrawPoint(x1, y1,color);
else if( (abs(y2 - y1))> (abs(x2 - x1)))
{
if(y1 > y2)
{
t = y1;
y1 = y2;
y2 = t;
t = x1;
x1 = x2;
x2 = t;
}
for(y = y1; y <= y2; y ++)
{
x = (y - y1) * (x2 - x1) / (y2 - y1) + x1;
LCD_DrawPoint(x, y,color);
}
}
else
{
if(x1 > x2)
{
t = y1;
y1 = y2;
y2 = t;
t = x1;
x1 = x2;
x2 = t;
}
for(x = x1; x <= x2; x ++)
{
y = (x - x1) * (y2 - y1) / (x2 - x1) + y1;
LCD_DrawPoint(x, y,color);
}
}
}
以前网上找的 楼上的算法采用y=kx+c来画线,没有bresenham画线算法好。 to 【12楼】 ppa2001
函数有bug啊
else if( (abs(y2 - y1))> (abs(x2 - x1)))
{
if(y1 > y2)
{
t = y1;
y1 = y2;
y2 = t;
t = x1;
x1 = x2;
x2 = t;
}
for(y = y1; y <= y2; y ++)
{
x = (y - y1) * (x2 - x1) / (y2 - y1) + x1;
LCD_DrawPoint(x, y,color);
}
}
x = (y - y1) * (x2 - x1) / (y2 - y1) + x1; 这里会丢失不少x 我是来学习的 【6楼】 cqfeiyu
效果还行的 mark 我是来学习的 看在什么平台上了,Qt里有专门的DrawLine API,还有DrawRect等等 simond 发表于 2009-5-18 19:48 static/image/common/back.gif
to 【12楼】 ppa2001
函数有bug啊
为什么会丢失x,求解释
页:
[1]