Computer graphics experiment – using MFC dialog box to realize polygon drawing and filling (scan line filling algorithm) with source code

Content summary:

  • Utilizing dialog-based MFC projects
  • Implement mouse click to draw polygons
  • Implement scanline algorithm to fill polygons

For the source code, see Yushan-Ji/ComputerGraphics: ECNU2023 Fall Computer Graphics Course Experiment Code (github.com)

Experimental content

  1. Polygon input via mouse interaction
  2. Fill various polygons, including boundary self-intersections

Algorithm description

Polygon drawing

  1. Use the OnLButtonDown and OnRButtonDown functions to implement:
    • Left click: Start drawing the polygon and connect the previous vertex to the currently clicked vertex
    • Right-click: End drawing the polygon and connect the previous vertex to the first vertex
      Among them, the MoveTo and LineTo functions are used to draw the connection between vertices.
  2. In addition, in order to prevent the program from still responding to mouse click events after the polygon is drawn, it is necessary to add a Boolean variable IsCompleted to monitor whether the current polygon has been drawn
    Therefore, at the beginning of the OnLButtonDown and OnRButtonDown functions, a conditional judgment statement needs to be added. If IsCompleted is TRUE, It indicates that the polygon has been drawn and the function returns directly
  3. Save all vertex information in vector points to facilitate subsequent calculation of edge information

Scan line filling