The idea is that I want to get a percentage of the position of Rect2 in relation to Rect1. In the picture, it would be about 75/80%. Rect2 never leaves Rect1. I move Rect1 around all the time, so it should work regardless of Rect1's position.

I had something like this, but it didn't work as intended: ((Rect2.X + Rect1.X) / (Rect1.Width + Rect1.X)) * 100

Rect1.X would work as an offset so the calculation would "ignore" the moving around of Rect1. Or that was the idea...


1 Answer 1


If you want 100% to mean, "Right edge of Rect2 touches right edge of Rect1", then we just want the ratio of where the Rect1's left edge is within that reduced range of Rect2.

(Rect2.left - Rect1.left) / (Rect1.width - Rect2.width) * 100.0

Should do it. (I used "left" instead of "x" to remove some possible ambiguity.) You could do it from the right, like

100.0 - (Rect1.right - Rect2.right) / (Rect1.width - Rect2.width) * 100.0

Where right = left + width.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .