#include <iostream>
#include <SFML/Graphics.hpp>
float temp[64][64];
float ktemp[64][64];
sf::RectangleShape quads[64][64];
//初始化温度散布
void initTemp() {
	for (int i = 0; i < 64; i++) {
		for (int j = 0; j < 64; j++) {
			ktemp[i][j] = temp[i][j] = 0;
			quads[i][j].setPosition(sf::Vector2f(j * 10, i * 10));
			quads[i][j].setSize(sf::Vector2f(10, 10));
		}
	}
}
//计算下一步的渲染画面
void Next() {
	for (int i = 1; i < 63; i++) {
		temp[31][i] = 255;//设置热源 
	}
	for (int i = 0; i < 64; i++) {
		for (int j = 0; j < 64; j++) {
			ktemp[i][j] = temp[i][j];
		}
	}
	for (int i = 1; i < 63; i++) {
		for (int j = 1; j < 63; j++) {
			/* f(x, y, t + 1) = a * (f(x + 1, y, t) + f(x - 1, y, t) + f(x, y + 1, t) + f(x, y - 1, t) - 4 * f(x, y, t)) + f(x, y, t)
			 * a为热扩散率,f(x,y,t)为横坐标为x,纵坐标为y,时刻为t的温度
			 */
			temp[i][j] = 0.1 * (ktemp[i][j + 1] + ktemp[i][j - 1] + ktemp[i + 1][j] + ktemp[i - 1][j] - 4 * ktemp[i][j]) + ktemp[i][j];
		}
	}
}
//绘制
void Draw(sf::RenderWindow& window) {
	for (int i = 0; i < 64; i++) {
		for (int j = 0; j < 64; j++) {
			if(temp[i][j] > 255)
				quads[i][j].setFillColor(sf::Color::White);
			else if(temp[i][j] < 0)
				quads[i][j].setFillColor(sf::Color::Black);
			else
				quads[i][j].setFillColor(sf::Color(temp[i][j], temp[i][j], temp[i][j]));
		}
	}
	for (int i = 0; i < 64; i++) {
		for (int j = 0; j < 64; j++) {
			window.draw(quads[i][j]);
		}
	}
}
int main() {
	sf::RenderWindow window(sf::VideoMode(640, 640), "Hello SFML");
	window.setFramerateLimit(60);
	initTemp();
	while (window.isOpen()) {
		sf::Event event;
		while (window.pollEvent(event)) {
			if (event.type == sf::Event::Closed)
				window.close();
		}
		window.clear();
		Draw(window);
		Next();
		window.display();
	}
	return 0;
}

演示效果图:
在这里插入图片描述

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐