图形的伸缩,错切等变化

版权声明:欢迎转载,请注明出处噢,谢谢 https://blog.csdn.net/DT2131/article/details/78948104

在此以二维图形为例,扩展到更高维思路都是一样的,明白变化矩阵就都明白了。

使用了 EasyX 图形库,对 EasyX 团队表示感谢

#include <bits/stdc++.h>
#include <graphics.h>
#include <conio.h>
typedef struct Node {
	int x, y, z;
}Node;
typedef struct Matrix {
	int a, b, p,
		c, d, q,
		k, m, s;
}Matrix;
const int N = 3;
Node nodes[N];
Matrix transer;
void trans() {
	Node temp;
	for (int i = 0; i < N; i++) {
		temp = nodes[i];
		nodes[i].x = temp.x*transer.a + temp.y*transer.c + temp.z*transer.k;
		nodes[i].y = temp.x*transer.b + temp.y*transer.d + temp.z*transer.m;
		nodes[i].z = temp.x*transer.p + temp.y*transer.q + temp.z*transer.s;
		nodes[i].x /= nodes[i].z; nodes[i].y /= nodes[i].z; nodes[i].z = 1;
	}
}
void output() {
	for (int i = 0; i < N; i++)	
		line(nodes[i].x, nodes[i].y, nodes[(i + 1) % N].x, nodes[(i + 1) % N].y);
}
void input() {
	nodes[0] = { 25,25,1 };
	nodes[1] = { 50,25,1 };
	nodes[2] = { 50,50,1 };
	output();
}
void translation() {
	transer = { 1,0,0,
		    0,1,0,
		    100,50,1 };
	trans();
	output();
}
void scaling() {
	transer = { 2,0,0,
		    0,2,0,
		    0,0,1 };
	trans();
	output();
}
void symmetry() {
	//X axis symmetry
	transer = { 1,0,0,
		    0,-1,0,
		    0,0,1 };
	//Y axis symmetry
	transer = { -1,0,0,
		     0,1,0,
		     0,0,1 };
	//Origin symmetry
	transer = { -1,0,0,
		     0,-1,0,
		     0,0,1 };
	//45 symmetry
	transer = { 0,1,0,
		    1,0,0,
		    0,0,1 };
	trans();
	output();
}
void Shear_mapping() {
	transer = { 1,1,0,
		    2,1,0,
		    0,0,1 };
	trans();
	output();
}

int main()
{
	initgraph(1366, 768);
	input();
	translation();
	scaling();
	symmetry();
	Shear_mapping();
	system("pause");
    return 0;
}




展开阅读全文

没有更多推荐了,返回首页