Dotcpp  >  编程教程  >  C/C++工具及其他类项目  >  C语言绘制隐藏的爱心教程及源码

C语言绘制隐藏的爱心教程及源码

点击打开在线编译器,边学边练

一、项目介绍

这是一个用C语言绘制的静态立体图形,但是一直盯住你就会发现图形中间有一颗心在隐隐跳动!

眼见不一定为实,你也会被自己的大脑“欺骗”。

编译环境:visual c++ 6.0

第三方库:Easyx2022  注意需要提前安装easyX,如没有基础可以先了解easyX图形编程

二、运行截图

添加折线前

(添加折线前)

添加折线后

(添加折线后)

三、代码思路

1.引入easyx头文件

#include <easyx.h>

2.创建绘图窗口

initgraph(774, 774);
setbkcolor(0xa6a6a6);
cleardevice();

3.绘制背景

setorigin(387 - UNIT / 2, 387 - UNIT / 2);
for (int x = -9; x <= 9; x++)
for (int y = -9; y <= 9; y++)
DrawBlock(x * UNIT, y * UNIT, (x + y) % 2);
setorigin(0, 0);

4.画辅助图

IMAGE img(774, 774);
SetWorkingImage(&img);
setbkcolor(BLACK);
cleardevice();

5.画心(只需画左半侧)

POINT ps[4] = { 387, 236,  321, 167,  129, 419,  387, 666 };
setfillcolor(WHITE);
solidpolygon(ps, 4);
solidellipse(76, 124, 368, 456);
SetWorkingImage(NULL);

6.根据辅助图,将辅助图中白色部分与右侧颠倒

DWORD* ref = GetImageBuffer(&img);
DWORD* bk = GetImageBuffer(NULL);
int t;
for (int y = 0; y < 774; y++)
for (int x = 0; x < 774 / 2; x++)
if (ref[y * 774 + x] != BLACK)
{
t = bk[y * 774 + x];
bk[y * 774 + x] = bk[y * 774 + 774 - x];
bk[y * 774 + 774 - x] = t;
}

完成

四、完整源码

#include <graphics.h> 
#include <conio.h>
#include <easyx.h>
#include <stdio.h>
const int UNIT = 43;
///画背景单元方格///
void DrawBlock(int x, int y, bool odd)
{
int c1, c2;
if (odd) { c1 = RED; c2 = WHITE; }
else { c1 = WHITE; c2 = LIGHTRED; }
setlinecolor(c1);
line(x, y, x + UNIT - 1, y);
line(x + UNIT - 1, y, x + UNIT - 1, y + UNIT - 1);
setlinecolor(c2);
line(x, y, x , y + UNIT - 1);
line(x, y + UNIT - 1, x + UNIT - 1, y + UNIT - 1);
setfillcolor(c1);
POINT ps[3] = {x, y,  x + 8, y,  x, y + 8};  // 左上
solidpolygon(ps, 3);
ps[0].x = x + UNIT;ps[0].y = y;  // 右上
ps[1].x = ps[0].x - 8;ps[1].y = ps[0].y;
ps[2].x = ps[0].x;ps[2].y = ps[0].y + 8;
solidpolygon(ps, 3);
ps[0].x = x + UNIT;ps[0].y = y + UNIT - 1;  // 右下
ps[1].x = ps[0].x - 8;ps[1].y = ps[0].y;
ps[2].x = ps[0].x;ps[2].y = ps[0].y - 8;
solidpolygon(ps, 3);
setfillcolor(c2);
ps[0].x = x;ps[0].y = y;  // 左上
ps[1].x = ps[0].x + 4;ps[1].y = ps[0].y + 4;
ps[2].x = ps[0].x;ps[2].y = ps[0].y + 8;
solidpolygon(ps, 3);
ps[0].x = x + UNIT;ps[0].y = y + UNIT - 1;  // 右下
ps[1].x = ps[0].x - 8;ps[1].y = ps[0].y;
ps[2].x = ps[0].x - 4;ps[2].y = ps[0].y - 4;
solidpolygon(ps, 3);
ps[0].x = x;ps[0].y = y + UNIT - 1;  // 左下
ps[1].x = ps[0].x + 8;ps[1].y = ps[0].y;
ps[2].x = ps[0].x;ps[2].y = ps[0].y - 8;
solidpolygon(ps, 3);
setlinecolor(0x888888);
line(x, y, x + 3, y + 3);
line(x + UNIT - 1, y + UNIT - 1, x + UNIT - 4, y + UNIT - 4);
}
int main()
{
initgraph(774, 774);// 创建图形窗口
setbkcolor(0xa6a6a6);
cleardevice();
///画背景///
setorigin(387 - UNIT / 2, 387 - UNIT / 2);
for (int x = -9; x <= 9; x++)
for (int y = -9; y <= 9; y++)
DrawBlock(x * UNIT, y * UNIT, (x + y) % 2);
setorigin(0, 0);
///画辅助图///
IMAGE img(774, 774);
SetWorkingImage(&img);
setbkcolor(BLACK);
cleardevice();
///画心(只需画左半侧)///
POINT ps[4] = { 387, 236,  321, 167,  129, 419,  387, 666 };
setfillcolor(WHITE);
solidpolygon(ps, 4);
solidellipse(76, 124, 368, 456);
SetWorkingImage(NULL);
///根据辅助图,将辅助图中白色部分与右侧颠倒///
DWORD* ref = GetImageBuffer(&img);
DWORD* bk = GetImageBuffer(NULL);
int t;
for (int y = 0; y < 774; y++)
for (int x = 0; x < 774 / 2; x++)
if (ref[y * 774 + x] != BLACK)
{
t = bk[y * 774 + x];
bk[y * 774 + x] = bk[y * 774 + 774 - x];
bk[y * 774 + 774 - x] = t;
}
    setcolor(BLUE);
    setbkmode(TRANSPARENT);
settextstyle(30,0,"楷体");
outtextxy(600, 600, "Dotcpp.com");
_getch();
closegraph();
return 0;
}



本文固定URL:https://www.dotcpp.com/course/1268

趣味项目教程
第一章 C/C++游戏类项目
第二章 C/C++工具及其他类项目
第三章 Python趣味项目
Dotcpp在线编译      (登录可减少运行等待时间)