2

EasyX绘制多边形

 1 year ago
source link: https://blog.51cto.com/u_15740457/5992156
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

在Easyx中,专门给了一个函数绘制多边形——polygon函数

一、打印较简单的多边形

像长方形、正方形、三角形、梯形这些多边形较容易打印,因为他们的顶点坐标较容易求出。

比如三角形

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

int main()
{
initgraph(800, 600);
setorigin(400, 300);
setaspectratio(1, -1);
POINT points[] = { {0,200},{200,-200},{-200,-200} };//声明POINT结构数组,使用三角形顶点初始化
polygon(points, 3);
//专门打印多边形的函数|先将上面定义数组首元素的指针传给polygon函数的第一个参数,第二个参数指明数组中的元素数量
getchar();
closegraph();
return 0;
}
EasyX绘制多边形_EasyX

二、较难打印的多边形

这一类多边形因为顶点坐标较难算出,需要用到一些数学知识。

举个栗子:打印正五边形

#include<easyx.h>
#include<stdio.h>
#include<math.h>//用到三角函数sin和cos,需要引用数学类头文件
#define PI 3.14//需要使用弧度制,因此需要定义PI

int main()
{
initgraph(800, 600);
setorigin(400, 300);
setaspectratio(1, -1);
double theta = PI / 2;
double delta = 2 * PI / 5;
int r = 200;
POINT points[5];//存储五边形的顶点
for (int i = 0; i < 5; i++)
{
points[i].x = cos(theta + i * delta) * r;
points[i].y = sin(theta + i * delta) * r;
}
//通过三角函数的方式计算五个顶点的坐标
polygon(points, 5);
getchar();
closegraph();
return 0;
}

输出结果:

EasyX绘制多边形_数组_02

三、polygon函数扩展

polygon函数不仅可以打印规则正多边形,不规则图形也能打印。

只需要将各个顶点传入polygon函数,就能将各个顶点依次相连,组成一个封闭的图形。

知道顶点坐标,一切都好说!

四、polygon函数和polyline函数对比

这两个函数基本一致,唯一区别在于前者绘制封闭图形,也就是会连接首尾两个顶点形成封闭图形,polyline就不会,因此形成不封闭图形。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK