C + + Classic Drawing tool Easyx

Source: Internet
Author: User
Tags ellipse drawing

Easyx Introduction
Easyx

In learning C, many students complained that C can only write the simplest demo program, and print characters on the screen by printf to verify the code. This kind of programming is very dull, does not feel oneself in the design software.

Easyx is a third-party graphics library for C + +, through which we are able to draw pictures of the various colors we like on the screen. With it, I write a fun little game is not a dream oh.

There are several requirements for using Easyx:

    • Can only be used under Windows
    • We recommend that you use Visual Studio as the IDE
    • C + + code must be written (file suffix called cpp)
Easyx Installation Download Path

Download

Installation

Unzip the download directory easyx_20151015 (Beta). zip as follows:

Double-click the setup.hta file

Find the desired VS version and click Install.

PS: in the extracted file, there is a easyx_help.chm file, it is the Easyx document, all the API is introduced inside.

As a software developer, reading the documentation is the most basic skill, and I hope you will start to develop your ability from now on. If you read through this document, believe that you can complete your own on the screen to draw the pattern you want.

Easyx Basic API

In Easyx's documentation, there is a section on "super-Simple use preview", which contains a piece of code.

#include <iostream>#include <graphics.h>#include <conio.h>void main(){ initgraph(640, 480); // 创建图形界面 circle(200, 200, 100); // 画圆,圆心(200, 200),半径 100 getch(); // 按任意键继续 closegraph(); // 关闭图形界面}

The result of this code execution is as follows:

This demo program realizes the function of drawing a circle on the screen.

1. Creation and destruction of canvas

In the main function, the first and last lines of code are the creation and deletion of graphical interfaces, which are often called "canvases".

    • Initgraph ()

When you create a canvas, you need to enter a length-width value for the target window.

    • Closegraph ()

As in C, C + + also emphasizes the symmetry of code, and the code that requests resources and frees resources is always paired.

2. Coordinate system

In Easyx, coordinates are divided into: logical coordinates and physical coordinates.

We only introduce logical coordinates here.

Logical coordinates are the coordinate systems used in the program for plotting.

The default origin of the coordinates is in the upper-left corner of the screen, the X-axis is positive on the right, the Y-axis is positive, and the unit of measure is pixels.

The origin, direction, and units of the coordinates can be modified by a specific function. We generally only use the default settings.

3. Round Drawing

Circle This function is used to draw circles.

void circle(    int x, int y, int radius);
Parameters:
    • X
      The x-coordinate of the circle's center.
    • Y
      The y-coordinate of the circle's center.
    • Radius
      The radius of the circle.

This function is used in the demo program to draw a circle with a circle centered at (200, 200) and a radius of 100.

4. Line drawing

This function is used to draw lines. You can also draw lines with Linerel and LineTo.

void line(    int x1, int y1, int x2, int y2);
Parameters
    • X1
      The x-coordinate of the starting point of the line.

    • Y1
      The y-coordinate of the starting point of the line.

    • X2
      The x-coordinate of the end point of the line.

    • Y2
      The y-coordinate of the end point of the line.

5. Rectangle Drawing

This function is used to draw a hollow rectangle.

void rectangle(    int left, int top, int right, int bottom);
Parameters
    • Left
      The x-coordinate of the left side of the rectangle.

    • Top
      The y-coordinate of the upper rectangle.

    • Right
      The x-coordinate of the right side of the rectangle.

    • Bottom
      The y-coordinate of the lower rectangle.

6. Ellipse drawing

This function is used to draw ellipses.

void ellipse(    int left, int top, int right, int bottom);
Parameters
      • Left
        The x-coordinate of the upper-left corner of the ellipse's bounding rectangle.

      • Top
        The y-coordinate of the upper-left corner of the ellipse's bounding rectangle.

      • Righ The x-coordinate of the lower-right corner of an elliptical bounding rectangle.

Bottom
The y-coordinate of the lower-right corner of the ellipse's bounding rectangle.

The above is a few basic graphics drawing methods, they have a feature is the drawing is a hollow shape. Corresponding to this is a set of interfaces that draw a graphic with a fill color.

fillcircle(), fillellipse(), fillrectangle()

Their parameters are exactly the same as the hollow interface, except that they need to call the following interface to set the fill color before use.

setfillcolor()

Let's take a look at what these functions can do.

Easyx Combat

The picture above is implemented with Easyx. Haha, looks a bit ugly, but if you can write this function independently, you have basically mastered the use of Easyx.

Provides a code framework:

#include <iostream>#include <graphics.h>void main(){ initgraph(800, 600); // Add your code closegraph(); }

Here please think for yourself, do not hurry down to see.

(... 15 minutes passed. )

Analysis

This image is mainly composed of round, oval, rectangular and straight lines, these shapes of API we have already introduced. We just have to draw them on the screen in order.

Code implementation

Enter the following code in the C + + project's main.cpp file.

#include <iostream> #include <graphics.h> #include <conio.h>void main () {initgraph (800,600);Record the current fill color colorref Save_color = Getfillcolor ();Draw a rectangular box rectangle (50,50,550,550);Draw a hollow circle circle (200,150,50); Circle400,150,50);Draw Hollow Oval Ellipse (100,100,500,300);//fills the green part of the Ellipse setfillcolor (green); FloodFill (300, 200, Getlinecolor ()); //filled with a white oval Setfillcolor, FillEllipse (200, 210, 400, 290); //fills a green rectangle Setfillcolor, FillRectangle (150, 300, 450, 500); //draw a straight line (300, 500, 300, 550); //Press any key to continue getch (); //Restore Fill Color setfillcolor (save_color); Closegraph ();}       

OK, click CTRL + F5, do you see the effect?

For most people, the irregular green area inside the ellipse should be the hardest to achieve, and FloodFill () is a function that helps us to paint all the fill color out of the enclosing area of a point. In addition to setting the point coordinates, enter the color of this enclosing area border.

The point here is that, after the canvas is initialized, we get the current fill color, and after the drawing is done, the current fill color is restored to the state before our program executes.

This habit is very important, is to let our program after running, the system environment has not changed. Otherwise in the future in charge of the project, it is likely because of a module to change the system settings to create a lot of unnecessary bugs.

Well, is it more of a sense of accomplishment now?

Since the use of EASYX will be in our series of projects, so there is not too much to introduce, the later use of new content we say.


You are welcome to join the Learning Exchange Group if you encounter any problems or want to acquire learning resources in the learning process.
639368839, we learn c/c++! together.

C + + Classic Drawing tool Easyx

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.