MFC computer Graphics (3) _ Computers

Source: Internet
Author: User

Before we can draw a line and a curve. But although the algorithm is easy to understand, but the complexity of the high, today introduced the more popular DDA line method, and the mouse to carry out a response to events, that is, on the artboard, the left mouse button click, move to another place to release, at this point between the two points to draw a line. DDA Drawing Line

Algorithm code

void Dda_line (int x1, int y1, int x2, int y2, int color)
    CDC *PDC = GetDC ();/Get device Environment
    int  k,i; float x, y , DX, dy;
    K = ABS (X2-X1);
    if (ABS (Y2-Y1) >k) 
        k = ABS (Y2-Y1);
    DX = float (x2-x1)/k;
    DY = float (y2-y1)/k;
    for (i=0;i<k;i++)
        pdc->setpixel (x, y, color);
        x = X+DX;
        y = y+dy;

A lot of people on the Internet to paint explanations, here I will not say.
The following is the handling of mouse events mouse events

We first add four member variable x1,y1,x2,y2 to represent the segment endpoint coordinates
After adding the following figure

In the menu bar
View (view) –> Mouse event –> Right class name Select Cxxxview (Note: XXX is your project name) –> in messages: Find Wm_lbuttondown Double-click or click First, then select Add on the right Function
Finally click Edit Code
As shown in figure

This is the mouse click Response Event function, and we edit the code:

void OnLButtonDown (UINT nflags, CPoint point) 
    //todo:add your message handler code here and/or call Default
  cview::onlbuttondown (nflags, point);
    x1 = point.x;
    y1 = Point.y;

Follow the steps above to continue adding the left mouse button to release the response event.
At the same time you can draw line segments.

void OnLButtonUp (UINT nflags, CPoint point) 
    //todo:add your message handler code here and/or call default

    Cview::onlbuttonup (nflags, point);
    x2 = point.x;
    y2 = point.y;

    Dda_line (X1,y1,x2,y2,rgb (255,0,0));

The effect is as follows:

Below, we draw a rectangle with a diagonal line that is drawn.
This is also very simple, you draw a map, you can write code

Dda_line (X1,y1,x2,y1,rgb (0,255,0));
Dda_line (X1,y1,x1,y2,rgb (0,255,0));
Dda_line (X1,y2,x2,y2,rgb (0,255,0));
Dda_line (X2,y1,x2,y2,rgb (0,255,0));

The effect is as follows

Finish the call ...

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: and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: