Skip to content

Instantly share code, notes, and snippets.

@getify
Created October 11, 2020 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save getify/9c348556149d602524f22d37ce806587 to your computer and use it in GitHub Desktop.
Save getify/9c348556149d602524f22d37ce806587 to your computer and use it in GitHub Desktop.

Need some geometry and/or trigonometry help, specifically with rotating points (about the origin) on a 2d cartesian coordinate graph. To set up the question, let's observe a few already known formulas. See this rough diagram/graph:

rotation-drawing

In the graph, you'll see point A, and you'll also see points B - F that are all shown as rotations (about the origin) from A, given a specific angle (θ). All 6 line segments (from origin to points A - F) are the same length.

If the coordinates of A (x0,y0) are known, and the desired rotation angle θ (in the counter-clockwise direction) is known, the coordinates (x1,y1) of the new point (B - F) can readily be calcuated with these formulas:

x1 = x0 * cos(θ) – y0 * sin(θ)
y1 = x0 * sin(θ) + y0 * cos(θ)

Note that this assumes rotation counter-clockwise, so the angles for points E and F would have to be greater than 180 degrees, as opposed to the smaller clockwise angles (red arcs) shown.


But what if you already knew the coordinates of A and another point (like B), but didn't know the angle? You can actually compute that θ angle based on the fact that the two respective line segments, being the same length (r), actually form two equal sides of an isosceles triangle. See this diagram:

rotation-drawing-2

The formulas needed to find base, height, and then ultimately the angle θ, are:

base = sqrt( (x1 - x0)^2 + (y1 - y0)^2 )   // standard 2d cartesian distance:
height = sqrt( r^2 - (base^2 / 4) )
θ = 180 - ( 2 * arctan(2 * height / base) )

OK, that's great so far. But here's the problem/question I have. As computed above, θ will always assume a counter-clockwise rotation from one point (like A) to another.

What if, given the coordinates of any two points, the θ that comes out is headed in the wrong direction? In other words, what if we wanted to represent the rotation from B to A instead of A to B?

rotation-drawing

In this case, let's say the above computations ended up with the angle θ between A and B being about 35 degrees, but for rotation from B to A, we need θ to be -35 degrees.

So, given the coordinates for any two points, how can I figure out whether the θ I've computed should be the positive one that came out, or if I should negate it?

Keep in mind, that the two points could be in any of the quadrants of the coordinate system. I could write a bunch of if statements for each of the different (16 I think?) permutations of where the two points could be located, but I would really like to express this with a simpler math property if possible.

I contemplated computing the slope of the segment between the two points and use the sign of the slope, but that doesn't seem to be workable. Then I thought maybe the x-axis angle for each point might tell me something about the relative position of each point? But I can't quite work this out.

Help!?

@sahand12
Copy link

You're making the problem so hard for yourself. Just for each of the two points calculate their angle relative to the origin and then subtract from each other.

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