画图板--用DDA算法和Bresenham算法画直线

本文介绍了在任意斜率下使用DDA和Bresenham算法绘制直线的方法,并提供了具体实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上一篇文章中为了简单起见,直接用了CDC的画直线功能,这几天认真研读了图形学的课本,发现书上的算法都是假定直线斜率-1<m<1的情况下适用的,参考了网上的一些资料,将在任意斜率下画直线的两种算法实现如下:
<?xml:namespace prefix = o />

None.gif void Line::Draw_DDA(CDC * pDC)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {//用DDA算法画直线
InBlock.gif
inti;
InBlock.gif
InBlock.gif
if(pStart.x==pEnd.x)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//为竖线
InBlock.gif
if(pStart.y<=pEnd.y)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
for(i=pStart.y;i<=pEnd.y;i++)
InBlock.gifpDC
->SetPixel(pStart.x,i,m_lPenColor);
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
for(i=pEnd.y;i<=pStart.y;i++)
InBlock.gifpDC
->SetPixel(pStart.x,i,m_lPenColor);
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
return;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
//为横线
InBlock.gif
if(pStart.y==pEnd.y)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(pStart.x<=pEnd.x)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
for(i=pStart.x;i<=pEnd.x;i++)
InBlock.gifpDC
->SetPixel(i,pStart.y,m_lPenColor);
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
for(i=pEnd.x;i<=pStart.x;i++)
InBlock.gifpDC
->SetPixel(i,pStart.y,m_lPenColor);
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
return;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
//为斜线
InBlock.gif
doublem=(pEnd.y-pStart.y)*1.0/(pEnd.x-pStart.x);
InBlock.gif
floatfTemp;
InBlock.gif
InBlock.gif
if(abs(m)<=1)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(pStart.x<pEnd.x)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.giffTemp
=pStart.y-m;
InBlock.gif
for(i=pStart.x;i<=pEnd.x;i++)
InBlock.gifpDC
->SetPixel(i,fTemp+=m,m_lPenColor);
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.giffTemp
=pEnd.y-m;
InBlock.gif
for(i=pEnd.x;i<=pStart.x;i++)
InBlock.gifpDC
->SetPixel(i,fTemp+=m,m_lPenColor);
ExpandedSubBlockEnd.gif}

InBlock.gif
return;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
if(pStart.y<pEnd.y)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.giffTemp
=pStart.x-1/m;
InBlock.gif
for(i=pStart.y;i<=pEnd.y;i++)
InBlock.gifpDC
->SetPixel(fTemp+=1/m,i,m_lPenColor);
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.giffTemp
=pEnd.x-1/m;
InBlock.gif
for(i=pEnd.y;i<=pStart.y;i++)
InBlock.gifpDC
->SetPixel(fTemp+=1/m,i,m_lPenColor);
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif

None.gif void Line::Draw_Bresenham(CDC * pDC)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {//用Bresenham算法画直线
InBlock.gif
inti;
InBlock.gif
InBlock.gif
if(pStart.x==pEnd.x)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//为竖线
InBlock.gif
if(pStart.y<=pEnd.y)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
for(i=pStart.y;i<=pEnd.y;i++)
InBlock.gifpDC
->SetPixel(pStart.x,i,m_lPenColor);
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
for(i=pEnd.y;i<=pStart.y;i++)
InBlock.gifpDC
->SetPixel(pStart.x,i,m_lPenColor);
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
return;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
//为横线
InBlock.gif
if(pStart.y==pEnd.y)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(pStart.x<=pEnd.x)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
for(i=pStart.x;i<=pEnd.x;i++)
InBlock.gifpDC
->SetPixel(i,pStart.y,m_lPenColor);
ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
for(i=pEnd.x;i<=pStart.x;i++)
InBlock.gifpDC
->SetPixel(i,pStart.y,m_lPenColor);
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
return;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
//为斜线
InBlock.gif
floatm=(pEnd.y-pStart.y)*1.0/(pEnd.x-pStart.x);
InBlock.gif
floatp;
InBlock.gif
InBlock.gifp
=2*m-1;
InBlock.gif
if(m>0&&m<=1)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(pStart.x<pEnd.x)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
while(pStart.x<=pEnd.x)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifpDC
->SetPixel(pStart.x++,pStart.y,m_lPenColor);
InBlock.gif
if(p>=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifp
+=2*m-2;
InBlock.gifpStart.y
++;
ExpandedSubBlockEnd.gif}

InBlock.gif
else
InBlock.gifp
+=2*m;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
while(pEnd.x<=pStart.x)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifpDC
->SetPixel(pEnd.x++,pEnd.y,m_lPenColor);
InBlock.gif
if(p>=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifp
+=2*m-2;
InBlock.gifpEnd.y
++;
ExpandedSubBlockEnd.gif}

InBlock.gif
else
InBlock.gifp
+=2*m;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
return;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gifp
=-2*m-1;
InBlock.gif
if(m<0&&m>=-1)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(pStart.x<pEnd.x)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
while(pStart.x<=pEnd.x)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifpDC
->SetPixel(pStart.x++,pStart.y,m_lPenColor);
InBlock.gif
if(p>=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifp
+=-2*m-2;
InBlock.gifpStart.y
--;
ExpandedSubBlockEnd.gif}

InBlock.gif
else
InBlock.gifp
+=-2*m;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
while(pEnd.x<=pStart.x)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifpDC
->SetPixel(pEnd.x++,pEnd.y,m_lPenColor);
InBlock.gif
if(p>=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifp
+=-2*m-2;
InBlock.gifpEnd.y
--;
ExpandedSubBlockEnd.gif}

InBlock.gif
else
InBlock.gifp
+=-2*m;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
return;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gifp
=2/m-1;
InBlock.gif
if(m>1)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
if(pStart.y<pEnd.y)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
while(pStart.y<=pEnd.y)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifpDC
->SetPixel(pStart.x,pStart.y++,m_lPenColor);
InBlock.gif
if(p>=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifp
+=2/m-2;
InBlock.gifpStart.x
++;
ExpandedSubBlockEnd.gif}

InBlock.gif
else
InBlock.gifp
+=2/m;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
while(pEnd.y<=pStart.y)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifpDC
->SetPixel(pEnd.x,pEnd.y++,m_lPenColor);
InBlock.gif
if(p>=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifp
+=2/m-2;
InBlock.gifpEnd.x
++;
ExpandedSubBlockEnd.gif}

InBlock.gif
else
InBlock.gifp
+=2/m;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
return;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gifp
=-2/m-1;
InBlock.gif
if(pStart.y<pEnd.y)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
while(pStart.y<=pEnd.y)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifpDC
->SetPixel(pStart.x,pStart.y++,m_lPenColor);
InBlock.gif
if(p>=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifp
+=-2/m-2;
InBlock.gifpStart.x
--;
ExpandedSubBlockEnd.gif}

InBlock.gif
else
InBlock.gifp
+=-2/m;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
else
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
while(pEnd.y<=pStart.y)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifpDC
->SetPixel(pEnd.x,pEnd.y++,m_lPenColor);
InBlock.gif
if(p>=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifp
+=-2/m-2;
InBlock.gifpEnd.x
--;
ExpandedSubBlockEnd.gif}

InBlock.gif
else
InBlock.gifp
+=-2/m;
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
None.gif

源代码下载

窗口重绘还是有问题,郁闷。。。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值