题目: 第一步: 写一个根据输入坐标生成一个平面图形(点、线段和三角形)并能对该几何图形进行平移和旋转操作的命令行 “软件”,要求:
a) 通过命令行输入图形的名称,端点数(1为点,2为线段,3为三角形)和端点坐标(x,y)。
如:输入一个点 p1 1 (2,5)
输入一个线段 l1 2 (3,5) (4,6)
输入一个三角形 t1 3 (1,1) (2,5) (-4,7)
b) 输入一个计算指令,实现对上述图形的平移和绕原点旋转等操作,并输出该图形新的坐标值。
如:move l1 (3,6) 表示把线段l1沿x方向移动3,沿y方向移动6。
rotate t1 -30 表示把三角形t1绕原点逆时针旋转30度。
程序设计要求使用Eigen库函数,(如觉得有难度可先用自己编写的函数实现,但在实现第二步前必须修改成使用Eigen库函数),先在博客上提交代码,并附上运行结果截图。同学之间可相互阅读博客,比较一下各自程序的功能、实现方法的异同。
第二步,
在小组内每个同学在如下方向中选择一个,进行功能扩展:
a) 实现更复杂的二维图形操作,如定义更复杂的图形(多边形),实现图形围绕任意点的旋转,图形相对于某一条线的镜像,并考虑扩展到三维空间。
b) 把图形数据保存到文件,并能从文件中恢复。
c) 实现简易GUI,通过GUI输入图形坐标和操作命令,并显示图形。
考虑如何把上述三个软件功能集成在一起,如何定义接口?
估计做好这个软件需要的时间,并且写出大概的设计步骤和实现算法。
1 #include <iostream>
2 #include <Eigen/Dense>
3 #include <iomanip>
4
5 using Eigen::MatrixXd;
6 using namespace std;
7 #define PI 3.14159
8
9 class Shape
10 {
11 public:
12 char Name[10];
13 void virtual Move(double x, double y) = 0;
14 void virtual Rotation(double theta) = 0;
15 };
16 class Point : public Shape
17 {
18 public:
19 Point()
20 {
21 cin >> x0 >> y0;
22 }
23 void output()
24 {
25 cout << setiosflags(ios::fixed) << setprecision(2) << "(" << x0 << "," << y0 << ")" << endl;
26 }
27 void Move(double x, double y)
28 {
29 x0 = x0 + x;
30 y0 = y0 + y;
31 output();
32 }
33 void Rotation(double theta)
34 {
35 MatrixXd R(3, 3);
36 MatrixXd p(1, 3);
37 MatrixXd result(1, 3);
38 double theta1;
39 theta1 = theta * (2 * PI) / 360;
40 R << cos(theta1), sin(theta1), 0,
41 -sin(theta1), cos(theta1), 0,
42 0, 0, 1;
43 p << x0, y0, 1;
44 result = p*R;
45 x0 = result(0, 0);
46 y0 = result(0, 1);
47 output();
48 } //三个类的函数没写
49 private:
50 double x0, y0;//为什么定义成私有的
51 };
52 class Line : public Shape
53 {
54 public:
55 void Move(double x, double y)
56 {
57 p1.Move(x, y);
58 p2.Move(x, y);
59 }
60 void virtual Rotation(double theta)
61 {
62 p1.Rotation(theta);
63 p2.Rotation(theta);
64 }
65 private:
66 Point p1;
67 Point p2;
68 };
69 class Triangle : public Shape
70 {
71 public:
72 void Move(double x, double y)
73 {
74 p1.Move(x, y);
75 p2.Move(x, y);
76 p3.Move(x, y);
77 }
78 void virtual Rotation(double theta)
79 {
80 p1.Rotation(theta);
81 p2.Rotation(theta);
82 p3.Rotation(theta);
83 }
84 private:
85 Point p1;
86 Point p2;
87 Point p3;
88 };
89
90 class Command
91 {
92 public:
93 string Com;
94 char Obj[10];
95 double x[2];
96 };
97
98 int main()
99 {
100 char Name_Temp[10];
101 int Point_Count;
102 char Ctinue[1];
103
104 Point** P;
105 P = new Point*[10];
106 Line** L;
107 L = new Line*[10];
108 Triangle** T;
109 T = new Triangle*[10];
110 while (true)
111 {
112 cout << "请输入图形名称、点数、坐标" << endl;
113 cin >> Name_Temp >> Point_Count;
114 int i = 0, j = 0, k = 0;
115 switch (Point_Count)
116 {
117 case 1:
118 {
119 P[i] = new Point();
120 for (int l = 0; l<10; l++)
121 {
122 P[i]->Name[l] = Name_Temp[l];
123 }
124 cout << "Success" << endl;
125 i++;
126 break;
127 }
128 case 2:
129 {
130 L[j] = new Line();
131 for (int l = 0; l<10; l++)
132 {
133 L[j]->Name[l] = Name_Temp[l];
134 }
135 cout << "Success" << endl;
136 j++;
137 break;
138 }
139 case 3:
140 {
141 T[k] = new Triangle();
142 for (int l = 0; l<10; l++)
143 {
144 T[k]->Name[l] = Name_Temp[l];
145 }
146 cout << "Success" << endl;
147 k++;
148 break;
149 }
150 }
151 cout << "继续输入?" << endl;
152 cin >> Ctinue;
153 if (Ctinue[0] == 'N')
154 break;
155 else
156 continue;
157 }
158 while(true)
159 {
160 cout << "请输入指令:" << endl;
161 Command C;
162 cin >> C.Com >> C.Obj;
163 int m = 0;
164 while ((cin.get() != '
') && m<2)
165 {
166 cin >> C.x[m];
167 m++;
168 }
169 bool found = 0;
170 for (int i = 0; i<10;i++)
171 {
172 if (!strcmp(C.Obj, P[i]->Name))
173 {
174 if (C.Com == "Move")
175 {
176 cout<<"平移后坐标"<<endl;
177 P[i]->Move(C.x[0], C.x[1]);
178 }
179 else if (C.Com == "Rotation")
180 {
181 cout<<"旋转后坐标"<<endl;
182 P[i]->Rotation(C.x[0]);
183 }
184 else
185 cout << "指令不存在";
186 break;
187 }
188 else if (!strcmp(C.Obj, L[i]->Name))
189 {
190 if (C.Com == "Move")
191 {
192 cout<<"平移后坐标"<<endl;
193 L[i]->Move(C.x[0], C.x[1]);
194 }
195
196 else if (C.Com == "Rotation")
197 {
198 cout<<"旋转后坐标"<<endl;
199 L[i]->Rotation(C.x[0]);
200 }
201
202 else
203 cout << "指令不存在";
204 break;
205 }
206 else if (!strcmp(C.Obj, T[i]->Name))
207 {
208 if (C.Com == "Move")
209 {
210 cout<<"平移后坐标"<<endl;
211 T[i]->Move(C.x[0], C.x[1]);
212 }
213
214 else if (C.Com == "Rotation")
215 {
216 cout<<"旋转后坐标"<<endl;
217 T[i]->Rotation(C.x[0]);
218 }
219 else
220 cout << "指令不存在";
221 break;
222 }
223 }
224 cout << "继续输入?" << endl;
225 cin >> Ctinue;
226 if (Ctinue[0] == 'N')
227 break;
228 else
229 continue;
230 }
231 system("pause");
232 return 0;
233 }
![](https://images2015.cnblogs.com/blog/843772/201511/843772-20151130231959780-2106616793.jpg)
还有一些小bug没改。