首页 > 解决方案 > 通过整数除法缩放一组坐标时丢失像素

问题描述

我正在尝试制作一个程序,将鼠标移动一定数量的像素。移动的坐标设置在“dx”和“dy”整数中,并取自整数数组,例如{ -85, 75 }, { 0, 67 }, { 33, 57 }, { 58, 48 }等等 - 有很多这样的移动。

这些坐标的数组设置为鼠标灵敏度 =0,25

我需要做的是将其转换为等于鼠标灵敏度1

为了获得小 4 倍的移动,我尝试将我从数组 ( {-85, 75}) 中得到的值(整数)除以 4。

整个问题是,当我划分时,假设-85 / 4它给我留下了余数,这意味着我每次移动都会丢失像素。

它可以存储为双精度,但鼠标移动只能以整数表示,因为我们不能移动半个像素

有什么解决办法吗?如果我在每次拍摄时丢失 3 或 4 个像素,这没什么大不了的,但因为它越来越糟。

这是我的一段代码,可用于移动鼠标:

`

private void _MoveMouse()
        {
            while (appRunning)
            {
                if (Utility.bGetAsyncKeyState(Keys.LButton))
                {
                    int[,] pattern = Current_Table().Item1; //Current_Table() returns table of (x, y) coordinates in integers and sleep time in integer that is used to sleep the thread
                    int sleep = Current_Table().Item2;

                    if (_ENABLED == true)
                    {
                        for (int i = 0; i < 30; i++)
                        {
                            int dx = pattern[i, 0]/4; //leaves a remainder from X coordinates that can not be stored in integer.
                            int dy = pattern[i, 1]/4; //leaves a remainder from Y coordinates that can not be stored in integer

                            int dx_rest = dx % 4; //the remainder from X
                            int dy_rest = dy % 4; //the remainder from Y


                            //MessageBox.Show(Convert.ToString(pattern[i,0] + " " + pattern[i,1]));

                            Utility.Move(dx, dy );

                            Thread.Sleep(sleep);

                            if (Utility.bGetAsyncKeyState(Keys.LButton)== false)
                            { 
                                break;
                            }

                        }
                    }`

标签: c#integerdoublepixelmousemove

解决方案


推荐阅读