1. 坐标系简介在地理信息系统、卫星导航、计算机图形学等领域,坐标转换是基本且重要的操作。常见的坐标系包括笛卡尔坐标系、极坐标系、地理坐标系等。以下将介绍这些坐标系及其转换方法。1.1 笛卡尔坐标系笛...
在地理信息系统、卫星导航、计算机图形学等领域,坐标转换是基本且重要的操作。常见的坐标系包括笛卡尔坐标系、极坐标系、地理坐标系等。以下将介绍这些坐标系及其转换方法。
笛卡尔坐标系是最常用的直角坐标系,以原点 (0,0) 为中心,X 轴和 Y 轴分别表示水平和垂直方向。
极坐标系以原点为中心,角度表示点与正 X 轴的夹角,半径表示点到原点的距离。
地理坐标系也称为经纬度坐标系,以地球为参考,使用经度 (L) 和纬度 (B) 表示位置。
#include
#include
void cartesian_to_polar(double x, double y, double *r, double *theta) { *r = sqrt(x * x + y * y); *theta = atan2(y, x);
}
int main() { double x = 3, y = 4; double r, theta; cartesian_to_polar(x, y, &r, &theta); printf("极坐标: (半径: %.2f, 角度: %.2f)\n", r, theta * 180 / M_PI); return 0;
} void polar_to_cartesian(double r, double theta, double *x, double *y) { *x = r * cos(theta); *y = r * sin(theta);
}
int main() { double r = 5, theta = M_PI / 4; double x, y; polar_to_cartesian(r, theta, &x, &y); printf("笛卡尔坐标: (%.2f, %.2f)\n", x, y); return 0;
}#include
#include
#define R 6378137 // 地球半径
#define PI 3.14159265358979323846
void geographic_to_cartesian(double latitude, double longitude, double *x, double *y, double *z) { *latitude = latitude * PI / 180; // 纬度转为弧度 *longitude = longitude * PI / 180; // 经度转为弧度 *x = R * cos(*latitude) * cos(*longitude); *y = R * cos(*latitude) * sin(*longitude); *z = R * sin(*latitude);
}
int main() { double latitude = 30, longitude = 120; double x, y, z; geographic_to_cartesian(latitude, longitude, &x, &y, &z); printf("笛卡尔坐标: (%.2f, %.2f, %.2f)\n", x, y, z); return 0;
} void cartesian_to_geographic(double x, double y, double z, double *latitude, double *longitude, double *height) { *latitude = asin(z / R); *latitude = *latitude * 180 / PI; *longitude = atan2(y, x); *longitude = *longitude * 180 / PI; *height = R * (sin(*latitude) - (z / R));
}
int main() { double x = 3, y = 4, z = 5; double latitude, longitude, height; cartesian_to_geographic(x, y, z, &latitude, &longitude, &height); printf("地理坐标: (%.2f, %.2f, %.2f)\n", latitude, longitude, height); return 0;
}本文介绍了笛卡尔坐标系、极坐标系、地理坐标系及其转换方法。通过学习这些内容,您可以轻松地在不同坐标系之间进行转换。在实际应用中,合理选择和使用坐标系,将有助于您解决实际问题。