Using MFC to realize the midpoint circle algorithm in graphics course

Source: Internet
Author: User

1. Go to Resource View, new dialog, edit Modify caption or id attribute

Right-click the dialog box to add the class base class to CDialog

Named as: Mycircledialog

Using the Class Wizard, select the Mycircledialog class, select the member variable to add the variable value type int

M_radius setting variable value ranges



2. Add the menu, set the ID to Id_mycirclemenu and add the message map function through the Class wizard

Select the class name C***view

Find ID id_mycirclemenu, map OnCommand message


3. Add a variable and initialize it in the constructor


Public:int M_vradius;    int M_vcirclex; int M_vcircley;


M_vradius = 0;    M_vcirclex = 0; M_vcircley = 0;

Add Mycircledialog header File

#include "MyCircleDialog.h"

4. Adding code to the message handler to get input data with dialog interaction

Cmfccircledialogdemoview Message handler void Cmfccircledialogdemoview::onmycirclemenu () {///TODO:  Add Command handler code here Mycircledialog dlg; Defines a dialog box object if (dlg.   DoModal () = = IDOK)//Display dialog box {M_vradius = Dlg.m_radius;m_vcirclex = Dlg.m_circlex;m_vcircley = Dlg.m_circley; Receive and store edit box data invalidate (); Refresh View}}

Cmfctestview Drawing

5. Add drawing code in OnDraw (cancel cdc* parameter comment, use all device environment base class CDC paint,


can also define the use of other device environment classes such as CCLIENTDC, etc.)  

Void cmfccircledialogdemoview::ondraw (CDC*&NBSP;PDC) {cmfccircledialogdemodoc* pdoc =  GetDocument (); Assert_valid (PDOC);if  (!pdoc) return;// todo:  here to add drawing code for native data/* To determine the center of the circle, draw a Cartesian coordinate system */crect rect; GetWindowRect (rect); Int center_x = rect. Width ()/2;int center_y = rect. Height ()/2; cpen mypen; //Create Brush Mypen.createstockobject (black_pen);  //Set brush Properties Pdc->selectobject (&myPen);  //bind brush Handle Pdc->moveto (center_x,0);pD C->lineto (center_x, rect. Height ());pD C->moveto (0,center_y);pD C->lineto (rect. Width (), center_y);/********************* Draw Circle ****************///Logical Center coordinate/*int m_vradius = 50;int  m_vcirclex = -70; int m_vcircley = 100;*///into the center coordinate of the canvas center_x =  Center_x + m_vcirclex;center_y = center_y - m_vcircley;pdc->setpixel (center_x , Center_y+m_vradius,rgb (125,125,125));//Draw Center point Pdc->ellipse (center_x-3,center_y-3,center_x+3, center_y+3);  //draw a small circle to determine the center position int p=5/4-m_vradius;                                        //the initial value of the key value Int x=0;int y=m_ vradius;        //using the midpoint circle algorithm to draw a circle   while (x<y) {x++;if (p<0) {p= P+2*x+3;pdc->setpixel (Center_x+x,center_y+y,rgb (125,125,125));pD C->setpixel (Center_x+y,center_y+x,rgb ( 125,125,125));pD C->setpixel (Center_x+y,center_y-x,rgb (125,125,125));pD C->setpixel (Center_x+x,center_y-y, RGB (125,125,125));pD C->setpixel (Center_x-x,center_y-y,rgb (125,125,125));pD C->setpixel (center_x-y,center_ Y-x,rgb (125,125,125));pD C->setpixel (Center_x-y,center_y+x,rgb (125,125,125));pD C->setpixel (Center_x-x, Center_y+y,rgb (125,125,125))           //complete the drawing of the circle in a symmetrical way}else{ y--;p =p+2*x+3-2*y-2;pdc-> SetPixel (Center_x+x,center_y+y,rgb (125,125,125));pD C->setpixel (Center_x+y,center_y+x,rgb (125,125,125));pD C- >setpixel (Center_x+y,center_y-x,rgb (125,125,125));pD C->setpixel (Center_x+x,center_y-y,rgb (125,125,125)); Pdc->setpixel (Center_x-x,center_y-y,rgb (125,125,125));pD C->setpixel (Center_x-y,center_y-x,rgb ( 125,125,125));pD C->setpixel (Center_x-y,center_y+x,rgb (125,125,125));pD C->setpixel (Center_x-x,center_y+y, RGB (125,125,125));              //complete the drawing of the circle in a symmetrical way   }sleep (20);}}

This article is from "Whispering Autumn Wind" blog, please be sure to keep this source

Using MFC to realize the midpoint circle algorithm in graphics course

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.