变幻线

**将某些点之间用线连起来,就可以做一个屏保“变幻线”的程序

#include <graphics.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

#define HIGH   600                    //定义窗口大小
#define WIDTH  800
#define RADIUS 20
#define PN     5                       //点的个数

typedef struct Point                //定义结构体包含坐标以及速度
{
    int x;
    int y;
    int velocity_x;
    int velocity_y;
}VPOINT;

static VPOINT vpoint[PN];

int main(void)
{
    int a = 0;
    initgraph(WIDTH, HIGH);                 //初始化窗口
    srand(time(NULL));                      //设置随机种子
    setbkcolor(getbkcolor());               //设置当前绘图背景色
    cleardevice();                          //清屏

    for (int i = 0; i < PN; i++)
    {
        vpoint[i].x = rand() % (WIDTH - RADIUS) + RADIUS;//随机设置每个点的坐标
        vpoint[i].y = rand() % (HIGH - RADIUS) + RADIUS;
        a = rand() % 361;
        vpoint[i].velocity_x = 4 * cos(a);              //改变点的方向
        vpoint[i].velocity_y = 4 * sin(a);
    }
  
    while (!_kbhit())
    {
        BeginBatchDraw();
        cleardevice();                                   //清屏
        for (int i = 0; i < PN; i++)
        {
            if ((vpoint[i].x <= 0) || (vpoint[i].x >= WIDTH))
                vpoint[i].velocity_x = -vpoint[i].velocity_x;
            if ((vpoint[i].y <= 0) || (vpoint[i].y >= HIGH))
                vpoint[i].velocity_y = -vpoint[i].velocity_y;

            vpoint[i].x += vpoint[i].velocity_x;               //改变点的方向
            vpoint[i].y += vpoint[i].velocity_y;

            setlinecolor(BLUE);
            for (int i = 0; i < PN - 1; i++)                                   //连线
                line(vpoint[i].x, vpoint[i].y, vpoint[i + 1].x, vpoint[i + 1].y);
            line(vpoint[0].x, vpoint[0].y, vpoint[PN - 1].x, vpoint[PN - 1].y);//连线第一个点以及最后一个点
        }
        EndBatchDraw();
        Sleep(50);
    }
    closegraph();
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值