一个未验证的查找陆地角度算法
#include <stdio.h>
#include <math.h>
#include <float.h>
// 定义图像大小
#define IMAGE_WIDTH 100
#define IMAGE_HEIGHT 50
// 定义颜色枚举
typedef enum {
COLOR_WHITE = 0, // 海,白色
COLOR_BLACK = 1 // 陆地,黑色
} Color;
// 假设的当前位置坐标
int current_x = 20;
int current_y = 10;
// 假设的图像数据,1 表示黑色(陆地),0 表示白色(海)
Color image[IMAGE_HEIGHT][IMAGE_WIDTH] = {
{COLOR_WHITE, COLOR_WHITE, COLOR_WHITE, /* ... */ COLOR_BLACK, /* ... */ },
{COLOR_WHITE, COLOR_BLACK, COLOR_BLACK, /* ... */ COLOR_BLACK, /* ... */ },
// 更多行数据
};
// 计算两点之间的距离(这里用简化的欧几里得距离的平方)
double distance_squared(int x1, int y1, int x2, int y2) {
return pow(x1 - x2, 2) + pow(y1 - y2, 2);
}
// 计算岛屿的大小(黑色像素点的数量)
int calculate_island_size(int start_x, int start_y) {
int island_size = 0;
Color visited[IMAGE_HEIGHT][IMAGE_WIDTH] = {COLOR_WHITE}; // 标记已访问的像素点,初始为海(白色)
// 使用栈来实现深度优先搜索(DFS)
int stack[IMAGE_WIDTH * IMAGE_HEIGHT][2]; // 最大可能的栈大小
int top = -1;
// 初始点入栈
stack[++top][0] = start_x;
stack[top][1] = start_y;
visited[start_y][start_x] = COLOR_BLACK; // 标记为已访问(陆地)
// DFS 遍历连通区域
while (top >= 0) {
int x = stack[top][0];
int y = stack[top][1];
top--;
// 统计陆地像素点数量
island_size++;
// 检查上下左右四个方向
// 上方
if (y > 0 && visited[y - 1][x] == COLOR_WHITE && image[y - 1][x] == COLOR_BLACK) {
visited[y - 1][x] = COLOR_BLACK;
stack[++top][0] = x;
stack[top][1] = y - 1;
}
// 下方
if (y < IMAGE_HEIGHT - 1 && visited[y + 1][x] == COLOR_WHITE && image[y + 1][x] == COLOR_BLACK) {
visited[y + 1][x] = COLOR_BLACK;
stack[++top][0] = x;
stack[top][1] = y + 1;
}
// 左方
if (x > 0 && visited[y][x - 1] == COLOR_WHITE && image[y][x - 1] == COLOR_BLACK) {
visited[y][x - 1] = COLOR_BLACK;
stack[++top][0] = x - 1;
stack[top][1] = y;
}
// 右方
if (x < IMAGE_WIDTH - 1 && visited[y][x + 1] == COLOR_WHITE && image[y][x + 1] == COLOR_BLACK) {
visited[y][x + 1] = COLOR_BLACK;
stack[++top][0] = x + 1;
stack[top][1] = y;
}
}
return island_size;
}
// 找到离当前位置最近且大小合适的黑色岛屿的角度
double find_nearest_land_angle() {
int nearest_x = -1;
int nearest_y = -1;
double min_distance = DBL_MAX; // 使用 DBL_MAX 表示初始的最大距离
// 设置距离阈值
double distance_threshold = 100.0; // 这里设定一个较大的距离阈值,根据实际情况调整
// 遍历图像
for (int y = 0; y < IMAGE_HEIGHT; y++) {
for (int x = 0; x < IMAGE_WIDTH; x++) {
if (image[y][x] == COLOR_BLACK) { // 如果是黑色(陆地)
// 计算距离
double dist = distance_squared(current_x, current_y, x, y);
// 判断距离是否在阈值内
if (dist < min_distance && dist < distance_threshold * distance_threshold) {
// 计算岛屿大小
int island_size = calculate_island_size(x, y);
// 仅考虑大小合适的岛屿
if (island_size > 5) { // 这里以5个像素点为岛屿的最小大小阈值,根据实际情况调整
min_distance = dist;
nearest_x = x;
nearest_y = y;
}
}
}
}
}
// 如果找不到合适的最近点,返回错误标志或特定值
if (nearest_x == -1 || nearest_y == -1) {
printf("Error: No suitable nearest land found.\n");
return NAN; // 返回 NaN 表示未找到有效角度
}
// 计算角度
double angle = atan2(nearest_y - current_y, nearest_x - current_x);
// 转换为角度制
angle = angle * 180 / M_PI;
return angle;
}
int main() {
// 找到离当前位置最近且大小合适的黑色岛屿的角度
double nearest_land_angle = find_nearest_land_angle();
if (!isnan(nearest_land_angle)) {
printf("Nearest suitable land is at %.2f degrees relative to current position.\n", nearest_land_angle);
}
return 0;
}
License:
CC BY 4.0