图形学画圆实验报告.docx

上传人:b****6 文档编号:12329867 上传时间:2023-06-05 格式:DOCX 页数:8 大小:30.03KB
下载 相关 举报
图形学画圆实验报告.docx_第1页
第1页 / 共8页
图形学画圆实验报告.docx_第2页
第2页 / 共8页
图形学画圆实验报告.docx_第3页
第3页 / 共8页
图形学画圆实验报告.docx_第4页
第4页 / 共8页
图形学画圆实验报告.docx_第5页
第5页 / 共8页
图形学画圆实验报告.docx_第6页
第6页 / 共8页
图形学画圆实验报告.docx_第7页
第7页 / 共8页
图形学画圆实验报告.docx_第8页
第8页 / 共8页
亲,该文档总共8页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

图形学画圆实验报告.docx

《图形学画圆实验报告.docx》由会员分享,可在线阅读,更多相关《图形学画圆实验报告.docx(8页珍藏版)》请在冰点文库上搜索。

图形学画圆实验报告.docx

图形学画圆实验报告

图形学画圆实验报告

4.2.3程序实现与上机实习

(二)

一、实验目的

编写圆和椭圆的扫描转换算法程序,验证算法的正确性。

二、实验任务

1.编写中点画圆法的扫描转换程序,考虑原点在(x0,y0)处程序的改动;

2.添加鼠标程序,实现交互式画圆;

3.编写中点画椭圆法的扫描转换程序;

4.添加鼠标程序,实现交互式画椭圆;

三、实验内容

1.编写中点画圆法的扫描转换程序,考虑原点在(x0,y0)处程序的改动;

分析:

考虑圆心不再原点,设圆心坐标为(x0,y0)。

通过平移坐标原点到圆心,则第二个8分圆上一点p(x,y),其原始坐标为

x’=x+x0

y’=y+y0

即p’1(x0+x,y+y0)

其它7个对称点分别是:

p’2(x0+y,y+x0),p’3(x0+y,y0-x),p’4(x0+x,y0-y),p’5(x0-x,y0-y),p’6(x0-y,y0-x),p’7(x0-y,y0+x),p’8(x0-x,y0+y)

算法程序如下:

MidpointCircle(intx0,inty0,intr,intcolor)

{

intx,y;

floatd;

x=0;y=r;d=1.25-r;

CirPot(x0,y0,x,y,color);

while(x<=y)

{

if(d<0)

{

d+=2*x+3;x++;

}

else

{

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

x++;y--;

}

CirPot(x0,y0,x,y,color);

}/*while*/

}/*MidpointCiecle*/

intCirPot(intx0,inty0,intx,inty,intcolor)

{

Setpixel((x0+x),(y0+y));

Setpixel((x0+y),(y0+x));

Setpixel((x0+y),(y0-x));

Setpixel((x0+x),(y0-y));

Setpixel((x0-x),(y0-y));

Setpixel((x0-y),(y0-x));

Setpixel((x0-y),(y0+x));

Setpixel((x0-x),(y0+y));

}

程序实现步骤:

(1)建立MidPointCircle工程文件;

(2)右击CMidPointCircleView类,建立成员函数

voidMidpointCircle(CDC*pDC,intx0,inty0,intr,COLORREFcolor)

intCirPot(CDC*pDC,intx0,inty0,intx,inty,COLORREFcolor)

(3)编写成员函数代码,程序如下:

voidCMidPointCircleView:

:

MidpointCircle(CDC*pDC,intx0,inty0,intr,COLORREFcolor)

{

intx,y;

floatd;

x=0;y=r;d=1.25-r;

CirPot(pDC,x0,y0,x,y,color);

while(x<=y)

{

if(d<0)

{

d+=2*x+3;x++;

}

else

{

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

x++;y--;

}

CirPot(pDC,x0,y0,x,y,color);

}/*while*/

}

intCMidPointCircleView:

:

CirPot(CDC*pDC,intx0,inty0,intx,inty,COLORREFcolor)

{

pDC->SetPixel((x0+x),(y0+y),color);

pDC->SetPixel((x0+y),(y0+x),color);

pDC->SetPixel((x0+y),(y0-x),color);

pDC->SetPixel((x0+x),(y0-y),color);

pDC->SetPixel((x0-x),(y0-y),color);

pDC->SetPixel((x0-y),(y0-x),color);

pDC->SetPixel((x0-y),(y0+x),color);

pDC->SetPixel((x0-x),(y0+y),color);

return0;

}

(4)编写OnDraw(CDC*pDC)函数,程序如下:

voidCMidPointCircleView:

:

OnDraw(CDC*pDC)

{

CMidPointCircleDoc*pDoc=GetDocument();

ASSERT_VALID(pDoc);

//TODO:

adddrawcodefornativedatahere

MidpointCircle(pDC,100,100,10,RGB(255,0,0));

MidpointCircle(pDC,500,300,60,RGB(255,255,0));

}

(6)编译、运行程序,查看结果。

任务2:

添加鼠标程序,实现交互式画圆

在任务1的基础上,完成下列步骤:

(1)向视图类中添加自定义的成员变量

用鼠标右键单击视图类,选择“AddMemberVariable…”,添加下面三个成员变量。

proctected:

intm_r;//半径

CPointm_bO;//圆心

CPointm_bR;//圆上的点

intm_ist;//圆心与圆周上点的区别,m_ist=0,表示鼠标左击点为圆心,

//m_ist=1,表示鼠标左击点为圆周上的点

(2)在视图类CPP文件的构造函数中初始化成员变量

CMidPointCircleMouseView:

:

CMidPointCircleMouseView()

{

//TODO:

addconstructioncodehere

m_bO.x=0;m_bO.y=0;//圆心

m_bR.x=0;m_bR.y=0;//圆上的点

m_ist=0;//圆心与圆上的点区别

m_r=0;//圆的半径

}

(3)向视图类中添加自定义的成员函数原型:

public:

intComputeRadius(CPointcenp,CPointardp);

添加成员函数的程序代码:

intCMouseSpringView:

:

ComputeRadius(CPointcenp,CPointardp)

{

intdx=cenp.x-ardp.x;

intdy=cenp.y-ardp.y;

//sqrt()函数的调用,在头文件中加入#include"math.h"

return(int)sqrt(dx*dx+dy*dy);

}

(4)向视图类中添加两个鼠标消息响应函数,并输入鼠标处理程序代码。

具体操作方法与鼠标示例1方法相同。

一个是OnLButtonDown()函数,另一个是OnMouseMove()函数。

程序如下:

voidCMidPointCircleMouseView:

:

OnLButtonDown(UINTnFlags,CPointpoint)

{

//TODO:

Addyourmessagehandlercodehereand/orcalldefault

CDC*pDC=GetDC();

pDC->SelectStockObject(NULL_BRUSH);

if(!

m_ist)//绘制圆

{

m_bO=m_bR=point;//纪录第一次单击鼠标位置,定圆心

m_ist++;

}

else

{

m_bR=point;//记录第二次单击鼠标的位置,定圆周上的点

m_ist--;//为新绘图作准备

m_r=ComputeRadius(m_bO,m_bR);

MidpointCircle(pDC,m_bO.x,m_bO.y,m_r,RGB(255,0,0));

}

ReleaseDC(pDC);//释放设备环境

CView:

:

OnLButtonDown(nFlags,point);

}

voidCMidPointCircleMouseView:

:

OnMouseMove(UINTnFlags,CPointpoint)

{

//TODO:

Addyourmessagehandlercodehereand/orcalldefault

CDC*pDC=GetDC();

intnDrawmode=pDC->SetROP2(R2_NOT);//设置异或绘图模式,并保存原来绘图模式

pDC->SelectStockObject(NULL_BRUSH);

if(m_ist==1)

{

CPointprePnt,curPnt;

prePnt=m_bR;//获得鼠标所在的前一位置

curPnt=point;

//绘制橡皮筋线

m_r=ComputeRadius(m_bO,prePnt);

MidpointCircle(pDC,m_bO.x,m_bO.y,m_r,RGB(255,0,0));//用异或模式重复画圆,擦出所画的圆

//DrawCircle(pDC,m_bO,prePnt);

m_r=ComputeRadius(m_bO,curPnt);

MidpointCircle(pDC,m_bO.x,m_bO.y,m_r,RGB(255,0,0));//用当前位置作为圆周上的点画圆

m_bR=point;

}

pDC->SetROP2(nDrawmode);//恢复原绘图模式

ReleaseDC(pDC);//释放设备环境

CView:

:

OnMouseMove(nFlags,point);

}

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2