Skip to content

Instantly share code, notes, and snippets.

Created January 18, 2013 00:55
Show Gist options
  • Save anonymous/4561378 to your computer and use it in GitHub Desktop.
Save anonymous/4561378 to your computer and use it in GitHub Desktop.
Prevents the point argument from going outside the bounds of the rect. This might be useful in cases where the user is dragging an object and the object need to be contained within a given rect.
NSPoint ConstrainPointInsideRect(CGPoint fp, NSRect frame)
{
NSPoint fp = fp;
NSPoint pt = NSZeroPoint;
NSRect f = frame;
if (fp.x <= NSMinX(br) && fp.y <= NSMinY(br) ) {
pt.x = br.origin.x; pt.y = br.origin.y; }
else if (fp.x <= NSMinX(br) && fp.y >= NSMaxY(br)) {
pt.x = NSMinX(br); pt.y = NSMaxY(br); }
else if (fp.x >= NSMaxX(br) && fp.y <= NSMinY(br)) {
pt.x = NSMaxX(br); pt.y = NSMinY(br); }
else if (fp.x >= NSMaxX(br) && fp.y >= NSMaxY(br)) {
pt.x = NSMaxX(br); pt.y = NSMaxY(br); }
else if (fp.x <= NSMinX(br))
pt.x = NSMinX(br);
else if (fp.x > NSMaxX(br))
pt.x = NSMaxX(br);
else if (fp.y <= NSMinY(br))
pt.y = NSMinY(br);
else // y > NSMaxY
pt.y = NSMaxY(br);
return pt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment