About the drawing bottom algorithm

Source: Internet
Author: User

At ordinary times, used to the API, CDC and encapsulated internal functions, about drawing lines, draw a circle these things are at your fingertips ...

Recently learning computer graphics, have to go deep inside the bottom of the algorithm ...

Put a few code here to share!

(Only the MFC OnDraw function is given here)

First, draw the line of three algorithms:

1. DDA (Numerical differential) method:

void Cddalineview::ondraw (cdc* pDC)

{

cddalinedoc* PDoc = GetDocument ();

Assert_valid (PDOC);

Todo:add Draw code for native

int x,x0 (y0), X1 (+), y1 (500);

float dx,dy,y,k;

dx=x1-x0;

Dy=y1-y0;

K=DY/DX;

Y=y0;

for (x=x0;x<=x1;x++)

{

Pdc->setpixel (x, (int) (y+0.5), RGB (255,0,0));

Y+=k;

}

}

2. Midpoint Drawing line algorithm

void Cmidpointlineview::ondraw (cdc* pDC)

{

cmidpointlinedoc* PDoc = GetDocument ();

Assert_valid (PDOC);

Todo:add Draw code for native

int a,b,d1,d2,d,x,y;

int x0 (x1), y0 (500), y1;

A=y0-y1;

b=x1-x0;

D=2*a+b;

D1=2*a;

d2=2* (A+B);

x=x0;

Y=y0;

Pdc->setpixel (X,y,rgb (0,255,0));

while (X<X1)

{

if (d<0)

{

x + +;

y++;

D+=D2;

}

Else

{

x + +;

D+=D1;

}

Pdc->setpixel (X,y,rgb (0,255,0));

}

}

3. Bresenham algorithm:

void Cbresenhamline2view::ondraw (cdc* pDC)

{

cbresenhamline2doc* PDoc = GetDocument ();

Assert_valid (PDOC);

Todo:add Draw code for native

int x0 (y0), X1 (+), y1 (500);

int x,y,dx,dy;

dx=x1-x0;

Dy=y1-y0;

int e=-dx;

x=x0;

Y=y0;

for (int i=0;i<=dx;i++)

{

Pdc->setpixel (X,y,rgb (0,0,255));

x=x+1;

E=e+2*dy;

if (e>=0)

{

y++;

E=E-2*DX;

}

}

}

Second, the Midpoint Draw circle algorithm:

void Cmidpointcircleview::ondraw (cdc* pDC)

{

cmidpointcircledoc* PDoc = GetDocument ();

Assert_valid (PDOC);

Todo:add Draw code for native

int r=100; Radius

int m (+), n (250);//Center coordinates

int x,y;

float D;

x=0;

Y=0+r;

D=1.25-r;

Algorithm of midpoint drawing circle

Pdc->setpixel (M+x,n+y,rgb (255,0,0));

Pdc->setpixel (M+y,n+x,rgb (255,0,0));

Pdc->setpixel (M-x,n+y,rgb (255,0,0));

Pdc->setpixel (M+y,n-x,rgb (255,0,0));

Pdc->setpixel (M+x,n-y,rgb (255,0,0));

Pdc->setpixel (M-y,n+x,rgb (255,0,0));

Pdc->setpixel (M-x,n-y,rgb (255,0,0));

Pdc->setpixel (M-y,n-x,rgb (255,0,0));

while (x<=y)

{

if (d<0)

d+=2*x+3;

Else

{

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

y--;

}

x + +;

Pdc->setpixel (M+x,n+y,rgb (255,0,0));

Pdc->setpixel (M+y,n+x,rgb (255,0,0));

Pdc->setpixel (M-x,n+y,rgb (255,0,0));

Pdc->setpixel (M+y,n-x,rgb (255,0,0));

Pdc->setpixel (M+x,n-y,rgb (255,0,0));

Pdc->setpixel (M-y,n+x,rgb (255,0,0));

Pdc->setpixel (M-x,n-y,rgb (255,0,0));

Pdc->setpixel (M-y,n-x,rgb (255,0,0));

}

}

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.