教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 基础教育 >

计算机图形学实验指导书(15)

来源:网络收集 时间:2026-08-08
导读: O(0,0) X { d+=2*x+3; x++; } (x0+R,y0) else { d+=2*(x-y)+5; x++; y--; } CirPot(x0,y0,x,y,color); } /* while*/ } /* MidpointCiecle */ int CirPot(int x0,int y0,int x,int y,int color) { Setpixel((x0+x),(
<0)

O(0,0) X {

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

(x0+R,y0)

else {

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

CirPot(x0,y0,x,y,color); } /* while*/

} /* MidpointCiecle */

int CirPot(int x0,int y0,int x,int y,int color) {

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));

-- 38

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

程序实现步骤:

(1) 建立MidPointCircle工程文件;

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

void MidpointCircle(CDC *pDC,int x0, int y0, int r, COLORREF color) int CirPot(CDC *pDC,int x0, int y0, int x, int y, COLORREF color) (3) 编写成员函数代码,程序如下:

void CMidPointCircleView::MidpointCircle(CDC *pDC,int x0, int y0, int r, COLORREF color) {

int x,y; float d;

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*/ }

int CMidPointCircleView::CirPot(CDC *pDC,int x0, int y0, int x, int y, COLORREF 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); 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); return 0; }

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

-- 39

CMidPointCircleDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here MidpointCircle(pDC,100, 100, 10, RGB(255,0,0)); MidpointCircle(pDC,500, 300, 60, RGB(255,255,0)); }

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

任务二:添加鼠标程序,实现交互式画圆

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

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

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

int m_r; // 半径

CPoint m_bO; // 圆心 CPoint m_bR; //圆上的点

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

(2)在视图类CPP文件的构造函数中初始化成员变量 CMidPointCircleMouseView::CMidPointCircleMouseView() { // TODO: add construction code here m_bO.x=0; m_bO.y=0; //圆心 m_bR.x=0; m_bR.y=0; //圆上的点 m_ist=0; //圆心与圆上的点区别 m_r=0; //圆的半径 }

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

-- 40

public:

int ComputeRadius(CPoint cenp,CPoint ardp);

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

int CMouseSpringView::ComputeRadius(CPoint cenp, CPoint ardp) {

int dx=cenp.x-ardp.x; int dy=cenp.y-ardp.y;

//sqrt()函数的调用,在头文件中加入#include \ return (int)sqrt(dx*dx+dy*dy); }

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

具体操作方法与鼠标示例1方法相同。一个是OnLButtonDown()函数,另一个是OnMouseMove()函数。程序如下:

void CMidPointCircleMouseView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default 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); }

void CMidPointCircleMouseView::OnMouseMove(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CDC *pDC=GetDC();

int nDrawmode=pDC->SetROP2(R2_NOT); //设置异或绘图模式,并保存原来绘图模式 pDC->SelectStockObject(NULL_BRUSH); if(m_ist==1)

-- 41

…… 此处隐藏:1193字,全部文档内容请下载后查看。喜欢就下载吧 ……
计算机图形学实验指导书(15).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/564065.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)