均匀划分范围,留出空间和边距

发布于 2024-12-19 21:00:09 字数 1004 浏览 2 评论 0原文

我有一个点阵列和一个面板。 getWidth() 基于面板。想象一下您手中的牌,因此 HAND_CARD_WIDTH 是非常不言自明的。该循环的目的是在面板上均匀地设置点,它确实做到了。然而,它允许卡片从面板中伸出,这使得它看起来非常糟糕。我想做的是,无论你手里有多少张牌,在面板的两侧都留出一小块空白。

这是代码

        int iteration = 1;
        int limiter = getWidth();
        int slice = (limiter/(handsize+1));

        while (points.size() < handsize) {
            int x = slice*(iteration++);
            x -= HAND_CARD_WIDTH/2;
            points.add(new Point(x, y));    
        }

基本上我希望最左边的x至少为20,最右边的最多为getWidth() - 20 - HAND_CARD_WIDTH。我还希望卡片均匀分布......我只是想不出正确的方程式(遗憾的是达到这一点是一项壮举......)。

谢谢,根据回复(全部 2 个),我的做法是:

        if((int)points.get(0).getX() < margin){
            int boost = Math.abs(margin - (int)points.get(0).getX());
            slice = (boost*2)/handsize;
            for(Point p : points){
                p.x += boost;
                boost -= slice;
            }
        }

I have a point array and a panel. getWidth() is based off the panel. Imagine cards in your hand, so HAND_CARD_WIDTH is pretty self explanatory. The purpose of this loop is to set the points evenly across the panel, which it does. However, it allow the cards to go out of the panel, which makes it look very bad. What I want to do is give a small empty margin on both sides of the panel no matter how many cards you have in your hand.

Here's the code

        int iteration = 1;
        int limiter = getWidth();
        int slice = (limiter/(handsize+1));

        while (points.size() < handsize) {
            int x = slice*(iteration++);
            x -= HAND_CARD_WIDTH/2;
            points.add(new Point(x, y));    
        }

Basically I want the leftmost x to be at least 20 and the rightmost to be at most getWidth() - 20 - HAND_CARD_WIDTH. I also want the cards to be evenly spaced... I just can't think of the right equation (getting to this point was sadly a feat..).

Thanks, based on the responses (all 2 of them) heres what I went with:

        if((int)points.get(0).getX() < margin){
            int boost = Math.abs(margin - (int)points.get(0).getX());
            slice = (boost*2)/handsize;
            for(Point p : points){
                p.x += boost;
                boost -= slice;
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玻璃人 2024-12-26 21:00:09

不确定我是否理解你的布局,但我认为它是这样的:

|边距|空格|卡|空格|卡|空格|边距|

或者

|边距|空格|卡片|空格|边距|

因此,空格的数量比卡片的数量多一,总宽度是组件宽度减去边距。 componentWidth = numCards x cardWidth + (numCards + 1) x spaceWidth 现在很容易计算出所需的空间,即 (componentWidth - numCards x cardWidth) / (numCards + 1) > 因此卡片的左侧位置为 leftMargin + spaceWidth x cardNum + cardWidth x (cardNum - 1)

当 space 为负数时必须小心,则相反,您必须计算卡片必须重叠的程度。

Not sure if I understand your layout, but I think it is something like this:

|margin|space|card|space|card|space|margin|

or

|margin|space|card|space|margin|

So, the number of spaces is one more than number of cards and the total width is component width minus margins. componentWidth = numCards x cardWidth + (numCards + 1) x spaceWidth Now it is easy to calculate the space needed which is (componentWidth - numCards x cardWidth) / (numCards + 1) so the left position of a card is leftMargin + spaceWidth x cardNum + cardWidth x (cardNum - 1)

Care must be taken when the space is negative, then you instead must calculate how much the cards must overlap.

鲸落 2024-12-26 21:00:09

试试这个:

final int MARGIN = 20;
int availableWidth = getWidth() - 2 * MARGIN - HAND_CARD_WIDTH;
int cardSpaceWidth = availableWidth / handsize;
for (int i = 0; i < handsize; i++) {
    int x = MARGIN + ((i + 0.5) * cardSpaceWidth) - HAND_CARD_WIDTH / 2;
    points.add(new Point(x, y));
}

所以:

  • 通过从面板总宽度中减去边距来计算可用宽度,
  • 通过将每张卡的剩余空间除以卡的数量来计算每张卡的空间
  • ,我们通过取卡空间的中间并减去一半来计算其最左边的点卡的宽度。

请注意,如果 HAND_CARD_WIDTH 大于卡片的可用空间,则使用此解决方案卡片仍然可以重叠边距。

Try this:

final int MARGIN = 20;
int availableWidth = getWidth() - 2 * MARGIN - HAND_CARD_WIDTH;
int cardSpaceWidth = availableWidth / handsize;
for (int i = 0; i < handsize; i++) {
    int x = MARGIN + ((i + 0.5) * cardSpaceWidth) - HAND_CARD_WIDTH / 2;
    points.add(new Point(x, y));
}

So:

  • calculate the available width by subtracting the margins from total panel width
  • calculate the space for each card by dividing remaining space by number of cards
  • for each card, we count its leftmost point by taking the middle of card space and subtracting half the width of the card.

Mind you, with this solution cards still can overlap margins, if HAND_CARD_WIDTH is greater than available space for card.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文