Maths: How far from level?

I have a rectangle laying horizontally across the screen. It rotates at the centre. There is a surrounding rectangle which I want to glow brighter as the main rectangle comes into line with it.

For some reason I can’t figure out the (what I’m fairly sure is basic) maths to get this working.

The challenge is that the .rotation of the rotating rectangle should not matter - the practical angle between it and the surrounding rectangle is what matters. Think of it like a gyroscope which gets brighter the closer to being inline they are.

given two arbitrarily rotated rectangles, (presumably similar, but not necessarily so), i believe your question is equivalent to asking “are their presumed perpendicular sides actually perpendicular?”  if so, then a dot-product will answer it:

  1. use basic trig (or localToContent and contentToLocal) on the half-width/height to obtain a rotated corner point in local coordinates for each rect (assuming you don’t otherwise have those points available already)

  2. for the second rect, swap the xy’s obtained and invert one (equivalent to using the “long edge” of rect#1, and the “short edge” of rect#2)

  3. treat both as vectors (need not normalize if just testing for perpendicular) then take their dot-product, then compare that against zero (the more non-zero, the more non-perpendicular)

for example:

 local x1, y1 = rect1:contentToLocal(rect1:localToContent(rect1.width/2,rect1.height/2)) local x2, y2 = rect1:contentToLocal(rect2:localToContent(rect2.width/2,rect2.height/2)) x2,y2 = -y2,x2 local dot = x1 \* x2 + y1 \* y2 print("dot", dot)

given two arbitrarily rotated rectangles, (presumably similar, but not necessarily so), i believe your question is equivalent to asking “are their presumed perpendicular sides actually perpendicular?”  if so, then a dot-product will answer it:

  1. use basic trig (or localToContent and contentToLocal) on the half-width/height to obtain a rotated corner point in local coordinates for each rect (assuming you don’t otherwise have those points available already)

  2. for the second rect, swap the xy’s obtained and invert one (equivalent to using the “long edge” of rect#1, and the “short edge” of rect#2)

  3. treat both as vectors (need not normalize if just testing for perpendicular) then take their dot-product, then compare that against zero (the more non-zero, the more non-perpendicular)

for example:

 local x1, y1 = rect1:contentToLocal(rect1:localToContent(rect1.width/2,rect1.height/2)) local x2, y2 = rect1:contentToLocal(rect2:localToContent(rect2.width/2,rect2.height/2)) x2,y2 = -y2,x2 local dot = x1 \* x2 + y1 \* y2 print("dot", dot)