1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
| #include <cmath>
class Renderer { public: virtual ~Renderer() = default; virtual void drawLine(double x1, double y1, double x2, double y2) const = 0; virtual void drawCircle(double x, double y, double radius) const = 0; virtual void drawRectangle(double x, double y, double width, double height) const = 0; virtual void setColor(const string& color) const = 0; };
class ConsoleRenderer : public Renderer { private: mutable string currentColor = "黑色"; public: void drawLine(double x1, double y1, double x2, double y2) const override { cout << "[控制台] 绘制" << currentColor << "线段: (" << x1 << "," << y1 << ") -> (" << x2 << "," << y2 << ")" << endl; } void drawCircle(double x, double y, double radius) const override { cout << "[控制台] 绘制" << currentColor << "圆形: 中心(" << x << "," << y << "), 半径=" << radius << endl; } void drawRectangle(double x, double y, double width, double height) const override { cout << "[控制台] 绘制" << currentColor << "矩形: 位置(" << x << "," << y << "), 大小=" << width << "x" << height << endl; } void setColor(const string& color) const override { currentColor = color; cout << "[控制台] 设置颜色为: " << color << endl; } };
class SVGRenderer : public Renderer { private: mutable string currentColor = "black"; mutable vector<string> svgElements; public: void drawLine(double x1, double y1, double x2, double y2) const override { string element = "<line x1=\"" + to_string(x1) + "\" y1=\"" + to_string(y1) + "\" x2=\"" + to_string(x2) + "\" y2=\"" + to_string(y2) + "\" stroke=\"" + currentColor + "\" />"; svgElements.push_back(element); cout << "[SVG] 添加线段元素" << endl; } void drawCircle(double x, double y, double radius) const override { string element = "<circle cx=\"" + to_string(x) + "\" cy=\"" + to_string(y) + "\" r=\"" + to_string(radius) + "\" fill=\"none\" stroke=\"" + currentColor + "\" />"; svgElements.push_back(element); cout << "[SVG] 添加圆形元素" << endl; } void drawRectangle(double x, double y, double width, double height) const override { string element = "<rect x=\"" + to_string(x) + "\" y=\"" + to_string(y) + "\" width=\"" + to_string(width) + "\" height=\"" + to_string(height) + "\" fill=\"none\" stroke=\"" + currentColor + "\" />"; svgElements.push_back(element); cout << "[SVG] 添加矩形元素" << endl; } void setColor(const string& color) const override { currentColor = color; cout << "[SVG] 设置颜色为: " << color << endl; } void exportSVG(const string& filename) const { cout << "\n导出SVG文件: " << filename << endl; cout << "<svg xmlns=\"http://www.w3.org/2000/svg\">" << endl; for (const auto& element : svgElements) { cout << " " << element << endl; } cout << "</svg>" << endl; } void clearElements() const { svgElements.clear(); cout << "[SVG] 清空所有元素" << endl; } };
class Drawable { protected: double x, y; string color; public: Drawable(double x, double y, const string& c) : x(x), y(y), color(c) {} virtual ~Drawable() = default; virtual void draw(const Renderer& renderer) const = 0; virtual double getArea() const = 0; virtual void move(double dx, double dy) { x += dx; y += dy; } virtual void displayInfo() const { cout << "位置: (" << x << "," << y << "), 颜色: " << color << endl; } double getX() const { return x; } double getY() const { return y; } string getColor() const { return color; } void setColor(const string& c) { color = c; } };
class DrawableCircle : public Drawable { private: double radius; public: DrawableCircle(double x, double y, double r, const string& c) : Drawable(x, y, c), radius(r) {} void draw(const Renderer& renderer) const override { renderer.setColor(color); renderer.drawCircle(x, y, radius); } double getArea() const override { return M_PI * radius * radius; } void displayInfo() const override { cout << "圆形 - "; Drawable::displayInfo(); cout << "半径: " << radius << ", 面积: " << getArea() << endl; } double getRadius() const { return radius; } void setRadius(double r) { if (r > 0) radius = r; } };
class DrawableRectangle : public Drawable { private: double width, height; public: DrawableRectangle(double x, double y, double w, double h, const string& c) : Drawable(x, y, c), width(w), height(h) {} void draw(const Renderer& renderer) const override { renderer.setColor(color); renderer.drawRectangle(x, y, width, height); } double getArea() const override { return width * height; } void displayInfo() const override { cout << "矩形 - "; Drawable::displayInfo(); cout << "尺寸: " << width << "x" << height << ", 面积: " << getArea() << endl; } double getWidth() const { return width; } double getHeight() const { return height; } };
class DrawingCanvas { private: vector<unique_ptr<Drawable>> shapes; public: void addShape(unique_ptr<Drawable> shape) { shapes.push_back(move(shape)); } void drawAll(const Renderer& renderer) const { cout << "\n绘制画布上的所有图形:" << endl; for (const auto& shape : shapes) { shape->draw(renderer); } } void displayAllInfo() const { cout << "\n画布上的图形信息:" << endl; for (size_t i = 0; i < shapes.size(); ++i) { cout << (i + 1) << ". "; shapes[i]->displayInfo(); } } double getTotalArea() const { double total = 0; for (const auto& shape : shapes) { total += shape->getArea(); } return total; } void moveAll(double dx, double dy) { cout << "\n移动所有图形 (" << dx << "," << dy << ")" << endl; for (auto& shape : shapes) { shape->move(dx, dy); } } size_t getShapeCount() const { return shapes.size(); } };
void exercise1() { cout << "\n=== 练习1:图形渲染系统 ===" << endl; ConsoleRenderer consoleRenderer; SVGRenderer svgRenderer; DrawingCanvas canvas; canvas.addShape(make_unique<DrawableCircle>(100, 100, 50, "红色")); canvas.addShape(make_unique<DrawableRectangle>(200, 150, 80, 60, "蓝色")); canvas.addShape(make_unique<DrawableCircle>(300, 200, 30, "绿色")); canvas.displayAllInfo(); cout << "总面积: " << canvas.getTotalArea() << endl; cout << "\n=== 控制台渲染 ===" << endl; canvas.drawAll(consoleRenderer); cout << "\n=== SVG渲染 ===" << endl; canvas.drawAll(svgRenderer); const SVGRenderer* svgPtr = &svgRenderer; svgPtr->exportSVG("drawing.svg"); canvas.moveAll(10, -5); cout << "\n移动后的图形信息:" << endl; canvas.displayAllInfo(); cout << "\n函数结束,开始析构..." << endl; }
|