强制调整大小为 8 的奇数倍

发布于 2024-12-04 04:55:16 字数 366 浏览 2 评论 0原文

我有几个相互嵌套的分割面板。问题是,我正在中央面板内渲染 8x8 平铺游戏。基本上,面板的高度和宽度需要是 8 的奇数倍,这样我才能轻松找到显示的中心图块。

我正在使用 VB.net,所以所有 .net 解决方案都是可以接受的:)

编辑 抱歉,这有点令人困惑...

我的意思是,我需要宽度和高度能被 8 整除。数字 8 乘以应该是奇数:

再次编辑下面的这些数字并不涉及尺寸。它们指的是两个数字相乘。我已将它们更改为 * 以显示这一点。下面的这些数字适用于高度和宽度。一个数字应该是奇数,另一个数字应该是 8。 8*x

5*8 - 好

6*8 - 坏

I have a couple of split panels nested inside of each other. The problem is, I'm rendering an 8x8 tiled game inside of the center panel. Basically, the height and width of the panel need to be an odd multiples of 8 so I can find the center tile displayed easily.

I'm using VB.net so all .net solutions are acceptable :)

EDIT sorry, that was confusing a bit...

I mean, I need the width and height to be divisible by 8. The number 8 is multiplied by should be odd:

EDIT AGAIN these numbers below do not refer to the size. They refer to two number being multiplied. I've changed them to a * to show this. These numbers below apply to both the height and width. One number should be odd, the other 8. 8*x

5*8 - Good

6*8 - Bad

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

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

发布评论

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

评论(2

蓝礼 2024-12-11 04:55:16

您可以通过对数字执行 mod 2 来检查是否有奇数。所以就这样做

if number mod 2 == 1:
   code for board

You can check if something is odd by doing mod 2 to the number. So just do

if number mod 2 == 1:
   code for board
韬韬不绝 2024-12-11 04:55:16

您说过您需要高度和宽度都可以被 8 整除,但在您的示例中只有高度可以被 8 整除。无论如何,这是一种方法:

将其放入调整大小事件处理程序中

Dim Height as Integer = SplitControl1.Panel1.Width
    If Height mod 8 <> 0 then
  Height -= (Height mod 8)
End If

Height += 9 //This ensures that the Height is not 0 and still is divisible by 8 + 1 (to be odd)

最后

Dim Width as Integer = SplitControl1.Panel1.Width
If Width mod 8 <> 0 then
   Width -= (Width mod 8)
End If

Width += 9 //This ensures that the Width is not 0 and still is divisible by 8 + 1 (to be odd)

SplitControl1.Panel1.Width = Width
SplitControl1.Panel1.Height = Height

You stated that you need both the height and the width be divisable by 8 but in your example only height is divisible by it. anyway here's one way to do it:

place this into a resize event handler:

Dim Height as Integer = SplitControl1.Panel1.Width
    If Height mod 8 <> 0 then
  Height -= (Height mod 8)
End If

Height += 9 //This ensures that the Height is not 0 and still is divisible by 8 + 1 (to be odd)

and

Dim Width as Integer = SplitControl1.Panel1.Width
If Width mod 8 <> 0 then
   Width -= (Width mod 8)
End If

Width += 9 //This ensures that the Width is not 0 and still is divisible by 8 + 1 (to be odd)

finally

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