You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何调整6x6立方体内部3x3立方体的位置?

问题描述

我没法调整在6x6网格内部绘制的3x3小网格的位置:

  • 当前效果:3x3小网格位于6x6网格的左上角区域
  • 目标效果:3x3小网格位于6x6网格的中心区域
代码说明

变量定义:

  • Xstart:X轴起始位置
  • Ystart:Y轴起始位置
  • Xend:X轴结束位置
  • Yend:Y轴结束位置
using (FileStream file = new FileStream("sample.bmp", FileMode.Create, FileAccess.Write))
{
    file.Write(header);
    int l = (1000 + 31) / 32 * 4;
    var t = new byte[1000 * l];
    RecursiveFigure(t, l, 2, 1000, 1000, 0, 0);
    file.Write(t);
}
static void RecursiveFigure(byte[] t,int l, int recursiveCount, double Xend, double Yend, double Xstart, double Ystart)
{
    if (recursiveCount == 1)
    {
        CreateNet(t, l, 3, Xend, Yend, Xstart, Ystart);
        //CreateBlackBox(t, Xend, Yend, l, 3, Xstart, Ystart);
        return;
    }
    CreateNet(t, l, 6, Xend, Yend, Xstart, Ystart);
    //CreateBlackBox(t, Xend, Yend, l, 6, Xstart, Ystart);
    RecursiveFigure(t, l, recursiveCount - 1, Xend / 6, Yend / 6, 0, 0);
}
static void CreateNet(byte[] t, int l, int boxSize, double Xend, double Yend, double Xstart, double Ystart)
{
    for (int i = 0; i < boxSize; i++)
    {
        int k = Convert.ToInt16(i * Yend / boxSize); // boxSize is always 6x6, except when doing last recursion its 3x3
        int x = Convert.ToInt16(i * Xend / boxSize); // boxSize is always 6x6, except when doing last recursion its 3x3
        DrawVerticle(t, x, l, Ystart, Yend);
        DrawHorizontal(t, k, l, Xstart, Xend);
    }
}
static void DrawVerticle(byte[] t, int s, int l, double Ystart, double Yend)
{
    for (int y = (int)Ystart; y < Yend; y++)
    {
        int index = (y * l) + (s / 8);
        if (index < t.Length)
            t[index] |= (byte)(0x80 >> (s % 8));
    }
}
static void DrawHorizontal(byte[] t, int k, int l, double Xstart, double Xend)
{
    for (int x = (int)Xstart; x < Xend; x++)
    {
        int index = (k * l) + (x / 8);
        if (index < t.Length)
            t[index] |= (byte)(0x80 >> (x % 8));
    }
}
尝试过的方法
  • 尝试将绘制垂直线和水平线的方法合并为一个,用双重循环实现,但未成功(可能操作有误)。
  • 参照以下涂黑中间立方体的代码改写绘制线条的逻辑,同样无效:
for (int y = (Height / 6 / 3); y < (Height / 6 / 3) * 2; y++)
{
    for (int x = (Width / 3 + Width / 6) + (Width / 6 / 3); x < ((Width / 3) * 2) - (Width / 6) / 3; x++)
    {
        int index = y * l + x / 8;
        t[index] |= (byte)(0x80 >> (x % 8));
    }
}
诉求
  • 希望有人指出代码中的问题,或提供可行的解决方案。
  • 求推荐讲解递归绘制BMP文件结构的YouTube视频。

内容的提问来源于stack exchange,提问作者NoOne

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 09:46:12