gpt4 book ai didi

将屏幕坐标转换为数组网格坐标

转载 作者:行者123 更新时间:2023-11-30 16:45:17 25 4
gpt4 key购买 nike

我正在使用 ANSI C 并希望将屏幕上的 borland 图形坐标(即 (35,134))转换为网格坐标(0,0)。我的网格创建函数如下:

void createGrid(int ***a, int m, int n)
{
int i,j,color=0;

*a=(int**)malloc(sizeof(int)*n); //reserves memory for n

for(i=0;i<n;i++){ //external loop
*(*a+i)=(int*)malloc(sizeof(int)*m); //reserves memory for m
if(a){
for(j=0;j<m;j++){ //internal loop
*(*(*a+i)+j)=color;
}
}
}
}

正如你所看到的,它是一个动态网格,我一直无法找到正确的转换方法。我所能找到的只是从数组(0,1)到单个索引的转换方法。此时,我使用的网格大小为 30X30。我的鼠标跟随屏幕上的坐标,我需要将这些坐标转换为网格位置,以便可以存储颜色并从这样的位置读取颜色。

最佳答案

好吧,经过大量的阅读和探索,我发现这个困境的答案是采用(坐标-0后的空格数)/单元格的大小。就我之前的公式而言,它是 (52-32)/10=2。此公式适用于 x 和 y 坐标,将它们转换为二维数组的 i 和 j 索引。

关于将屏幕坐标转换为数组网格坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44164603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com