Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

NEW REGISTRATIONS - please contact us if you wish to register on the forum

Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright

[Instruction] How to rotate points

Post any examples or modules that you want to share here

[Instruction] How to rotate points

Postby tulamide » Tue May 24, 2016 7:58 pm

I thought this might be interesting, since it is useful for a lot of applications. If you know how to rotate points, you won't learn anything here.

The process is relatively simple, but you need to understand it once. Therefore I go through the process in detail. That makes it look complex or difficult, but only when reading for the first time. Promised!

(read left to right, top to bottom)
rotating_points_1.png
rotating_points_1.png (23.51 KiB) Viewed 19295 times

So you have a line and want to rotate it. A line has two points, so you need to rotate the two points, then connect them again by a line.

The first step is to define the anchor point, which is the point around that you want to rotate the points. Here I decided to take the middle of the line, so that both points rotate evenly. We can calculate that anchor point easily by halving the distance on the x-axis and the halving the distance on the y-axis (see image 2)

Now we need the distance between the anchor point and the points P1 and P2. Here comes Pythagoras to the rescue: A right-angled triangle with two sides known. The hypotenuse is then the squareroot of a² + b². If we build such a rectangle in a way that the distance we're looking for equals the hypotenuse, we are already done.
In this example, a is the distance on the x-axis and b the distance on the y-axis, between AP and P1, or:
Code: Select all
a = APx - P1x
b = APy - P1y

d1 = ((APx - P1x)^^2 + (APy - P1y)^^2)^^0.5

Best of all: we get d2 for free, since both points are axactly the same distance away from AP: d2 = d1

The unit circle is a thought circle around AP with a radius of 1. Read about the unit circle in math publications. For us it especially means that we can a) get an angle and b) scale the result easily later on.
What we need as well is the angle at which the points P1 and P2 are offsetted to AP. Therefore we can use the arcustangent. All programming languages know atan2, which is a variation of atan, that takes into account the sign of the two components. This is important because we would just get -(pi/2) to pi/2 from atan, but a full circle ranges from -pi to pi. The signs are used in atan2 to get the correct quadrant.

CAUTION: The standard convention is to pass y before x!

Since atan2 works for the unit circle by following a vector starting at 0,0, we need to make AP that [0,0] point. Just subtract it from the points' coordinates as shown in image 4 and 5.

We now have an angle alpha, and just need to add the rotation n we want to it:
Code: Select all
beta = alpha + n


rotating_points_2.png
rotating_points_2.png (8.75 KiB) Viewed 19295 times


With the new angle beta, we can calculate the new position of the point P2. We already know the position of the point that is on the unit circle at angle beta. We just need to use sin and cos. But cos(beta) and sin(beta) will only be in the range of the unit circle, which has a radius of 1. That range is [-1, 1]. But luckily we already calculated the original distance between AP and P2: d2. So let's multiply the results with d2. But we also temporarily moved AP to [0, 0], so we have to add AP as well. And now we have the new position of P2:
Code: Select all
P2'x = APx + cos(beta) * d2
P2'y = APy + sin(beta) * d2


The last step is to get the coordinates of P1 as well. Very easy, just use the opposite angle of beta. If you're working with radians, just reverse the sign. If you're working with degrees, just add beta to 180. Then just repeat the last calculation, but for P1'.

Shortlist:
(1) Define anchor point.
(2) Get distance between points
(3) atan2(y, x) to get angle
(4) Rotate angle
(5) Do offset + [cos or sin] * distance

And that's it!
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: [Instruction] How to rotate points

Postby RJHollins » Tue May 24, 2016 8:58 pm

For those of us that took algebra/trig in what seems 'another lifetime' ... this is a great tutorial Tulamide.

Thanks for taken the time for putting this together. I'd like to figure how to get this into an FS schematic for reference.

Thanks again 8-)
RJHollins
 
Posts: 1571
Joined: Thu Mar 08, 2012 7:58 pm

Re: [Instruction] How to rotate points

Postby tulamide » Wed May 25, 2016 4:15 pm

I hoped you would be able to after these instructions.
Here's one simple fsm, just for the fun of it.

If anyone uses this fsm, or parts of it, or the code, in parts or completely, in his/her own schematics, executables or VST plugins, that one is required to credit "copyright by tulamide, used with kind permission".
Attachments
ROTATION.fsm
(1.06 KiB) Downloaded 1098 times
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: [Instruction] How to rotate points

Postby adamszabo » Thu May 26, 2016 9:28 am

Really cool! Maybe you can even take it a step further and create some random arpeggiator thing out of it. Instead of the balls bouncing in a white rectangle, perhaps it can do the same inside a circle. The circle could have some keyboard scale around the edge, and the bouncing balls can hit each note creating some strange sequences :D
adamszabo
 
Posts: 667
Joined: Sun Jul 11, 2010 7:21 am

Re: [Instruction] How to rotate points

Postby tulamide » Fri May 27, 2016 1:16 am

adamszabo wrote:Really cool! Maybe you can even take it a step further and create some random arpeggiator thing out of it. Instead of the balls bouncing in a white rectangle, perhaps it can do the same inside a circle. The circle could have some keyboard scale around the edge, and the bouncing balls can hit each note creating some strange sequences :D

That's actually a really good idea! I might be doing a prototype. But not right now, I'm in the midst of work for an awesome project of someone else. I'm focused on that, until it's done! But I'll come back to this ;)
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: [Instruction] How to rotate points

Postby adamszabo » Fri May 27, 2016 10:12 am

All right, no problemo! Its definitely something to revisit, best of luck with your big project! :mrgreen:
adamszabo
 
Posts: 667
Joined: Sun Jul 11, 2010 7:21 am


Return to User Examples

Who is online

Users browsing this forum: No registered users and 27 guests