#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <string>
#include <memory>
#include <stdexcept>
using namespace std;
class Shape {
public:
virtual ~Shape() = default;
virtual void show() const = 0;
virtual string getType() const = 0;
virtual double distanceTo(const Shape* other) const = 0;
};
// 前向声明Line类
class Line;
class Point : public Shape {
private:
double x, y;
public:
Point(double x = 0.0, double y = 0.0) : x(x), y(y) {}
double getX() const { return x; }
double getY() const { return y; }
virtual void show() const override {
cout << fixed << setprecision(3);
cout << "Point: (" << x << ", " << y << ")" << endl;
}
virtual string getType() const override {
return "Point";
}
// 声明distanceTo,稍后在Line类定义后实现
virtual double distanceTo(const Shape* other) const override;
};
class Line : public Shape {
private:
double a, b, c;
const double EPS = 1e-9;
public:
Line(double a, double b, double c) : a(a), b(b), c(c) {
if (abs(a) < EPS && abs(b) < EPS) {
throw invalid_argument("a and b cannot both be zero");
}
}
Line(const Point& p, double k) {
if (isinf(k)) {
a = 1;
b = 0;
c = -p.getX();
}
else {
a = k;
b = -1;
c = p.getY() - k * p.getX();
}
}
Line(const Point& p1, const Point& p2) {
if (abs(p1.getX() - p2.getX()) < EPS &&
abs(p1.getY() - p2.getY()) < EPS) {
throw invalid_argument("Points cannot be identical");
}
a = p2.getY() - p1.getY();
b = p1.getX() - p2.getX();
c = p2.getX() * p1.getY() - p1.getX() * p2.getY();
}
double getA() const { return a; }
double getB() const { return b; }
double getC() const { return c; }
virtual void show() const override {
cout << fixed << setprecision(3);
cout << "Line: " << a << "x + " << b << "y + " << c << " = 0" << endl;
}
virtual string getType() const override {
return "Line";
}
virtual double distanceTo(const Shape* other) const override {
if (!other) throw invalid_argument("Null shape pointer");
if (other->getType() == "Point") {
const Point* p = dynamic_cast<const Point*>(other);
if (!p) throw runtime_error("Dynamic cast failed");
return abs(a * p->getX() + b * p->getY() + c) / sqrt(a * a + b * b);
}
else if (other->getType() == "Line") {
const Line* l = dynamic_cast<const Line*>(other);
if (!l) throw runtime_error("Dynamic cast failed");
double delta = a * l->b - b * l->a;
if (abs(delta) < EPS) {
if (abs(c - l->c) < EPS)
return 0;
return abs(c - l->c) / sqrt(a * a + b * b);
}
throw runtime_error("distance is 0");
return 0;
}
throw invalid_argument("Unsupported shape type");
}
Point get(const Line& other) const {
double delta = a * other.b - b * other.a;
if (abs(delta) < EPS) {
throw runtime_error("Lines are parallel");
}
double x = (b * other.c - other.b * c) / delta;
double y = (other.a * c - a * other.c) / delta;
return Point(x, y);
}
};
double Point::distanceTo(const Shape* other) const {
if (!other) throw invalid_argument("Null shape pointer");
if (other->getType() == "Point") {
const Point* p = dynamic_cast<const Point*>(other);
if (!p) throw runtime_error("Dynamic cast failed");
return sqrt(pow(x - p->x, 2) + pow(y - p->y, 2));
}
else if (other->getType() == "Line") {
const Line* ll = dynamic_cast<const Line*>(other);
if (!ll) throw runtime_error("Dynamic cast failed");
return ll->distanceTo(this);
}
throw invalid_argument("Unsupported shape type");
}
class GeometrySystem {
private:
vector<unique_ptr<Shape>> shapes;
public:
void add(Shape* shape) {
if (!shape) throw invalid_argument("It's a null shape");
shapes.emplace_back(shape);
}
Shape* getShape(int n) {
if (n < 0 || n >= static_cast<int>(shapes.size())) {
throw out_of_range("Index out of range");
}
return shapes[n].get();
}
void removeShape(int n) {
if (n < 0 || n >= static_cast<int>(shapes.size())) {
throw out_of_range("Index out of range");
}
if (!shapes[n]) {
throw runtime_error("Null pointer, cannot remove");
}
shapes.erase(shapes.begin() + n);
}
void showAll() const {
cout << "===== Geometry System =====" << endl;
for (size_t i = 0; i < shapes.size(); ++i) {
cout << "[" << i << "]: ";
shapes[i]->show();
}
cout << "==========================" << endl;
}
};
int main() {
try {
GeometrySystem system;
Point p1(1.0, 2.0);
Point p2(3.0, 4.0);
Point p3(5.0, 6.0);
Point p4(2.0, 3.0);
Line l1(1.0, -1.0, 0.0); // y = x
Line l2(p1, 0.5); // 通过p1点,斜率0.5
Line l3(p2, p3); // 通过p2和p3
system.add(new Point(p1));
system.add(new Point(p2));
system.add(new Point(p3));
system.add(new Point(p4));
system.add(new Line(l1));
system.add(new Line(l2));
system.add(new Line(l3));
cout << "测试基本功能(类型、显示):\n";
Shape* s1 = system.getShape(0); // Point
Shape* s2 = system.getShape(4); // Line
cout << "Type of s1: " << s1->getType() << endl;
s1->show();
cout << "Type of s2: " << s2->getType() << endl;
s2->show();
system.showAll();
cout << "\n测试距离计算:\n";
double d1 = p1.distanceTo(&p2);
double d2 = p1.distanceTo(&l1);
// double d3 = l1.distanceTo(&l2); // 相交直线
cout << "Distance p1 - p2: " << d1 << endl;
cout << "Distance p1 - l1: " << d2 << endl;
// cout << "Distance l1 - l2: " << d3 << " (should be 0)\n";
cout << "\n测试交点计算:\n";
Point inter = l1.get(l2);
cout << "l1 & l2 交点: ";
inter.show();
cout << "\n测试系统功能:\n";
system.removeShape(1);
cout << "删除索引1后:\n";
system.showAll();
Shape* s = system.getShape(0);
cout << "索引0的图形: ";
s->show();
cout << "\n测试异常情况:\n";
try {
Line invalid(0, 0, 1);
}
catch (const exception& e) {
cout << "1. 无效直线: " << e.what() << endl;
}
try {
Line invalid(p1, p1);
}
catch (const exception& e) {
cout << "2. 重合点: " << e.what() << endl;
}
try {
Line pl1(1, 1, 0);
Line pl2(1, 1, 1);
Point p = pl1.get(pl2);
}
catch (const exception& e) {
cout << "3. 平行线交点: " << e.what() << endl;
}
try {
system.removeShape(10);
}
catch (const exception& e) {
cout << "4. 越界删除: " << e.what() << endl;
}
try {
double d3 = l1.distanceTo(&l2);
cout << "Distance l1 - l2: " << d3 << endl;
}
catch (const exception& e) {
cout << "5.直线相交: " << e.what() << endl;
}
}
catch (const exception& e) {
cerr << "错误: " << e.what() << endl;
return 1;
}
return 0;
}
Shape
Shape相关内容
于 2025-06-20 20:22:32 首次发布

被折叠的 条评论
为什么被折叠?



