MFC Drawing Small Experiment (2)

Source: Internet
Author: User
Tags value of pi

1, based on the 5 vertices of the positive Pentagon, the point storage constitutes a pentagram. The fill mode uses winding. The pentagram boundary is a solid blue line of 5 pixels wide, with red padding inside.
CRect rect;//Defining rectanglesGetClientRect (&rect);//get the customer area rectanglePdc->setmapmode (Mm_anisotropic);//Set mapping modePdc->setwindowext (rect. Width (), Rect. Height ());//Setup windowPdc->setviewportext (rect. Width (),-rect. Height ());//set the viewport: The x-axis is positive horizontally to the right and the y-axis is positive verticallyPdc->setviewportorg (rect. Width ()/2, Rect. Height ()/2);//Set the Customer Area Center as the coordinate system OriginRect. Offsetrect (-rect. Width ()/2,-rect. Height ()/2);//Customer Area Rectangle correctionCPen penblue (Ps_solid,5, RGB (0,0,255));//defines a 5-pixel-wide blue brushCPen *poldpen=pdc->selectobject (&penblue); CBrush brushred (RGB (255,0,0));//define a red paint brushCBrush *poldbrush=pdc->selectobject (&brushred); PDC->setpolyfillmode (winding);//Set Fill mode    intR= $;//positive Pentagon circumscribed circle radiusCPoint p[5];//defining a positive pentagon vertex array    DoubleBeta=2*pi/5;//define central angle β for each vertex    Doublealpha=pi/Ten;//defining the initial angle α     for(intI=0;i<5; i++) {p[i].x=round (R*cos (i*beta+Alpha)); P[i].y=round (R*sin (i*beta+Alpha)); } CPoint v[5]; v[0]=p[0];v[1]=p[2];v[2]=p[4];v[3]=p[1];v[4]=p[3];//dump verticesPdc->polygon (V,5);//Draw PentagramPdc->selectobject (Poldpen);//Restore BrushesPdc->selectobject (Poldbrush);//Restore Brush

Note: In this routine, the trigonometric functions are used to include the math header file, and the value of Pi pi is defined as PI; because Pentagon's vertex array is calculated as floating-point data, it needs to be rounded when stored as a cpoint type. The following compilation preprocessing statements have been added to this file header:

#include <math.h>#define PI 3.1415926#define Round (d) Int (floor (d+0.5))

2, use the line function to draw P0 ( -160,20), P1 (-240,100), P2 ( -280,0), P3 ( -240,-100), P4 ( -180,-40), P5 ( -140,-100), P6 ( -60,40) left polygon, Shift the horizontal right by 360 pixels to draw the same shape polygon, fill the left polygon with the FillPath () function, and fill the right polygon with the Strokeandfillpath () function. The polygon boundary color remains the default black and the inner fill is red.
CRect rect;//Defining rectanglesGetClientRect (&rect);//get the customer area rectanglePdc->setmapmode (Mm_anisotropic);//Set mapping modePdc->setwindowext (rect. Width (), Rect. Height ());//Setup windowPdc->setviewportext (rect. Width (),-rect. Height ());//set the viewport: The x-axis is positive horizontally to the right and the y-axis is positive verticallyPdc->setviewportorg (rect. Width ()/2, Rect. Height ()/2);//Set the Customer Area Center as the coordinate system OriginRect. Offsetrect (-rect. Width ()/2,-rect. Height ()/2);//Customer Area Rectangle correctionCPoint p[7];//Defining vertex Arraysp[0]=cpoint (- the, -);p [1]=cpoint (- -, -); p[2]=cpoint (-280,0);p [3]=cpoint (- -,- -); p[4]=cpoint (- the,- +);p [5]=cpoint (- $,- -);p [6]=cpoint (- -, +);    CBrush Newbrush; Newbrush.createsolidbrush (RGB (255,0,0)); CBrush*poldbrush=pdc->selectobject (&Newbrush); PDC-Beginpath (); PDC->moveto (p[0]);//Draw left Polygon     for(intI=1;i<7; i++) PDC-LineTo (P[i]); PDC->lineto (p[0]); PDC-Endpath (); PDC-FillPath (); p[0]=cpoint ( $, -);p [1]=cpoint ( -, -); p[2]=cpoint ( the,0);p [3]=cpoint ( -,- -); p[4]=cpoint ( the,- +);p [5]=cpoint ( -,- -);p [6]=cpoint ( -, +); PDC-Beginpath (); PDC->moveto (p[0]);  for(i=1;i<7; i++)//Draw Right PolygonPdc->LineTo (P[i]); PDC->lineto (p[0]); PDC-Endpath (); PDC->strokeandfillpath ();//Strokeandfillpath Fill Path layerPdc->SelectObject (Poldbrush); Newbrush.deleteobject ();

3, given 7 control points p0 ( -350,-100), p1 (-250,100), p2 (0,130), P3 (50,-50), P5 (350,-20), P6 (250,130). Use a black brush to draw a control polygon, and a blue brush to draw a two-piece Bezier spline. The two-stage Bezier spline is required to smooth the connection, that is to say P4 control points and P2, P3 control points collinear. Set the x-coordinate of the P4 point to 90, calculate the y-coordinate of the P4 point according to the linear equation and draw a smooth-stitched two-segment Bezier spline.
CRect rect;//Defining rectanglesGetClientRect (&rect);//get the customer area rectanglePdc->setmapmode (Mm_anisotropic);//Set mapping modePdc->setwindowext (rect. Width (), Rect. Height ());//Setup windowPdc->setviewportext (rect. Width (),-rect. Height ());//set the viewport: The x-axis is positive horizontally to the right and the y-axis is positive verticallyPdc->setviewportorg (rect. Width ()/2, Rect. Height ()/2);//Set the Customer Area Center as the coordinate system OriginRect. Offsetrect (-rect. Width ()/2,-rect. Height ()/2);//Customer Area Rectangle correctionCPoint p[7]; p[0]=cpoint (- -,- -);p [1]=cpoint (- -, -); p[2]=cpoint (0, the);p [3]=cpoint ( -,- -); Doublek= (p[3].y-p[2].Y)/(p[3].x-p[2].x); Doublex= -, y=k* (x-p[3].x) +p[3].y; p[4]=cpoint (Round (x), Round (y));p [5]=cpoint ( -,- -);p [6]=cpoint ( -, the);  for(intI=0;i<7; i++)    {        if(0==i) PDC-MoveTo (P[i]); ElsePDC-LineTo (P[i]); PDC->ellipse (p[i].x-5, p[i].y-5, p[i].x+5, p[i].y+5);//Black Solid Circle Draw control Point} CPen Newpen,*Poldpen; Newpen.createpen (Ps_solid,1, RGB (0,0,255));//Blue Brush Draw splinePoldpen=pdc->selectobject (&Newpen); PDC->polybezier (P,7);//To draw a Bezier splinePdc->selectobject (Poldpen);

4, draw two fan shapes to form the fan, and fill the inside of the fan with a bit drawing brush in the resource file.

In the resource View artboard, select test resources, right-click, and in the popup menu, select Introduce ...,

Note: The picture suffix name must be. bmp

The effect is as follows:

CRect rect;//Defining rectanglesGetClientRect (&rect);//get the customer area rectanglePdc->setmapmode (Mm_anisotropic);//Set mapping modePdc->setwindowext (rect. Width (), Rect. Height ());//Setup windowPdc->setviewportext (rect. Width (),-rect. Height ());//set the viewport: The x-axis is positive horizontally to the right and the y-axis is positive verticallyPdc->setviewportorg (rect. Width ()/2, Rect. Height ()/2);//Set the Customer Area Center as the coordinate system OriginRect. Offsetrect (-rect. Width ()/2,-rect. Height ()/2);//Customer Area Rectangle correctionCBitmap Newbitmap;    Newbitmap.loadbitmap (IDB_BITMAP1); CBrush Newbrush,*Poldbrush; Newbrush.createpatternbrush (&AMP;NEWBITMAP);//new pattern brush, picture as pattern brushesPoldbrush=pdc->selectobject (&Newbrush); CPen*Poldpen; Poldpen= (cpen*) pdc->Selectstockobject (Null_pen);    CPoint Ld,rt,sp,eq; LD=cpoint (- -,- -), Rt=cpoint ( -, $);//lower left corner of the bounding rectangle dot ld, upper right corner RTSp=cpoint ( -,0), Eq=cpoint (- -,0);//the beginning of the elliptical arc SP and endpoint eqPdc->Pie (CRect (LD,RT), sp,eq); PDC-SelectObject (Poldbrush);    Newbitmap.deleteobject (); LD=cpoint (- the,-280), Rt=cpoint ( the,- -); SP=cpoint ( -,0), Eq=cpoint (- -,0); PDC->pie (CRect (LD,RT), sp,eq);//fill with the default paint brushPdc->selectobject (Poldpen);

MFC Drawing Small Experiment (2)

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: info-contact@alibabacloud.com 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.