前几天学习了C语言上爱心代码:
1.静态的
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int main(int argc,char*argv[]){
float x,y,a;
for(y=1.5;y>-1.5;y-=0.1){
for(x=-1.5;x<1.5;x+=0.05){
a = x * x +y * y -1;
putchar(a*a*a-x*x*y*y*y<=0.0?'*':' ');
}
system("color 0c");
putchar('\n');
}
printf("天天开心\n");
printf("好好学习 好好生活\n");
return 0;
}
2.动态的
#include <graphics.h>
#include <conio.h>
#include <time.h>
#include <math.h>
#include <stdlib.h>
struct Point {
double x, y;
COLORREF color;
};
COLORREF colors[256] = { RGB(255,32,83), RGB(252,222,250), RGB(255,0,0), RGB(255,0,0), RGB(255,2,2), RGB(255,0,8), RGB(255,5,5) };
const int xScreen = 1200;
const int yScreen = 800;
const double PI = 3.14159265358979323846;
const double e = 2.71828;
const double averag_distance = 0.162;
const int quantity = 506;
const int circles = 210;
const int frames = 20;
Point origin_points[quantity];
Point points[circles * quantity]; // 修改为circles
IMAGE images[frames];
double screen_x(double x) {
x += xScreen / 2;
return x;
}
double screen_y(double y) {
y = -y + yScreen / 2;
return y;
}
int creat_random(int x1, int x2) {
if (x2 > x1)
return rand() % (x2 - x1 + 1) + x1;
return x1;
}
void creat_data() {
int index = 0;
double x1 = 0, y1 = 0, x2 = 0, y2 = 0;
for (double radian = 0.1; radian <= 2 * PI; radian += 0.005) {
x2 = 16 * pow(sin(radian), 3);
y2 = 13 * cos(radian) - 5 * cos(2 * radian) - 2 * cos(3 * radian) - cos(4 * radian);
double distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
if (distance > averag_distance) {
x1 = x2, y1 = y2;
origin_points[index].x = x2;
origin_points[index++].y = y2;
}
}
index = 0;
for (double size = 0.1, lightness = 1.5; size <= 20; size += 0.1) {
double success_p = 1 / (1 + pow(e, 8 - size / 2));
if (lightness > 1) lightness -= 0.0025;
for (int i = 0; i < quantity; ++i) {
if (creat_random(0, 100) / 100.0 < success_p) {
COLORREF color = colors[creat_random(0, 6)];
points[index].color = RGB(GetRValue(color) / lightness, GetGValue(color) / lightness, GetBValue(color) / lightness);
points[index].x = size * origin_points[i].x + creat_random(-4, 4);
points[index++].y = size * origin_points[i].y + creat_random(-4, 4);
}
}
}
int points_size = index;
for (int frame = 0; frame < frames; ++frame) {
images[frame] = IMAGE(xScreen, yScreen);
SetWorkingImage(&images[frame]);
for (index = 0; index < points_size; ++index) {
double x = points[index].x, y = points[index].y;
double distance = sqrt(pow(x, 2) + pow(y, 2));
double distance_increase = -0.0009 * distance * distance + 0.35714 * distance + 5;
double x_increase = distance_increase * x / distance / frames;
double y_increase = distance_increase * y / distance / frames;
points[index].x += x_increase;
points[index].y += y_increase;
setfillcolor(points[index].color);
solidcircle(screen_x(points[index].x), screen_y(points[index].y), 1);
}
for (double size = 17; size < 23; size += 0.3) {
for (index = 0; index < quantity; ++index) {
double x, y;
if ((creat_random(0, 100) / 100.0 > 0.6 && size >= 20) || (size < 20 && creat_random(0, 100) / 100.0 > 0.95)) {
x = origin_points[index].x * size + creat_random(-frame * frame / 5 - 15, frame * frame / 5 + 15);
y = origin_points[index].y * size + creat_random(-frame * frame / 5 - 15, frame * frame / 5 + 15);
}
else {
x = origin_points[index].x * size + creat_random(-5, 5);
y = origin_points[index].y * size + creat_random(-5, 5);
}
setfillcolor(colors[creat_random(0, 6)]);
solidcircle(screen_x(x), screen_y(y), 1);
}
}
}
SetWorkingImage();
}
int main() {
initgraph(xScreen, yScreen);
BeginBatchDraw();
srand(time(0));
creat_data();
bool extend = true, shrink = false;
for (int frame = 0; !_kbhit(); ) {
putimage(0, 0, &images[frame]);
FlushBatchDraw();
Sleep(20);
cleardevice();
if (extend) {
frame == 19 ? (shrink = true, extend = false) : ++frame;
}
else {
frame == 0 ? (shrink = false, extend = true) : --frame;
}
}
EndBatchDraw();
closegraph();
return 0;
}
这里需要一个图形库文件:<graphics.h>
但是如今的IDE中不再内置这个头文件,需要自行安装EasyX
安装方法:
1.下载安装包:
打开官网:EasyX Graphics Library for C++
点击右上角【下载】
点击右侧【下载EasyX】(注意自行更改下载位置)
打开安装文件并点击【下一步】
!!!注意:必须安装了C++桌面开发组件才可以被检测到,若没有安装请在Visual Studio Installer中进行安装
!!!注意:如果你的Visual Studio2022安装在了C盘这里可以自动检测出来,如果安装在其他盘无法被自动检测到,需要自行安装,操作如下:
解压安装包(使用本地压缩软件(如7-zip,但是不免费)进行解压或使用在线网页(free),如:在线文件压缩和转换变得简单。免费! | ezyZip)
选择导航栏的【在线解压文件-其他】,寻找到解压exe文件,上传文件并进行解压,解压后保存本地
然后按照EasyX 文档 - 安装中提供的VS版本找出自己需要安装的文件并复制
粘贴到如下所示:
EasyX安装包\include\easyx.h 拷贝到 D:\App\VS2022\VC\Auxiliary\VS\include
EasyX安装包\include\graphics.h 拷贝到 D:\App\VS2022\VC\Auxiliary\VS\include
EasyX安装包\lib\VC2015\x86\EasyXa.lib 拷贝到 D:\App\VS2022\VC\Auxiliary\VS\lib\x86
EasyX安装包\lib\VC2015\x86\EasyXw.lib 拷贝到 D:\App\VS2022\VC\Auxiliary\VS\lib\x86
EasyX安装包\lib\VC2015\x64\EasyXa.lib 拷贝到 D:\App\VS2022\VC\Auxiliary\VS\lib\x64
EasyX安装包\lib\VC2015\x64\EasyXw.lib 拷贝到 D:\App\VS2022\VC\Auxiliary\VS\lib\x64
[备注]引用资源与参考内容:
https://docs.easyx.cn/zh-cn/setup
安装 EasyX 图形库时检测不到我的 VisualStudio 2019 / 2022 - CodeBus
标签:index,int,double,frame,EasyX,C语言,爱心,points,size From: https://blog.csdn.net/wzzzz6423/article/details/142869019