计算机图形学实验报告--Bresenham直线算法与画圆算法 下载本文

实验一名称:基本图形的生成算法 要求:(1)掌握DDA生成线段算法

(2)掌握Bresenham生成线段算法 (3)掌握生成圆弧算法

1. 代码

(1) Bresenham画线算法

void CDlgBresenhamLine::OnBresenhamBUTTON2() {

UpdateData(true);

CDC* pDC=m_picture.GetDC(); int xa=m_start_x, ya=m_start_y, xb=m_end_x, yb=m_end_y,c=RGB(0,255,0);

int dx=abs(xa-xb),dy=abs(ya-yb); int p=2*dy-dx; int x,y; if(xa>xb){ x=xb; y=yb; xb=xa; } else{

x=xa; y=ya; }

while(x

p+=2*dy; else{ y++;

p+=2*(dy-dx); }

pDC->SetPixel((int)x, (int)y, c); }

ReleaseDC(pDC); }

(2) 中点画圆算法

void CDMidpointcircleDlg::OncircleBUTTON2() {

// TODO: Add your control notification handler code here UpdateData(true);

CDC* pDC=m_picture.GetDC();

int xc=m_x, yc=m_y, r=m_r, c=RGB(255,0,0); int d=(5-r*4)/4; int x=0,y=r; while(x<=y){

pDC->SetPixel(xc+x,yc+y,c); pDC->SetPixel(xc+x,yc-y,c); pDC->SetPixel(xc-x,yc+y,c); pDC->SetPixel(xc-x,yc-y,c); pDC->SetPixel(xc+y,yc+x,c); pDC->SetPixel(xc+y,yc-x,c); pDC->SetPixel(xc-y,yc+x,c); pDC->SetPixel(xc-y,yc-x,c);

if(d<0)

d += 2*x+3; else{

d += 2*(x-y)+5; y --; } x ++; }

ReleaseDC(pDC);

}

2. 结果截图

(1) Bresenham画线

(2) 中点画圆