0

我有一系列形状对象,可以检查它们是否彼此相交(如果任一对象的任何部分与另一个对象重叠,则相交为真)。这对双重分派很有用,因为两个对象的交集可以相对于任何一个对象表示,并且仍然有效且真实。

我最近添加(读作:“尝试添加”)一个 Contains 方法,用于测试一个形状是否完全包含另一个形状。双重分派失败,因为两种不同大小的形状适用于 Intersect 但不适用于 Contains:shape_one是一个大圆圈,shape_two是一个完全由 包围的小圆圈shape_one。该调用shape_one.Contains(shape_two)将返回 true,但shape_two.Contains(shape_one)将返回 false。

有没有办法为这种类型的场景实现双重调度?

(鼓励编辑标题以增加对问题的理解......)

具有双重调度的 Intersects 方法示例:

bool Rectangle::Intersects(const Shape& shape) const {
    return shape.Intersects(*this);
}

bool Rectangle::Intersects(const Point& point) const {
    return point.Intersects(*this);
}

bool Rectangle::Intersects(const Line& line) const {
    return line.Intersects(*this);
}

bool Rectangle::Intersects(const Rectangle& rectangle) const {
    double myTop = this->GetY();
    double myLeft = this->GetX();
    double myRight = myLeft + this->GetWidth();
    double myBottom = myTop + this->GetHeight();
    double rTop = rectangle.GetY();
    double rLeft = rectangle.GetX();
    double rRight = rLeft + rectangle.GetWidth();
    double rBottom = rTop + rectangle.GetHeight();

    if(myTop > rBottom) return false;
    if(myBottom < rTop) return false;
    if(myLeft > rRight) return false;
    if(myRight < rLeft) return false;

    return true;
}

具有双重分派的 Contains 方法示例:

//THIS RESULTS IN A STACK OVERFLOW! DUE TO INFINITE RECURSION!
bool Rectangle::Contains(const Shape& shape) const {
    return this->Contains(shape);
}

//THIS IS NOT TRUE IN ALL CASES!
bool Rectangle::Contains(const Shape& shape) const {
    return shape.Contains(*this);
}


bool Rectangle::Contains(const Point& point) const {
    return this->Intersects(point);
}

bool Rectangle::Contains(const Line& line) const {
    return this->Intersects(line.GetPointOne()) && this->Intersects(line.GetPointTwo());
}

bool Rectangle::Contains(const Rectangle& rectangle) const {
    return this->Intersects(rectangle.GetTopLeft()) && this->Intersects(rectangle.GetTopRight()) && this->Intersects(rectangle.GetBottomLeft()) && this->Intersects(rectangle.GetBottomRight());
}
4

1 回答 1

0

通过推送包含方法解决,直到基类和动态转换为正确的类型,并在 base-to-base Contains 调用中使用类型参数:

bool Shape::Contains(const Shape& shape) const {
    std::string type = shape.GetShapeType();
    if(type == "arc") {
        return this->Contains(dynamic_cast<const Arc&>(shape));
    } else if(type == "circle") {
        return this->Contains(dynamic_cast<const Circle&>(shape));
    } else if(type == "ellipse") {
        return this->Contains(dynamic_cast<const Ellipse&>(shape));
    } else if(type == "line") {
        return this->Contains(dynamic_cast<const Line&>(shape));
    } else if(type == "point") {
        return this->Contains(dynamic_cast<const Point&>(shape));
    } else if(type == "polygon") {
        return this->Contains(dynamic_cast<const Polygon&>(shape));
    } else if(type == "rectangle") {
        return this->Contains(dynamic_cast<const Rectangle&>(shape));
    } else if(type == "sector") {
        return this->Contains(dynamic_cast<const Sector&>(shape));
    } else if(type == "spline") {
        return this->Contains(dynamic_cast<const Spline&>(shape));
    } else if(type == "triangle") {
        return this->Contains(dynamic_cast<const Triangle&>(shape));
    } else {
        return false;
    }
}

bool Shape::Contains(const Point& point) const {
    return this->Intersects(point);
}

bool Shape::Contains(const Line& line) const {
    return this->Intersects(line.GetPointOne()) && this->Intersects(line.GetPointTwo());
}

bool Shape::Contains(const Rectangle& rectangle) const {
    return this->Intersects(rectangle.GetTopLeft()) && this->Intersects(rectangle.GetTopRight()) && this->Intersects(rectangle.GetBottomLeft()) && this->Intersects(rectangle.GetBottomRight());
}

//...etc
于 2013-02-05T00:01:55.473 回答