songhaibo_branch
1 year ago
parent feb4493622
commit 9aeddde25c

@ -0,0 +1,19 @@
#include "pixel.h"
#include <iostream>
using namespace std;
int main() {
// 传入现实中两个物体的面积和它们在图像中所占像素面积
PixelRatioConverter p(5, 10, 100, 200);
// 获取像素比例系数
double ratio = p.getPixelRatio();
cout << "像素比例系数:" << ratio << endl;
// 计算下一步物体在图像中所占像素面积
int next_pixel_area = p.getNextPixelArea(15);
cout << "下一步物体在图像中所占像素面积:" << next_pixel_area << endl;
return 0;
}

@ -0,0 +1,29 @@
class PixelRatioConverter {
public:
// 构造函数,传入两个现实中的物体面积和它们在图像中所占像素面积
PixelRatioConverter(double real_area1, double real_area2, int pixel_area1, int pixel_area2) {
this->real_area1 = real_area1;
this->real_area2 = real_area2;
this->pixel_area1 = pixel_area1;
this->pixel_area2 = pixel_area2;
}
// 获取像素比例系数
double getPixelRatio() {
double ratio = (real_area1 / real_area2) * (pixel_area2 / pixel_area1);
return ratio; // 返回像素比例系数
}
// 根据像素比例系数,计算下一步物体在图像中所占像素面积
int getNextPixelArea(int next_real_area) {
double ratio = this->getPixelRatio();
int next_pixel_area = static_cast<int>(next_real_area / ratio);
return next_pixel_area; // 返回下一步物体在图像中所占像素面积
}
private:
double real_area1; // 第一个现实中的物体面积
double real_area2; // 第二个现实中的物体面积
int pixel_area1; // 第一个物体在图像中所占像素面积
int pixel_area2; // 第二个物体在图像中所占像素面积
};

@ -0,0 +1,149 @@
#include <iostream>
#include <cmath>
using namespace std;
class ForestFireModel {
private:
int rows; // 森林地图行数
int cols; // 森林地图列数
double ignitionProbability; // 点燃概率
double burningProbability; // 火势蔓延概率
double** forestMap; // 森林地图
public:
ForestFireModel(int rows, int cols, double ignitionProbability, double burningProbability) {
this->rows = rows;
this->cols = cols;
this->ignitionProbability = ignitionProbability;
this->burningProbability = burningProbability;
// 初始化森林地图
forestMap = new double* [rows];
for (int i = 0; i < rows; i++) {
forestMap[i] = new double[cols];
for (int j = 0; j < cols; j++) {
forestMap[i][j] = (double)rand() / (RAND_MAX);
}
}
}
// 模拟火灾扩散过程
void simulate(int steps) {
for (int t = 0; t < steps; t++) {
double** nextMap = new double* [rows];
for (int i = 0; i < rows; i++) {
nextMap[i] = new double[cols];
for (int j = 0; j < cols; j++) {
bool isBurning = false;
bool isIgnited = (double)rand() / (RAND_MAX) < ignitionProbability;
if (isIgnited || forestMap[i][j] > 0) {
// 如果点燃或当前位置已经着火,判断周围是否有火灾扩散
for (int k = -1; k <= 1; k++) {
for (int l = -1; l <= 1; l++) {
if (k == 0 && l == 0) {
continue;
}
int ii = i + k;
int jj = j + l;
if (ii < 0 || ii >= rows || jj < 0 || jj >= cols) {
continue;
}
if (forestMap[ii][jj] > 0) {
// 周围有火,当前位置也着火
isBurning = true;
}
else if ((double)rand() / (RAND_MAX) < burningProbability) {
// 通过概率决定当前位置是否着火
isBurning = true;
}
}
}
}
nextMap[i][j] = isBurning ? 1 : (isIgnited ? 0.5 : forestMap[i][j]);
}
}
// 更新森林地图
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
forestMap[i][j] = nextMap[i][j];
}
delete[] nextMap[i];
}
delete[] nextMap;
}
}
// 计算指定位置的火灾蔓延速度
double spreadSpeed(int x, int y) {
double speed = 0;
if (forestMap[x][y] == 0) {
return speed;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
double distance = sqrt(pow(x - i, 2) + pow(y - j, 2));
if (forestMap[i][j] > 0 && distance > 0) {
speed += forestMap[i][j] / distance;
}
}
}
return speed;
}
// 计算当前火灾扩大范围的半径
double fireRadius() {
double radius = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (forestMap[i][j] >= 1) {
double distance = sqrt(pow(i - rows / 2, 2) + pow(j - cols / 2, 2));
radius = max(radius, distance);
}
}
}
return radius;
}
// 输出森林地图
void printMap() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (forestMap[i][j] >= 1) {
cout << "* ";
}
else if (forestMap[i][j] > 0) {
cout << "o ";
}
else {
cout << ". ";
}
}
cout << endl;
}
}
~ForestFireModel() {
for (int i = 0; i < rows; i++) {
delete[] forestMap[i];
}
delete[] forestMap;
}
};
int main() {
srand(time(0));
// 模拟一个5x5的森林点燃概率为0.2火势蔓延概率为0.8模拟100步
ForestFireModel model(5, 5, 0.2, 0.8);
model.simulate(100);
model.printMap();
// 计算指定位置的火灾蔓延速度
cout << "Spread speed at position (2, 3): " << model.spreadSpeed(2, 3) << endl;
// 计算当前火灾扩大范围的半径
cout << "Fire radius: " << model.fireRadius() << endl;
return 0;
}
Loading…
Cancel
Save