04.基础图形的绘制

  • 学习人数 15K+
  • 适合有C语言基础人群学习
avatar
林耿亮

你好编程主讲老师

这一节中,我们讨论文档中的图形绘制相关函数。

图形绘制相关函数

本节中的所有代码窗体大小均为800 * 600。坐标系原点在窗体中心,X轴正方向向右,Y轴正方向向上。

1.点

putpixel

这个函数用于画点。

void putpixel(
    int x,
    int y,
    COLORREF color
);
参数 类型 意义
x int 点的 x 坐标
y int 点的 y 坐标
color COLORREF 点的颜色

返回值 无

putpixel函数的前两个参数为点的坐标,第三个参数为点的颜色。目前,我们并不想展开讨论怎样选取颜色,大家可以先使用EasyX中符号常量中的颜色。

符号常量 颜色
BLACK
BLUE
GREEN 绿
CYAN
RED
MAGENTA
BROWN
LIGHTGRAY 浅灰
DARKGRAY 深灰
LIGHTBLUE 亮蓝
LIGHTGREEN 亮绿
LIGHTCYAN 亮青
LIGHTRED 亮红
LIGHTMAGENTA 亮紫
YELLOW
WHITE

例如,在原点处画一个红色的点,第三个参数可以填上表中的RED

putpixel(0, 0 , RED);

现在,我们在下图的坐标处,分别画上几个颜色不一样的点。

坐标上的点

#include <easyx.h>
#include <stdio.h>
int main()
{
    initgraph(800, 600);
    setorigin(400, 300);
    setaspectratio(1, -1);

    //  画点
    putpixel(0, 0, RED);            //  (0, 0) 红色
    putpixel(200, 200, YELLOW);     //  (200, 200) 黄色
    putpixel(200, -200, CYAN);      //  (200, -200) 青色
    putpixel(-200, -200, GREEN);    //  (-200, -200) 绿色
    putpixel(-200, 200, WHITE);     //  (-200, 200) 白色


    getchar();
    closegraph();
    return 0;
}

五个点

这些点仅占一个像素,所以很难看清。我们不如在平面内随机画点。为了不让点跑到平面外,那么,x、y的取值范围如下:

-400 <= x <= 400 -300 <= y <= 300

写成C语言代码如下:

x = rand() % (800 + 1) - 400;
y = rand() % (600 + 1) - 300;

表达式rand() % (800 + 1)的值最小为0, 最大为800。其后,减去400后,区间为[-400,400]。 表达式rand() % (600 + 1)的值最小为0, 最大为600。其后,减去300后,区间为[-300,300]。

随机在屏幕中绘制1000个点。

#include <easyx.h>
#include <stdio.h>
int main()
{
    initgraph(800, 600);
    setorigin(400, 300);
    setaspectratio(1, -1);

    int x, y;
    //  随机画点
    for (int i = 0; i < 1000; i++)
    {
        x = rand() % (800 + 1) - 400;
        y = rand() % (600 + 1) - 300;
        putpixel(x, y, WHITE);
    }

    getchar();
    closegraph();
    return 0;
}

星星点点

2.线

line

这个函数用于画直线。

void line(int x1, int y1, int x2, int y2);
参数 类型 意义
x1 int 直线的起始点的 x 坐标
y1 int 直线的起始点的 y 坐标
x2 int 直线的终止点的 x 坐标
y2 int 直线的终止点的 y 坐标

返回值 无

我们使用line函数来画两条直线。

  1. 从点(-200, 200)到(200, -200)
  2. 从点(-200, -200)到(200, 200)

画线

代码如下:

line(-200, 200, 200, -200);
line(-200, -200, 200, 200);

画两条线

3.圆

circle

这个函数用于画圆。

void circle(
    int x,
    int y,
    int radius
);
参数 类型 意义
x int 圆心 x 坐标
y int 圆心 y 坐标
radius int 半径

返回值 无

让我们用circie函数在原点(0 , 0)处,画一个半径为200的圆形。

circle(0, 0, 200);

画圆

接着多画几个圆,用circie函数在原点(0 , 0)处,从半径10开始,绘制同心圆。半径每次递增10,直到半径大于300为止。

#include <easyx.h>
#include <stdio.h>
int main()
{
    initgraph(800, 600);
    setorigin(400, 300);
    setaspectratio(1, -1);

    //  画同心圆
    for (int r = 10; r <= 300; r += 10)
    {
        circle(0 , 0 , r);
    }

    getchar();
    closegraph();
    return 0;
}

同心圆

4.矩形

rectangle

这个函数用于画矩形。

void rectangle(
    int left,
    int top,
    int right,
    int bottom
);
参数 类型 意义
left int 矩形左上角 x 坐标
top int 矩形左上角 y 坐标
right int 矩形右下角 x 坐标
bottom int 矩形右下角 y 坐标

返回值 无

由矩形的左上角右下角顶点即可确定一个矩形。

矩形

让我们使用左上角(-200, 100),右下角(200, -100)绘制一个矩形。

#include <easyx.h>
#include <stdio.h>
int main()
{
    initgraph(800, 600);
    setorigin(400, 300);
    setaspectratio(1, -1);

    //  矩形
    rectangle(-200, 100, 200, -100);

    getchar();
    closegraph();
    return 0;
}

矩形

5.椭圆

ellipse

这个函数用于画椭圆。

void ellipse(
    int left,
    int top,
    int right,
    int bottom
);
参数 类型 意义
left int 椭圆外切矩形的左上角 x 坐标
top int 椭圆外切矩形的左上角 y 坐标
right int 椭圆外切矩形的右下角 x 坐标
bottom int 椭圆外切矩形的右下角 y 坐标

返回值 无

由矩形的左上角右下角顶点可确定一个矩形,ellipse函数在该矩形内做内切椭圆

椭圆

让我们使用左上角(-200, 100),右下角(200, -100)确定一个矩形,接着绘制这个矩形的内切椭圆

#include <easyx.h>
#include <stdio.h>
int main()
{
    initgraph(800, 600);
    setorigin(400, 300);
    setaspectratio(1, -1);

    //  椭圆
    ellipse(-200, 100, 200, -100);

    getchar();
    closegraph();
    return 0;
}

椭圆

6.圆角矩形

roundrect

这个函数用于画圆角矩形。

void roundrect(
    int left,
    int top,
    int right,
    int bottom,
    int ellipsewidth,
    int ellipseheight
);
参数 类型 意义
left int 圆角矩形左上角 x 坐标
top int 圆角矩形左上角 y 坐标
right int 圆角矩形右下角 x 坐标
bottom int 圆角矩形右下角 y 坐标
ellipsewidth int 构成圆角矩形的圆角的椭圆的宽度
ellipseheight int 构成圆角矩形的圆角的椭圆的高度

返回值 无

圆角矩形与矩形类似。只不过,它的四个角被椭圆的一段弧形替代roundrect函数前四个参数为矩形左上角右下角顶点的坐标,后两个参数为椭圆的宽与高

圆角矩形

让我们使用左上角(-200, 100),右下角(200, -100)确定一个矩形,再使用宽度为200,高度为100的椭圆的一段弧形替代矩形的四个角。

#include <easyx.h>
#include <stdio.h>
int main()
{
    initgraph(800, 600);
    setorigin(400, 300);
    setaspectratio(1, -1);

    //  圆角矩形
    roundrect(-200, 100, 200, -100, 200, 100);

    getchar();
    closegraph();
    return 0;
}

圆角矩形

7.扇形

pie

这个函数用于画扇形。

void pie(
    int left,
    int top,
    int right,
    int bottom,
    double stangle,
    double endangle
);
参数 类型 意义
left int 扇形所在椭圆的外切矩形的左上角 x 坐标
top int 扇形所在椭圆的外切矩形的左上角 y 坐标
right int 扇形所在椭圆的外切矩形的右下角 x 坐标
bottom int 扇形所在椭圆的外切矩形的右下角 y 坐标
stangle int 扇形的起始角的弧度
endangle int 扇形的终止角的弧度

返回值 无

扇形pie函数前四个参数与椭圆函数ellipse一致,为椭圆外切矩形的左上角坐标与右下角坐标。确定椭圆之后,再根据后两个参数,起始弧度终止弧度确定扇形的圆心角大小。圆心角从X轴正方向开始顺时针旋转,单位为弧度。

若不翻转Y坐标轴,圆心角从X轴正方向开始逆时针旋转

让我们根据外切矩形左上角(-200, 100)、右下角(200, -100)确定椭圆。接着,圆心角选取从0弧度开始,到π / 4弧度的区间。

扇形

#include <easyx.h>
#include <stdio.h>

#define PI 3.14

int main()
{
    initgraph(800, 600);
    setorigin(400, 300);
    setaspectratio(1, -1);

    //  画扇形
    pie(-200, 100, 200, -100, 0, PI / 4);

    getchar();
    closegraph();
    return 0;
}

扇形

8.圆弧

arc

这个函数用于画圆弧。

void arc(
    int left,
    int top,
    int right,
    int bottom,
    double stangle,
    double endangle
);
参数 类型 意义
left int 圆弧所在椭圆的外切矩形的左上角 x 坐标
top int 圆弧所在椭圆的外切矩形的左上角 y 坐标
right int 圆弧所在椭圆的外切矩形的右下角 x 坐标
bottom int 圆弧所在椭圆的外切矩形的右下角 y 坐标
stangle int 圆弧的起始角的弧度
endangle int 圆弧的终止角的弧度

圆弧arc函数与扇形pie函数的参数全部一致,只不过圆弧arc函数不会绘制从圆心到圆周的线段

圆弧

#include <easyx.h>
#include <stdio.h>

#define PI 3.14

int main()
{
    initgraph(800, 600);
    setorigin(400, 300);
    setaspectratio(1, -1);

    //  画圆弧
    arc(-200, 100, 200, -100, 0, PI / 4);

    getchar();
    closegraph();
    return 0;
}

圆弧