简体   繁体   English

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

[英]Converting on-screen coordinates to array grid coordinates

I'm using ANSI C and looking to convert on-screen borland graphics coordinates ie (35,134) to a grid coordinate of (0,0). 我正在使用ANSI C,并希望将屏幕上的Borland图形坐标即(35,134)转换为网格坐标(0,0)。 My grid creation function is as follows: 我的网格创建功能如下:

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;
            }
        }
    }
}

As you can see, it is a dynamic grid and I have been unable to find the correct conversion method. 如您所见,它是一个动态网格,我一直无法找到正确的转换方法。 All I've been able to find is a conversion method from array(0,1) to a single index. 我所能找到的只是从array(0,1)到单个索引的转换方法。 At this time, I'm working with a grid size of 30X30. 目前,我正在使用30X30的网格。 My mouse follows on-screen coordinates and I need those coordinates to be converted to the grid position, so a color can be stored and read from such a position. 我的鼠标遵循屏幕上的坐标,因此我需要将这些坐标转换为网格位置,以便可以存储颜色并从该位置读取颜色。

Well, after a lot of reading and exploring, I found the answer to this dilemma is taking the (coordinate-number of spaces after 0)/size of the cell. 好了,经过大量的阅读和探索,我发现这个难题的答案是采用(0以后的空格的坐标数)/单元格的大小。 In the case of my previous formula, it's (52-32)/10=2. 对于我以前的公式,它是(52-32)/ 10 = 2。 This formula works for both, x and y coordinates to convert them into i & j indexes for a two-dimensional array. 此公式适用于x和y坐标,以将它们转换为二维数组的i&j索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM