Bottom layer Algorithm for drawing

Source: Internet
Author: User

I usually use APIs, CDC, and encapsulated internal functions. It's easy to draw lines and circles...

I recently learned about computer graphics and had to study the underlying algorithms internally...

I will post a few codes here to share with you!

(Only the OnDraw function of MFC is provided here)

I. Three algorithms for draw lines:

1. DDA (numerical differentiation) method:

Void CDDALineView: OnDraw (CDC * pDC)

{

CDDALineDoc * pDoc = GetDocument ();

ASSERT_VALID (pDoc );

// TODO: add draw code for native data here

Int x, x0 (200), y0 (200), x1 (500), 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 (, 0 ));

Y + = k;

}

}

2. midpoint draw Algorithm

Void CMidpointLineView: OnDraw (CDC * pDC)

{

CMidpointLineDoc * pDoc = GetDocument ();

ASSERT_VALID (pDoc );

// TODO: add draw code for native data here

Int a, B, d1, d2, d, x, y;

Int x0 (200), x1 (500), y0 (200), y1 (500 );

A = y0-y1;

B = x1-x0;

D = 2 * a + B;

D1 = 2 *;

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 data here

Int x0 (200), y0 (200), x1 (500), 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, 255 ));

X = x + 1;

E = e + 2 * dy;

If (e> = 0)

{

Y ++;

E = E-2 * dx;

}

}

}

Ii. MidPoint Circle Algorithm:

Void CMidPointCircleView: OnDraw (CDC * pDC)

{

CMidPointCircleDoc * pDoc = GetDocument ();

ASSERT_VALID (pDoc );

// TODO: add draw code for native data here

Int r = 100; // radius

Int m (300), n (250); // coordinates of the center

Int x, y;

Float d;

X = 0;

Y = 0 + r;

D = 1.25-r;

// MidPoint Circle Algorithm

PDC-> SetPixel (m + x, n + y, RGB (255, 0 ));

PDC-> SetPixel (m + y, n + x, RGB (255, 0 ));

PDC-> SetPixel (m-x, n + y, RGB (255, 0 ));

PDC-> SetPixel (m + y, n-x, RGB (255, 0 ));

PDC-> SetPixel (m + x, n-y, RGB (255, 0 ));

PDC-> SetPixel (m-y, n + x, RGB (255, 0 ));

PDC-> SetPixel (m-x, n-y, RGB (255, 0 ));

PDC-> SetPixel (m-y, n-x, RGB (255, 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 ));

PDC-> SetPixel (m + y, n + x, RGB (255, 0 ));

PDC-> SetPixel (m-x, n + y, RGB (255, 0 ));

PDC-> SetPixel (m + y, n-x, RGB (255, 0 ));

PDC-> SetPixel (m + x, n-y, RGB (255, 0 ));

PDC-> SetPixel (m-y, n + x, RGB (255, 0 ));

PDC-> SetPixel (m-x, n-y, RGB (255, 0 ));

PDC-> SetPixel (m-y, n-x, RGB (255, 0 ));

}

}

Related Article

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.