在超级视图中使用任意锚点定位子视图

时间:2019-01-15 13:06:02

标签: c++ algorithm geometry

我很难找出合适的算法。我有一个超级视图(绿色)和一个子视图(黄色)。在此子视图(红色)中有一个任意的锚点(只是一个选择的点,不与图层的anchor属性混合),我必须以放置该红色点的方式放置子视图并调整子视图的大小在超级视图的中心中,如果需要,子视图会按比例按比例调整的大小(如果移动后其框架超出了超级视图的范围)。没有使用自动版式。看到图片我想要得到的。

所以基本上,我编写了一个C ++测试例程,其中在给定新点的情况下“大约”计算了一个新的比例矩形(因此,该点成为该新矩形的中心),但是我不知道下一步该怎么处理一个点。不在子视图的中心。 Rect基本上是具有x, y, width, height个成员的结构。

    Rect
    calculateRectInBounds(const Rect &boundingRect, const Rect &initialRect, const Point &translationPoint)
    {
        double boundsWidth = boundingRect.getWidth();
        double boundsHeight = boundingRect.getHeight();
        double distanceX, distanceY;
        Size sizeByWidth, sizeByHeight;

        // detect part where point is

        /*
         * UL | UR
         * ---C---
         * LL | LR
         */

        // -- upper left - check left & top borders

        if (translationPoint.x <= boundsWidth / 2 && translationPoint.y < boundsHeight / 2) {
            distanceX = translationPoint.x;
            distanceY = translationPoint.y;

            sizeByWidth = scaleSizeToWidth(initialRect.getSize(), distanceX);
            sizeByHeight = scaleSizeToHeight(initialRect.getSize(), distanceY);

            if (sizeByWidth.height > distanceY) {
                return {translationPoint.x - sizeByHeight.width,
                        0,
                        sizeByHeight.width * 2,
                        sizeByHeight.height * 2};
            }

            return {0,
                    translationPoint.y - sizeByWidth.height,
                    sizeByWidth.width * 2,
                    sizeByWidth.height * 2};
        }

        // -- upper right - check top & right borders

        if (translationPoint.x > boundsWidth / 2 && translationPoint.y <= boundsHeight / 2) {
            distanceX = boundsWidth - translationPoint.x;
            distanceY = translationPoint.y;

            sizeByWidth = scaleSizeToWidth(initialRect.getSize(), distanceX);
            sizeByHeight = scaleSizeToHeight(initialRect.getSize(), distanceY);

            if (sizeByWidth.height > distanceY) {
                return {translationPoint.x - sizeByHeight.width,
                        0,
                        sizeByHeight.width * 2,
                        sizeByHeight.height * 2};
            }

            return {translationPoint.x - sizeByWidth.width,
                    translationPoint.y - sizeByWidth.height,
                    sizeByWidth.width * 2,
                    sizeByWidth.height * 2};
        }

        // -- lower right - check right & bottom borders

        if (translationPoint.x >= boundsWidth / 2 && translationPoint.y > boundsHeight / 2) {
            distanceX = boundsWidth - translationPoint.x;
            distanceY = boundsHeight - translationPoint.y;

            sizeByWidth = scaleSizeToWidth(initialRect.getSize(), distanceX);
            sizeByHeight = scaleSizeToHeight(initialRect.getSize(), distanceY);

            if (sizeByWidth.height > distanceY) {
                return {translationPoint.x - sizeByHeight.width,
                        translationPoint.y - sizeByHeight.height,
                        sizeByHeight.width * 2,
                        sizeByHeight.height * 2};
            }

            return {translationPoint.x - sizeByWidth.width,
                    translationPoint.y - sizeByWidth.height,
                    sizeByWidth.width * 2,
                    sizeByWidth.height * 2};
        }

        // -- lower left - check bottom & left borders

        if (translationPoint.x < boundsWidth / 2 && translationPoint.y >= boundsHeight / 2) {
            distanceX = translationPoint.x;
            distanceY = boundsHeight - translationPoint.y;

            sizeByWidth = scaleSizeToWidth(initialRect.getSize(), distanceX);
            sizeByHeight = scaleSizeToHeight(initialRect.getSize(), distanceY);

            if (sizeByWidth.height > distanceY) {
                return {translationPoint.x - sizeByHeight.width,
                        translationPoint.y - sizeByHeight.height,
                        sizeByHeight.width * 2,
                        sizeByHeight.height * 2};
            }

            return {0,
                    translationPoint.y - sizeByWidth.height,
                    sizeByWidth.width * 2,
                    sizeByWidth.height * 2};
        }

        // -- center

        return initialRect;
    }

Size
scaleSizeToWidth(Size size, double newWidth)
{
    return {newWidth, (std::min(size.width, newWidth) / std::max(size.width, newWidth)) * size.height};
}

Size
scaleSizeToHeight(Size size, double newHeight)
{
    return {(std::min(size.height, newHeight) / std::max(size.height, newHeight)) * size.width, newHeight};
}

enter image description here

编辑

感谢@MBo正确的例程

    Rect
    calculateRectInBounds(const Rect &boundingRect, const Rect &initialRect, const Point &anchorPoint)
    {
        auto lDist = anchorPoint.x - initialRect.getX();
        auto rDist = initialRect.getWidth() - lDist;
        auto tDist = anchorPoint.y - initialRect.getY();
        auto bDist = initialRect.getHeight() - tDist;

        auto lRatio = (lDist * 2) / boundingRect.getWidth();
        auto rRatio = (rDist * 2) / boundingRect.getWidth();
        auto tRatio = (tDist * 2) / boundingRect.getHeight();
        auto bRatio = (bDist * 2) / boundingRect.getHeight();

        auto scale = 1 / std::max({lRatio, rRatio, tRatio, bRatio});

        auto x = initialRect.getWidth() / 2 - lDist * scale + initialRect.getX();
        auto y = initialRect.getHeight() / 2 - tDist * scale + initialRect.getY();
        auto width = initialRect.getWidth() * scale;
        auto height = initialRect.getHeight() * scale;

        return {x, y, width, height};
    }

1 个答案:

答案 0 :(得分:1)

获取从红点到子视图rect的所有边缘的距离

RedX, YellowWidth-RedX, RedY, YellowHeight - RedY

并计算具有超级视图大小的4个比率

L = (2 * RedX) / GreenWidth
R = (2 * (YellowWidth-RedX)) / GreenWidth
T = (2 * RedY) / GreenHeight
B = (2 * (YellowHeight-RedY)) / GreenHeight

然后找到最多的

Mx = Max(L,R,T,B)

现在确定规模:

Scale = 1 / Mx 

新子视图坐标:

X =  YellowWidth / 2  - (RedX * Scale)
Y =  YellowHeight / 2  - (RedY * Scale)