1.编码实现绕原点的旋转变换(旋转角度由键盘输入),
2.绕任意一点的旋转变换(由键盘输入旋转角度和旋转中心)
编译器:vs2013
1 #include "stdafx.h" 2 #include<stdio.h> 3 #include"graphics.h" 4 #include<stdlib.h> 5 #include<math.h> 6 7 #define PI 3.14159 8 9 void spin00(int a[], double angle); 10 void spinxy(int a[], double angle, int x, int y); 11 12 int main() 13 { 14 int gdriver=DETECT, gmove; 15 double angle; 16 int a[28] = { 100, 100, 150, 150, 250, 150, 250, 0, 350, 150, 450, 150, 500, 200, 450, 250, 350, 250, 250, 400, 250, 250, 150, 250, 100, 300, 100, 100 }; 17 18 /*printf("please input the angle: "); 19 scanf_s("%lf", &angle); 20 21 initgraph(&gdriver, &gmove, ""); 22 23 drawpoly(14, a); 24 25 spin00(a, angle);*/ 26 27 int x, y; 28 29 printf("please input the angle: "); 30 scanf_s("%lf", &angle); 31 printf("please input the point: "); 32 scanf_s("%d%d", &x, &y); 33 34 initgraph(&gdriver, &gmove, ""); 35 36 spinxy(a, angle, x, y); 37 38 system("pause"); 39 40 closegraph(); 41 42 return 0; 43 } 44 45 //绕原点旋转变换 46 void spin00(int a[], double angle) 47 { 48 int i,b[28]; 49 50 for (i = 0; i < 28; i = i + 2) 51 { 52 b[i] = a[i] * cos(angle / 180.0*PI) - a[i + 1] * sin(angle / 180.0*PI); 53 b[i + 1] = a[i] * sin(angle / 180.0*PI) + a[i + 1] * cos(angle / 180.0*PI); 54 } 55 56 drawpoly(14, b); 57 } 58 59 //绕(x,y)旋转变换 60 void spinxy(int a[], double angle, int x, int y) 61 { 62 int i, b[28],t; 63 64 for (i = 0; i < 28; i = i + 2) 65 { 66 b[i] = a[i] - x; 67 b[i + 1] = a[i + 1] - y; 68 t = b[i]; 69 b[i] = b[i] * cos(angle / 180.0*PI) - b[i + 1] * sin(angle / 180.0*PI); 70 b[i + 1] = t * sin(angle / 180.0*PI) + b[i + 1] * cos(angle / 180.0*PI); 71 b[i] = b[i] + x; 72 b[i + 1] = b[i + 1] + y; 73 } 74 75 drawpoly(14, b); 76 }
小结:在第二题时需要注意b[i]在操作过程中被改变,需要设置新变量来存储旧值