1、设计简单几何工厂类,支持点、折线、圆形、矩形、三角形、多边线等几何形状。要求: (1)几何支持求面积、周长、判断是否包含给定点、几何类型、判断两个几何是否相交等 操作; (2)支持下列简单几何文本格式: Circle centerx entery radius Rectangle xmin ymin xmax ymax Triangle ax ay bx by cx cy
时间: 2023-05-10 14:56:06 浏览: 115
(三角形、正方形和圆形)几何形状分类数据集
5星 · 资源好评率100%
我可以为您提供一个简单的几何工厂类的设计,如下所示:
class GeometryFactory {
public:
enum GeometryType {
POINT,
LINE,
CIRCLE,
RECTANGLE,
TRIANGLE,
POLYGON
};
class Geometry {
public:
virtual double area() const = 0;
virtual double perimeter() const = 0;
virtual bool contains(const Point& point) const = 0;
virtual GeometryType type() const = 0;
virtual bool intersects(const Geometry& other) const = 0;
};
class Point : public Geometry {
public:
Point(double x, double y);
double area() const override;
double perimeter() const override;
bool contains(const Point& point) const override;
GeometryType type() const override;
bool intersects(const Geometry& other) const override;
private:
double x_;
double y_;
};
class Line : public Geometry {
public:
Line(const std::vector<Point>& points);
double area() const override;
double perimeter() const override;
bool contains(const Point& point) const override;
GeometryType type() const override;
bool intersects(const Geometry& other) const override;
private:
std::vector<Point> points_;
};
class Circle : public Geometry {
public:
Circle(double center_x, double center_y, double radius);
double area() const override;
double perimeter() const override;
bool contains(const Point& point) const override;
GeometryType type() const override;
bool intersects(const Geometry& other) const override;
private:
double center_x_;
double center_y_;
double radius_;
};
class Rectangle : public Geometry {
public:
Rectangle(double x_min, double y_min, double x_max, double y_max);
double area() const override;
double perimeter() const override;
bool contains(const Point& point) const override;
GeometryType type() const override;
bool intersects(const Geometry& other) const override;
private:
double x_min_;
double y_min_;
double x_max_;
double y_max_;
};
class Triangle : public Geometry {
public:
Triangle(const Point& a, const Point& b, const Point& c);
double area() const override;
double perimeter() const override;
bool contains(const Point& point) const override;
GeometryType type() const override;
bool intersects(const Geometry& other) const override;
private:
Point a_;
Point b_;
Point c_;
};
class Polygon : public Geometry {
public:
Polygon(const std::vector<Point>& points);
double area() const override;
double perimeter() const override;
bool contains(const Point& point) const override;
GeometryType type() const override;
bool intersects(const Geometry& other) const override;
private:
std::vector<Point> points_;
};
static std::unique_ptr<Geometry> createGeometry(const std::string& text);
};
这个几何工厂类支持点、折线、圆形、矩形、三角形、多边线等几何形状,并且支持求面积、周长、判断是否包含给定点、几何类型、判断两个几何是否相交等操作。它还支持下列简单几何文本格式:
Circle centerx entery radius
Rectangle xmin ymin xmax ymax
Triangle ax ay bx by cx cy
您可以使用 createGeometry() 方法来创建一个几何对象,该方法将接受一个简单几何文本格式的字符串作为参数,并返回一个指向 Geometry 对象的 unique_ptr。
阅读全文