圆扇形区域内孩子的最大尺寸

时间:2019-09-30 08:18:47

标签: java algorithm user-interface libgdx scene2d

说明

Visual aid 1 Visual aid 2 Visual aid 3

给出以下参数:

  • 外半径
  • 内半径
  • 地区数量
  • 起始角度(第二张图片始于180度)
  • 绘制角度(第二张图片从其起始角度总共绘制了320度)

如何计算要绘制的图像的最大尺寸,以使其保持在其扇形内?在左图中,我只是使用一个硬编码的值,因为我还没有专门考虑到​​一个好的,通用的数学方程式。

如果我决定进一步扩展我的库,那么更多的变量最终可能会发挥作用。诸如分隔符的宽度或周长的轮廓之类的东西。


代码

这是我用来绘制除儿童演员以外的所有对象的东西:

/**
 * 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);
    }
}

这就是我布置那些孩子的方式:

@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);
    }
}

1 个答案:

答案 0 :(得分:0)

这就是我最终要做的事情:

/**
 * 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);
}