c# - 如何绘制从现有网格中心偏移的对象?
问题描述
我正在尝试通过更改节点的颜色在已经处于活动状态的瓷砖网格上绘制图像。网格是用双 for 循环绘制的,我有一个适合该区域的图像。目前,图像平铺现有空间,但我希望它居中。
如何使用现有的 for 循环定位图像?
for (int x = 0; x < maxSize; x++)
{
for (int y = 0; y < maxSize; y++)
{
Node tile = new Node();
tile.x = x;
tile.z = y;
// copyImage.GetPixel(y, x) - The image being copied onto the grid
tile.currentColor = ColorUtility.ToHtmlStringRGB(Color.white);
Nodes.Add(tile);
}
}
解决方案
经过大量测试,我确实设法解决了这个问题。为了使图像居中而不是平铺,它需要正确的偏移量以及图像限制:
int center = maxSize / 2;
int offsetX = center - copyImage.width / 2;
int offsetY = center - copyImage.height / 2;
int maxY = offsetY + copyImage.height;
int maxX = offsetX + copyImage.width;
for (int x = 0; x < maxSize; x++)
{
for (int y = 0; y < maxSize; y++)
{
Node tile = new Node();
tile.x = x;
tile.z = y;
bool yTrue = y > offsetY && y < maxY;
bool xTrue = x > offsetX && x < maxX;
if (yTrue && xTrue)
{
tile.currentColor = ColorUtility.ToHtmlStringRGB(copyImage.GetPixel(x - offsetX, y - offsetY));
} else {
tile.currentColor = ColorUtility.ToHtmlStringRGB(Color.white);
}
tile.currentColor = ColorUtility.ToHtmlStringRGB(Color.white);
Nodes.Add(tile);
}
}
推荐阅读
- python - 安装 Flask 1.0.2(Jinja2 和 Werkzeug)时的需求问题
- javascript - Node.JS 中的 JSON POST 数据序列化为数组
- c# - 不知道如何构建方法,C# WFA
- tfs - VSTS / TFS REST API - 获取工作项及其链接的项
- javascript - 如何访问第n个父节点
- javascript - 在组件中调用反应类的方法
- linux - 如何在Bash中按字母排序顺序将数字排在小数点之后
- python - Pandas df.corr - 跨多个列的一个变量
- vba - VBA - 仅用第二个“-”分隔到另一列
- matlab - 从 MATLAB 中的元胞数组为矩阵赋值的问题