我创建了一个程序,可以根据黑白位图将鼠标限制在特定区域。该程序按原样 100% 正常运行,但使用了一种不准确但速度很快的算法,用于在鼠标偏离区域时重新定位鼠标。
目前,当鼠标移出区域时,基本上是这样的:
- 在区域内预定义的静态点和鼠标的新位置之间绘制一条线。
- 找到该线与允许区域边缘相交的点。
- 鼠标移动到那个点。
这行得通,但只适用于完美一个完美的圆,预定义的点设置在精确的中心。不幸的是,情况永远不会如此。该应用程序将用于各种矩形和不规则的无定形形状。在此类形状上,绘制的线与边缘相交的点通常不是形状上离鼠标最近的点。
我需要创建一个新算法来找到最接近 鼠标在允许区域边缘的新位置的点。我怎样才能做到这一点?优选地,该方法应该能够足够快地执行,以便在将鼠标拖动到区域边缘时提供平滑的鼠标移动。
(我在 OS X 10.7 上的 Objective C/Cocoa 中执行此操作,但是,如果您不想输入代码或不知道 Objective C/C,伪代码也可以)
谢谢!
这是我目前的算法:
#import <Cocoa/Cocoa.h>
#import "stuff.h"
#import <CoreMedia/CoreMedia.h>
bool
is_in_area(NSInteger x, NSInteger y, NSBitmapImageRep *mouse_mask){
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSUInteger pixel[4];
[mouse_mask getPixel:pixel atX:x y:y];
if(pixel[0]!= 0){
[pool release];
return false;
}
[pool release];
return true;
}
CGEventRef
mouse_filter(CGEventTapProxy proxy, CGEventType type, CGEventRef event, NSBitmapImageRep *mouse_mask) {
CGPoint point = CGEventGetLocation(event);
float tX = point.x;
float tY = point.y;
if( is_in_area(tX,tY, mouse_mask)){
// target is inside O.K. area, do nothing
}else{
CGPoint target;
//point inside restricted region:
float iX = 600; // inside x
float iY = 500; // inside y
// delta to midpoint between iX,iY and tX,tY
float dX;
float dY;
float accuracy = .5; //accuracy to loop until reached
do {
dX = (tX-iX)/2;
dY = (tY-iY)/2;
if(is_in_area((tX-dX),(tY-dY),mouse_mask)){
iX += dX;
iY += dY;
} else {
tX -= dX;
tY -= dY;
}
} while (abs(dX)>accuracy || abs(dY)>accuracy);
target = CGPointMake(roundf(tX), roundf(tY));
CGDisplayMoveCursorToPoint(CGMainDisplayID(),target);
}
return event;
}
int
main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
stuff *stuff_doer = [[stuff alloc] init];
NSBitmapImageRep *mouse_mask= [stuff_doer get_mouse_mask];
CFRunLoopSourceRef runLoopSource;
CGEventMask event_mask;
event_mask = CGEventMaskBit(kCGEventMouseMoved) | CGEventMaskBit(kCGEventLeftMouseDragged) | CGEventMaskBit(kCGEventRightMouseDragged) | CGEventMaskBit(kCGEventOtherMouseDragged);
CGSetLocalEventsSuppressionInterval(0);
CFMachPortRef eventTap = CGEventTapCreate(kCGHIDEventTap, kCGHeadInsertEventTap, 0, event_mask, mouse_filter, mouse_mask);
if (!eventTap) {
NSLog(@"Couldn't create event tap!");
exit(1);
}
runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
CGEventTapEnable(eventTap, true);
CFRunLoopRun();
CFRelease(eventTap);
CFRelease(runLoopSource);
[pool release];
exit(0);
}
这是可能使用的区域位图的示例,黑色是允许的区域。 这说明了为什么转换为多边形不方便,甚至不合理。
最佳答案
如果用户正在移动鼠标指针,需要限制在某个区域内,那么我认为最好的解决方案不是找到区域内最近的点。相反,对用户来说更直观的做法是将鼠标退出时放回有效区域。如果您能够以足够快的速度监控鼠标位置,这将更容易实现。
现在,您可能有理由按照自己的方式去做,我尊重这一点。在这种情况下,我可以提出以下想法:
如果您可以将有效鼠标区域的定义方式从位图更改为多边形(标记区域角点的二维点列表),那么任务就会变得简单得多。只需找到最接近鼠标位置的段即可。作为该计算的一部分,您可以获得该段内的最近点,这就是您要重新定位鼠标指针的位置。
蛮力解决方案也应该有效。从当前鼠标位置开始,向外移动。首先检查它周围的八个像素。然后是 8 周围的 16,依此类推。找到有效区域内的点后,记录它到当前鼠标位置的距离。继续前进,继续寻找更近的像素,或者直到当前外层中所有像素的距离都大于您记录的最小距离。
希望对您有所帮助。
关于objective-c - 如何确定某个形状上离鼠标最近的点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8222825/