作业要求: http://www.cnblogs.com/bingc/p/4919692.html
代码(未使用Eigen):
1 #include <iostream> 2 #include <Eigen/Dense> 3 using namespace Eigen; 4 using Eigen::MatrixXd; 5 using namespace std; 6 class POINT 7 { 8 public: 9 string name; 10 double x, y; 11 12 }; 13 class LINE 14 { 15 public: 16 string name; 17 POINT p_start,p_end; 18 }; 19 class TRIANGLE 20 { 21 public: 22 string name; 23 POINT p1,p2, p3; 24 }; 25 void rotate(POINT *pt, double angle); 26 int main() 27 { string name,cmd,name1; 28 int num; 29 int p = 0, l = 0, t = 0; 30 const int maxnum = 10; 31 POINT point[maxnum]; 32 LINE line[maxnum]; 33 TRIANGLE triangle[maxnum]; 34 int flag; 35 char a, b, c; 36 double x, y, angle; 37 while (1) 38 { 39 cout << "存点、线、三角形,请输入0,操作点、线、三角形请输入1" << endl; 40 cin >> flag; 41 switch (flag) 42 { 43 case 0:{ 44 cout << "请输入图形名称 点数 坐标" << endl; 45 cin >> name; 46 cin >> num; 47 switch (num) 48 { 49 case 1: 50 { 51 if (p >= maxnum) { cout << "您的点的个数已达上限,不能再存!" << endl; break; } 52 point[p].name = name; 53 cin >> a >> point[p].x >> b >> point[p].y >> c; 54 p++; 55 break; 56 } 57 case 2: 58 { 59 if (l >= maxnum) { cout << "您的线的个数已达上限,不能再存!" << endl; break; } 60 line[l].name = name; 61 cin >> a >> line[l].p_start.x >> b >> line[l].p_start.y >> c >> a >> line[l].p_end.x >> b >> line[l].p_end.y >> c; 62 l++; 63 break; 64 } 65 case 3: 66 { 67 if (t >= maxnum) { cout << "您的三角形的个数已达上限,不能再存!" << endl; break; } 68 triangle[t].name = name; 69 cin >> a >> triangle[t].p1.x >> b >> triangle[t].p1.y >> c >> a >> triangle[t].p2.x >> b >> triangle[t].p2.y >> c >> a >> triangle[t].p3.x >> b >> triangle[t].p3.y >> c; 70 t++; 71 break; 72 } 73 default: {cout << "点数输入错误" << endl; break; } 74 } 75 break; 76 } 77 case 1:{ 78 cout << "请输入操作指令" << endl; 79 cin >> cmd; 80 cin>> name1; 81 if (cmd == "move") 82 { 83 cin >> a >> x >> b >> y >> c; 84 for (int i = 0; i < p; i++) 85 { 86 if (name1 == point[i].name) 87 { 88 point[i].x += x; 89 point[i].y += y; 90 cout << "平移后的点为" << "(" << point[i].x << "," << point[i].y << ")" << endl; 91 } 92 } 93 for (int i = 0; i < l; i++) 94 { 95 if (name1 == line[i].name) 96 { 97 line[i].p_start.x += x; 98 line[i].p_start.y += y; 99 line[i].p_end.x += x; 100 line[i].p_end.y += y; 101 cout << "平移后的线段端点为" << "(" << line[i].p_start.x << "," << line[i].p_start.y << ")" << "、(" << line[i].p_end.x << "," << line[i].p_end.y << ")" << endl; 102 } 103 } 104 for (int i = 0; i < t; i++) 105 { 106 if (name1 == triangle[i].name) 107 { 108 triangle[i].p1.x += x; 109 triangle[i].p1.y += y; 110 triangle[i].p2.x += x; 111 triangle[i].p2.y += y; 112 triangle[i].p3.x += x; 113 triangle[i].p3.y += y; 114 cout << "平移后的三角形顶点为为" << "(" << triangle[i].p1.x << "," << triangle[i].p1.y << ")" << "、(" << triangle[i].p2.x << "," << triangle[i].p2.y << ")" << "、(" << triangle[i].p3.x << "," << triangle[i].p3.y << ")" << endl; 115 } 116 } 117 } 118 else if (cmd == "rotate") 119 { 120 cin >> angle; 121 for (int i = 0; i < p; i++) 122 { 123 if (name1 == point[i].name) 124 { 125 rotate(&point[i], angle); 126 cout << "旋转后的点为" << "(" << point[i].x << "," << point[i].y << ")" << endl; 127 } 128 } 129 for (int i = 0; i < l; i++) 130 { 131 if (name1 == line[i].name) 132 { 133 rotate(&line[i].p_start, angle); 134 rotate(&line[i].p_end, angle); 135 cout << "旋转后的直线端点为" << "(" << line[i].p_start.x << "," << line[i].p_start.y << ")" << "、(" << line[i].p_end.x << "," << line[i].p_end.y << ")" << endl; 136 } 137 } 138 for (int i = 0; i < t; i++) 139 { 140 if (name1 == triangle[i].name) 141 { 142 rotate(&triangle[i].p1, angle); 143 rotate(&triangle[i].p2, angle); 144 rotate(&triangle[i].p3, angle); 145 cout << "旋转后的三角形顶点为为" << "(" << triangle[i].p1.x << "," << triangle[i].p1.y << ")" << "、(" << triangle[i].p2.x << "," << triangle[i].p2.y << ")" << "、(" << triangle[i].p3.x << "," << triangle[i].p3.y << ")" << endl; 146 } 147 } 148 } 149 else cout << "comand is error!" << endl; 150 break; 151 } 152 default:{cout << "输入错误!" << endl; break; } 153 154 } 155 } 156 157 return 0; 158 } 159 160 void rotate(POINT *pt, double angle) //逆时针为正 161 { 162 double x, y,ang; 163 x = pt->x; 164 y = pt->y; 165 ang = angle*3.1415926 / 180; 166 pt->x = x*cos(ang) - y*sin(ang); 167 pt->y = x*sin(ang) + y*cos(ang); 168 }
运行结果:
时间: 2024-10-12 16:50:11