圆扇区内 child 的最大尺寸

Maximal size for a child within a circle's sector

描述

给定以下参数:

如何计算要绘制的图像的最大尺寸以使它们保持在其扇区内?在左图中,我只使用了一个 hard-coded 值,因为我还没有特别想到一个好的、通用的数学方程式。

如果我决定进一步扩展我的图书馆,最终可能会出现更多变数。诸如分隔符或圆周轮廓的宽度之类的东西。


代码

这是我用来绘制除 children 演员之外的所有内容的方法:

/**
 * Takes care of drawing everything that {@link #layout()} didn't.
 * (Basically everything but the children Actors.)
 *
 * @param batch a Batch used to draw Drawables. The {@link #sd} is used to
 *              draw everything else.
 * @param parentAlpha
 */
protected void drawWithShapeDrawer(Batch batch, float parentAlpha) {

    /* Pre-calculating */
    float bgRadian = MathUtils.degreesToRadians*style.totalDegreesDrawn;
    float tmpOffset = MathUtils.degreesToRadians*style.startDegreesOffset;
    int size = getChildren().size;
    float tmpRad = bgRadian / size;

    /* Background image */
    if(style.background != null)
        style.background.draw(batch, getX(), getY(), getWidth(), getHeight());

    /* Rest of background */
    if(style.backgroundColor != null) {
        sd.setColor(style.backgroundColor);
        sd.sector(getX()+style.radius, getY()+style.radius, style.radius-BG_BUFFER, tmpOffset, bgRadian);
    }

    /* Children */
    vector2.set(getX()+style.radius, getY()+style.radius);
    for(int i=0; i<size; i++) {
        float tmp = tmpOffset + i*tmpRad;
        drawChildWithoutSelection(vector2, i, tmp, tmpRad);

        /* Separator */
        drawChildSeparator(vector2, tmp);
    }

    /* The remaining last separator to be drawn */
    drawChildSeparator(vector2, tmpOffset + size*tmpRad);
}

protected void drawChildSeparator(Vector2 vector2, float drawnRadianAngle) {
    if(getChildren().size > 1 && style.separatorColor != null)
        sd.line(pointAtAngle(vector22, vector2, style.innerRadius, drawnRadianAngle),
                pointAtAngle(vector23, vector2, style.radius, drawnRadianAngle),
                style.separatorColor, style.separatorWidth);
}

protected void drawChildWithoutSelection(Vector2 vector2, int index, float startAngle, float radian) {
    if(style.childRegionColor != null) {
        if(style.alternateChildRegionColor != null) {
            sd.setColor(index%2 == 0 ? style.childRegionColor : style.alternateChildRegionColor);
            sd.arc(vector2.x, vector2.y, (style.radius+style.innerRadius)/2, startAngle, radian, style.radius-style.innerRadius);
        } else {
            sd.setColor(style.childRegionColor);
            sd.arc(vector2.x, vector2.y, (style.radius+style.innerRadius)/2, startAngle, radian, style.radius-style.innerRadius);
        }
    }
    drawChildCircumference(vector2, startAngle, radian, style.radius - style.circumferenceWidth/2);
}

protected void drawChildCircumference(Vector2 vector2, float startAngle, float radian, float radius) {
    if(style.circumferenceColor != null && style.circumferenceWidth > 0) {
        sd.setColor(style.circumferenceColor);
        sd.arc(vector2.x, vector2.y, radius, startAngle, radian, style.circumferenceWidth);
    }
}

我就是这样布置的 children:

@Override
public void layout() {
    float degreesPerChild = style.totalDegreesDrawn / getChildren().size;
    float half = (float)1 / 2;
    for (int i = 0; i < getChildren().size; i++) {
        Actor actor = getChildren().get(i);
        vector2.set((style.radius+style.innerRadius)/2, 0);
        vector2.rotate(degreesPerChild*(i + half) + style.startDegreesOffset);

        if(actor instanceof Image) { // todo: do this properly !
            actor.setSize(30, 30);
        }
        actor.setPosition(vector2.x+style.radius, vector2.y+style.radius, Align.center);
    }
}

这是我最后做的事情:

/**
 * Used to estimate the radius of a circle to be constrained within the widget
 * according to the input parameters. Doubling the returned value would give
 * you the size of a contained Actor which would roughly fill most of its
 * sector, or possibly overflow slightly. It is suggested to adjust slightly
 * the returned value by multiplying it with a factor of your choice.<br>
 * It's basically the minimum between 3 different possible radius values
 * based on certain layout properties.
 *
 * @param degreesPerChild the amount of degrees that a child's sector takes.
 * @param actorDistanceFromCenter the distance at which the child Actor is
 *                                positioned from the center of the widget.
 * @return an estimated radius value for an Actor placed with the given
 *         constraints.
 */
public float getEstimatedRadiusAt(float degreesPerChild, float actorDistanceFromCenter) {
    float tmp1 = actorDistanceFromCenter * MathUtils.sinDeg(degreesPerChild/2);
    float tmp2 = getMaxRadius() - actorDistanceFromCenter;
    float tmp3 = actorDistanceFromCenter - getInnerRadiusLength();
    return Math.min(Math.min(tmp1, tmp2), tmp3);
}