在MFC++6.0中用鼠标选择圆心实现椭圆的扫描转换的代码

时间: 2023-06-19 09:08:27 浏览: 50
以下是一个简单的例子,演示如何使用鼠标选择椭圆的中心,并实现椭圆的扫描转换。 首先,创建一个MFC项目,新建一个类名为CEllipseScan的类,并将其继承自CView。 CEllipseScan类的头文件中包含以下代码: ```cpp class CEllipseScan : public CView { protected: // create from serialization only CEllipseScan(); DECLARE_DYNCREATE(CEllipseScan) // Attributes public: CEllipseScanDoc* GetDocument(); // Operations public: // Overrides public: virtual void OnDraw(CDC* pDC); // overridden to draw this view virtual BOOL PreCreateWindow(CREATESTRUCT& cs); protected: virtual BOOL OnPreparePrinting(CPrintInfo* pInfo); virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo); virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo); // Implementation public: virtual ~CEllipseScan(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif protected: CPoint m_center; // 椭圆中心坐标 BOOL m_bDragging; // 是否正在拖动鼠标 int m_radiusX, m_radiusY; // 椭圆的半径 BOOL m_bDrawEllipse; // 是否绘制椭圆 void DrawEllipse(CDC* pDC, int x0, int y0, int a, int b); // 绘制椭圆 void ScanConvertEllipse(CDC* pDC, int x0, int y0, int a, int b); // 扫描转换椭圆 void ScanConvertLine(CDC* pDC, int x0, int y0, int x1, int y1); // 扫描转换直线 void DrawPixel(CDC* pDC, int x, int y, COLORREF color); // 绘制像素点 DECLARE_MESSAGE_MAP() public: afx_msg void OnLButtonDown(UINT nFlags, CPoint point); afx_msg void OnLButtonUp(UINT nFlags, CPoint point); afx_msg void OnMouseMove(UINT nFlags, CPoint point); }; ``` CEllipseScan类的实现文件中包含以下代码: ```cpp // CEllipseScan.cpp : implementation file // #include "stdafx.h" #include "EllipseScan.h" #include "EllipseScanDoc.h" #include "EllipseScanView.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CEllipseScan IMPLEMENT_DYNCREATE(CEllipseScan, CView) BEGIN_MESSAGE_MAP(CEllipseScan, CView) //{{AFX_MSG_MAP(CEllipseScan) ON_WM_LBUTTONDOWN() ON_WM_LBUTTONUP() ON_WM_MOUSEMOVE() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CEllipseScan construction/destruction CEllipseScan::CEllipseScan() { m_bDragging = FALSE; m_bDrawEllipse = FALSE; } CEllipseScan::~CEllipseScan() { } BOOL CEllipseScan::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CView::PreCreateWindow(cs); } ///////////////////////////////////////////////////////////////////////////// // CEllipseScan drawing void CEllipseScan::OnDraw(CDC* pDC) { CEllipseScanDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here if (m_bDrawEllipse) { DrawEllipse(pDC, m_center.x, m_center.y, m_radiusX, m_radiusY); } } ///////////////////////////////////////////////////////////////////////////// // CEllipseScan printing BOOL CEllipseScan::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CEllipseScan::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CEllipseScan::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } ///////////////////////////////////////////////////////////////////////////// // CEllipseScan diagnostics #ifdef _DEBUG void CEllipseScan::AssertValid() const { CView::AssertValid(); } void CEllipseScan::Dump(CDumpContext& dc) const { CView::Dump(dc); } CEllipseScanDoc* CEllipseScan::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEllipseScanDoc))); return (CEllipseScanDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CEllipseScan message handlers void CEllipseScan::OnLButtonDown(UINT nFlags, CPoint point) { m_bDragging = TRUE; m_center = point; CClientDC dc(this); dc.SetROP2(R2_NOTXORPEN); dc.Ellipse(m_center.x - 2, m_center.y - 2, m_center.x + 2, m_center.y + 2); CView::OnLButtonDown(nFlags, point); } void CEllipseScan::OnLButtonUp(UINT nFlags, CPoint point) { if (m_bDragging) { m_bDragging = FALSE; CClientDC dc(this); dc.SetROP2(R2_NOTXORPEN); dc.Ellipse(m_center.x - 2, m_center.y - 2, m_center.x + 2, m_center.y + 2); m_radiusX = abs(point.x - m_center.x); m_radiusY = abs(point.y - m_center.y); m_bDrawEllipse = TRUE; Invalidate(); } CView::OnLButtonUp(nFlags, point); } void CEllipseScan::OnMouseMove(UINT nFlags, CPoint point) { if (m_bDragging) { CClientDC dc(this); dc.SetROP2(R2_NOTXORPEN); dc.Ellipse(m_center.x - 2, m_center.y - 2, m_center.x + 2, m_center.y + 2); m_radiusX = abs(point.x - m_center.x); m_radiusY = abs(point.y - m_center.y); dc.Ellipse(m_center.x - m_radiusX, m_center.y - m_radiusY, m_center.x + m_radiusX, m_center.y + m_radiusY); } CView::OnMouseMove(nFlags, point); } void CEllipseScan::DrawEllipse(CDC* pDC, int x0, int y0, int a, int b) { int x, y, d; x = 0; y = b; d = 4 * b * b + a * a * (1 - 4 * b); ScanConvertLine(pDC, x0 + x, y0 + y, x0 + x, y0 - y); ScanConvertLine(pDC, x0 - x, y0 + y, x0 - x, y0 - y); ScanConvertLine(pDC, x0 + x, y0 + y, x0 - x, y0 + y); ScanConvertLine(pDC, x0 + x, y0 - y, x0 - x, y0 - y); while (b * b * (x + 1) < a * a * (y - 0.5)) { if (d < 0) { d += 4 * b * b * (2 * x + 3); } else { d += 4 * b * b * (2 * x + 3) + 4 * a * a * (-2 * y + 2); y--; } x++; ScanConvertLine(pDC, x0 + x, y0 + y, x0 + x, y0 - y); ScanConvertLine(pDC, x0 - x, y0 + y, x0 - x, y0 - y); ScanConvertLine(pDC, x0 + x, y0 + y, x0 - x, y0 + y); ScanConvertLine(pDC, x0 + x, y0 - y, x0 - x, y0 - y); } d = b * b * (2 * x + 1) * (2 * x + 1) + 4 * a * a * (y - 1) * (y - 1) - 4 * a * a * b * b; while (y > 0) { if (d < 0) { d += 4 * b * b * (2 * x + 2) + 4 * a * a * (-2 * y + 3); x++; } else { d += 4 * a * a * (-2 * y + 3); } y--; ScanConvertLine(pDC, x0 + x, y0 + y, x0 + x, y0 - y); ScanConvertLine(pDC, x0 - x, y0 + y, x0 - x, y0 - y); ScanConvertLine(pDC, x0 + x, y0 + y, x0 - x, y0 + y); ScanConvertLine(pDC, x0 + x, y0 - y, x0 - x, y0 - y); } } void CEllipseScan::ScanConvertEllipse(CDC* pDC, int x0, int y0, int a, int b) { int x, y, d; x = 0; y = b; d = 4 * b * b + a * a * (1 - 4 * b); DrawPixel(pDC, x0 + x, y0 + y, RGB(255, 0, 0)); DrawPixel(pDC, x0 + x, y0 - y, RGB(255, 0, 0)); DrawPixel(pDC, x0 - x, y0 + y, RGB(255, 0, 0)); DrawPixel(pDC, x0 - x, y0 - y, RGB(255, 0, 0)); while (b * b * (x + 1) < a * a * (y - 0.5)) { if (d < 0) { d += 4 * b * b * (2 * x + 3); } else { d += 4 * b * b * (2 * x + 3) + 4 * a * a * (-2 * y + 2); y--; } x++; DrawPixel(pDC, x0 + x, y0 + y, RGB(255, 0, 0)); DrawPixel(pDC, x0 + x, y0 - y, RGB(255, 0, 0)); DrawPixel(pDC, x0 - x, y0 + y, RGB(255, 0, 0)); DrawPixel(pDC, x0 - x, y0 - y, RGB(255, 0, 0)); } d = b * b * (2 * x + 1) * (2 * x + 1) + 4 * a * a * (y - 1) * (y - 1) - 4 * a * a * b * b; while (y > 0) { if (d < 0) { d += 4 * b * b * (2 * x + 2) + 4 * a * a * (-2 * y + 3); x++; } else { d += 4 * a * a * (-2 * y + 3); } y--; DrawPixel(pDC, x0 + x, y0 + y, RGB(255, 0, 0)); DrawPixel(pDC, x0 + x, y0 - y, RGB(255, 0, 0)); DrawPixel(pDC, x0 - x, y0 + y, RGB(255, 0, 0)); DrawPixel(pDC, x0 - x, y0 - y, RGB(255, 0, 0)); } } void CEllipseScan::ScanConvertLine(CDC* pDC, int x0, int y0, int x1, int y1) { int dx = abs(x1 - x0); int dy = abs(y1 - y0); int sx = (x0 < x1 ? 1 : -1); int sy = (y0 < y1 ? 1 : -1); int err = dx - dy; while (TRUE) { DrawPixel(pDC, x0, y0, RGB(255, 0, 0)); if (x0 == x1 && y0 == y1) { break; } int e2 = 2 * err; if (e2 > -dy) { err -= dy; x0 += sx; } if (e2 < dx) { err += dx; y0 += sy; } } } void CEllipseScan::DrawPixel(CDC* pDC, int x, int y, COLORREF color) { pDC->SetPixel(x, y, color); } ``` 现在,运行该应用程序,单击并拖动鼠标,选择椭圆的中心和半径。完成选择后,椭圆将被绘制,并使用扫描转换算法将其转换为像素。

相关推荐

最新推荐

recommend-type

VS+MFC+双摄像头的打开

VS+MFC+双摄像头的打开 VS+MFC+双摄像头的打开 VS+MFC+双摄像头的打开
recommend-type

MFC实现漂亮界面之美化按钮

主要为大家详细介绍了MFC实现漂亮界面之美化按钮,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

vs2010+MFC+OpenCV实现Canny边缘检测(文档)

用VS2010环境,创建MFC工程,使用OpenCV库函数,实现Canny边缘检测
recommend-type

MFC实现学生选课系统

主要为大家详细介绍了MFC实现学生选课系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】增量式PID的simulink仿真实现

# 2.1 Simulink仿真环境简介 Simulink是MATLAB中用于建模、仿真和分析动态系统的图形化环境。它提供了一个直观的用户界面,允许用户使用块和连接线来创建系统模型。Simulink模型由以下元素组成: - **子系统:**将复杂系统分解成更小的、可管理的模块。 - **块:**代表系统中的组件,如传感器、执行器和控制器。 - **连接线:**表示信号在块之间的流动。 Simulink仿真环境提供了广泛的块库,涵盖了各种工程学科,包括控制系统、电子和机械工程。它还支持用户自定义块的创建,以满足特定仿真需求。 # 2. Simulink仿真环境的搭建和建模 ### 2.
recommend-type

训练集和测试集的准确率都99%,但是预测效果不好

即使训练集和测试集的准确率都很高,但是在实际的预测中表现不佳,可能会有以下几个原因: 1. 数据质量问题:模型训练和测试的数据可能存在问题,比如数据标签错误、数据集采样不均衡、数据集中存在异常值等问题,这些问题可能会导致模型在实际预测中表现不佳。 2. 特征工程问题:模型的特征工程可能存在问题,导致模型无法很好地捕捉数据的特征,从而影响预测效果。 3. 模型选择问题:模型的选择可能不合适,比如选择的模型太简单,无法很好地拟合数据,或者选择的模型太复杂,导致过拟合等问题。 4. 超参数调整问题:模型的超参数可能没有调整到最佳状态,需要进行调整。 针对以上可能的原因,可以采取相应的措施进
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依