Skip to content

Instantly share code, notes, and snippets.

@IQAndreas
Last active December 23, 2015 03:59
Show Gist options
  • Save IQAndreas/6576705 to your computer and use it in GitHub Desktop.
Save IQAndreas/6576705 to your computer and use it in GitHub Desktop.
// `bigRect.mouseX` can be replaced by `stage.mouseX - bigRect.x`
var ratioX:Number = bigRect.mouseX / bigRect.width;
var ratioY:Number = bigRect.mouseY / bigRect.height;
// This is the x and y location inside of the small rectangle which should line up with the above point
var offsetX:Number = ratioX * smallRect.width;
var offsetY:Number = ratioY * smallRect.height;
smallRect.x = bigRect.x + (ratioX * bigRect.width) - offsetX;
smallRect.y = bigRect.y + (ratioY * bigRect.height) - offsetY;
@IQAndreas
Copy link
Author

The code assumes the registration point for the rectangles is in the top left corner. It's not in processing, but only uses basic math operators, so it should be quick and easy to convert to any language.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment