掌握C语言,坐标计算不再难:一招解锁二维、三维坐标变换秘籍

引言
在计算机图形学、游戏开发、物理模拟等领域,坐标变换是一项基础且重要的技能。掌握坐标变换,可以帮助我们更好地处理图形、物体在空间中的位置变化。本文将介绍如何使用C语言实现二维和三维坐标变换,包括平移、缩放、旋转等基本操作。
二维坐标变换
1. 平移变换
二维平移变换可以通过向量的加法来实现。假设原点为 ( O(0,0) ),点 ( P(x,y) ) 平移 ( (tx,ty) ) 后得到点 ( P’(x’,y’) ),则有:
[ x’ = x + tx ]
[ y’ = y + ty ]
C语言实现
#include

typedef struct {
double x;
double y;
} Point2D;

void translate(Point2D *p, double tx, double ty) {
p->x += tx;
p->y += ty;
}

int main() {
Point2D p = {1.0, 2.0};
double tx = 3.0, ty = 4.0;

translate(&p, tx, ty);
printf(“New coordinates: (%f, %f)\n”, p.x, p.y);

return 0;
}
2. 缩放变换
二维缩放变换可以通过乘法来实现。假设原点为 ( O(0,0) ),点 ( P(x,y) ) 缩放 ( s ) 倍后得到点 ( P’(x’,y’) ),则有:
[ x’ = sx ]
[ y’ = sy ]
C语言实现
#include

typedef struct {
double x;
double y;
} Point2D;

void scale(Point2D *p, double s) {
p->x *= s;
p->y *= s;
}

int main() {
Point2D p = {1.0, 2.0};
double s = 2.0;

scale(&p, s);
printf(“Scaled coordinates: (%f, %f)\n”, p.x, p.y);

return 0;
}
3. 旋转变换
二维旋转变换可以通过三角函数来实现。假设原点为 ( O(0,0) ),点 ( P(x,y) ) 绕原点逆时针旋转 ( \theta ) 角度后得到点 ( P’(x’,y’) ),则有:
[ x’ = x \cdot \cos(\theta) – y \cdot \sin(\theta) ]
[ y’ = x \cdot \sin(\theta) + y \cdot \cos(\theta) ]
C语言实现
#include
#include

typedef struct {
double x;
double y;
} Point2D;

void rotate(Point2D *p, double theta) {
double rad = theta * M_PI / 180.0;
double x = p->x;
double y = p->y;

p->x = x * cos(rad) – y * sin(rad);
p->y = x * sin(rad) + y * cos(rad);
}

int main() {
Point2D p = {1.0, 2.0};
double theta = 45.0;

rotate(&p, theta);
printf(“Rotated coordinates: (%f, %f)\n”, p.x, p.y);

return 0;
}
三维坐标变换
1. 平移变换
三维平移变换与二维类似,只需在 ( z ) 轴上增加一个平移量即可。
C语言实现
#include

typedef struct {
double x;
double y;
double z;
} Point3D;

void translate3D(Point3D *p, double tx, double ty, double tz) {
p->x += tx;
p->y += ty;
p->z += tz;
}

int main() {
Point3D p = {1.0, 2.0, 3.0};
double tx = 3.0, ty = 4.0, tz = 5.0;

translate3D(&p, tx, ty, tz);
printf(“New coordinates: (%f, %f, %f)\n”, p.x, p.y, p.z);

return 0;
}
2. 缩放变换
三维缩放变换与二维类似,只需在 ( x )、( y )、( z ) 轴上分别乘以缩放因子即可。
C语言实现
#include

typedef struct {
double x;
double y;
double z;
} Point3D;

void scale3D(Point3D *p, double s) {
p->x *= s;
p->y *= s;
p->z *= s;
}

int main() {
Point3D p = {1.0, 2.0, 3.0};
double s = 2.0;

scale3D(&p, s);
printf(“Scaled coordinates: (%f, %f, %f)\n”, p.x, p.y, p.z);

return 0;
}
3. 旋转变换
三维旋转变换较为复杂,需要使用旋转矩阵。以下为绕 ( X )、( Y )、( Z ) 轴旋转的旋转矩阵:
绕 ( X ) 轴旋转:
[ \begin{bmatrix} 1 & 0 & 0 \ 0 & \cos(\theta) & -\sin(\theta) \ 0 & \sin(\theta) & \cos(\theta) \end{bmatrix} ]
绕 ( Y ) 轴旋转:
[ \begin{bmatrix} \cos(\theta) & 0 & \sin(\theta) \ 0 & 1 & 0 \ -\sin(\theta) & 0 & \cos(\theta) \end{bmatrix} ]
绕 ( Z ) 轴旋转:
[ \begin{bmatrix} \cos(\theta) & -\sin(\theta) & 0 \ \sin(\theta) & \cos(\theta) & 0 \ 0 & 0 & 1 \end{bmatrix} ]
C语言实现
#include
#include

typedef struct {
double x;
double y;
double z;
} Point3D;

void rotateX(Point3D *p, double theta) {
double rad = theta * M_PI / 180.0;
double x = p->x;
double y = p->y;
double z = p->z;

p->x = x;
p->y = x * cos(rad) – y * sin(rad);
p->z = x * sin(rad) + y * cos(rad);
}

void rotateY(Point3D *p, double theta) {
double rad = theta * M_PI / 180.0;
double x = p->x;
double y = p->y;
double z = p->z;

p->x = x * cos(rad) + z * sin(rad);
p->y = y;
p->z = -x * sin(rad) + z * cos(rad);
}

void rotateZ(Point3D *p, double theta) {
double rad = theta * M_PI / 180.0;
double x = p->x;
double y = p->y;
double z = p->z;

p->x = x * cos(rad) – y * sin(rad);
p->y = x * sin(rad) + y * cos(rad);
p->z = z;
}

int main() {
Point3D p = {1.0, 2.0, 3.0};
double theta = 45.0;

rotateX(&p, theta);
printf(“Rotated around X-axis: (%f, %f, %f)\n”, p.x, p.y, p.z);

rotateY(&p, theta);
printf(“Rotated around Y-axis: (%f, %f, %f)\n”, p.x, p.y, p.z);

rotateZ(&p, theta);
printf(“Rotated around Z-axis: (%f, %f, %f)\n”, p.x, p.y, p.z);

return 0;
}
总结
通过本文的介绍,相信您已经掌握了使用C语言进行二维和三维坐标变换的方法。在实际应用中,可以根据需要选择合适的变换方式,实现各种图形和物体的位置变化。