-2

I have a rotated DIV within a non-rotated DIV and I'm trying to work out the cursor position on the outer DIV in relation to the rotated DIV. I guess I need the position as though the coordinate system has been rotated to match the angle of the rotated div.

Following is an example of what I need:

rotated DIV within a DIV

I know or can easily get:

  • w1, the width of the outer DIV
  • h1, the height of the outer DIV
  • w2, the width of the inner/rotated DIV
  • h2, the height of the inner/rotated DIV
  • a, the angle of the rotated DIV
  • x1, the x coord of a point P
  • y1, the y coord of a point P

I don't know how to get, but need, x2 and y2.

3
  • I made a mistake :-( y2 should be from P to the top corner (the corner touching the top edge of the outer DIV.) Commented Oct 21, 2024 at 5:10
  • 1
    Please visit How to Ask - if you post a minimal reproducible example it is easier to help Commented Oct 21, 2024 at 8:31
  • Please then edit your post to correct your expected output and, as @ㅤmplungjan says, add a minimal reproducible example. Commented Oct 21, 2024 at 11:08

1 Answer 1

0

I ignore whether you can access that information using built-in tools, so here is a possible answer that centres on the maths of the problem.

What you're doing is basically an affine transformation, specifically the composition of a translation and a rotation:

Transformation equation.

Where the rotation matrix is:

Rotation matrix.

; and the translation vector is:

Translation vector.

So, in fine, the coordinates in the new system should be:

let x2 = x1*Math.cos(a) + Math.sin(a) * (-y1 + w2*Math.sin(a));
let y2 = y1*Math.cos(a) + Math.sin(a) * (+x1 - w2*Math.cos(a));

EDIT - The previous solution deals with the original posted problem; to tackle the "new" modified version - as described by the OP in the comments -, there are two possibilities:

  1. either to simply apply to the above result the transformation (x, y) --> (x, h2-y) (this is the easy way);
  2. or to re-formalise the problem from scratch. We'll now do this for didactic reasons.

In this second scenario, apart from the aforementioned rotation and translation, the affine transformation includes a reflection about the x axis, and an additional translation:

Transformation equation.

; where the rotation matrix is still:

Rotation matrix.

; the reflection can be written:

Reflection matrix.

; the first translation, as before, is:

First translation vector.

; and, finally, the second translation vector:

Second translation vector.

Inverting the system, the final result is:

let x2 = +x1*Math.cos(a) + Math.sin(a) * (-y1 + w2*Math.sin(a));
let y2 = -y1*Math.cos(a) + Math.sin(a) * (-x1 + w2*Math.cos(a)) + h2;
Sign up to request clarification or add additional context in comments.

4 Comments

When I put some values into x2 and y2 - (100, 0) at 0.8 radians within a 300px square DIV - for, say, the top corner it gives 100 * Math.cos(0.8) + Math.sin(0.8) * (-0 + 300 * Math.sin(0.8)) which is 224 and not the 0 as I would expect. And y2 equals 0 * Math.cos(0.8) + Math.sin(0.8) * (+100 - 300 * Math.cos(0.8)); which is -78 and not 0. What am I missing/not understanding?
How is (100, 0) the top corner? The top corner of the inner, square div, with w2 = 300 and a = PI/4 ~ 0.8, would be (x1, y1) = (300/sqrt(2), 300*sqrt(2)). And the formula yields (x2, y2) = (0, 300).
With the same configuration, the left corner of the inner div, corresponding to (x1, y1) = (0, 300/sqrt(2)), transforms to (x2, y2) = (0, 0).
Oh no, I'm sorry I did something stupid (I took more care drawing a nice sketch than a correct one.) I drew the sketch incorrectly. y2 should be from P to the corner that touches the top edge of the outer DIV (not the left hand side edge.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.