rotate a set of random points

closed account (375jz8AR)
I must rotate a set of random numbers 0.122 radians, to do it i found the center of equilibrium of the system, which code you recomend me to rotate it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  #include <iostream>
#include <ctime>
#include <cstdlib>
#include <math.h>
using namespace std;

int main()
{
	srand(time(NULL));

	int x, y;
	bool xsign, ysign; // true if negative

	int arrX[5];
	int arrY[5];

	for (int i = 0; i<5; i++)
	{
		x = rand() % 100;
		y = rand() % 100;
		xsign = rand() % 2;
		ysign = rand() % 2;

		if (xsign) x *= -1;
		if (ysign) y *= -1;

		arrX[i] = x;
		arrY[i] = y;
	}

	for (int i = 0; i < 5; i++)
	{
		printf("%3d %3d\n", arrX[i], arrY[i]);
	}
 
	
	
		double n, m;
		printf("centre of gravity is");
		n = (arrX[1]+arrX[2]+arrX[0]+arrX[3]+arrX[4])/5;
		m = (arrY[1]+arrY[2]+arrY[0]+arrY[3]+arrY[4])/5;
		printf("%lf , %lf\n", n, m);
	
		
		
		return 0;

}
Last edited on
You mean rotation in 2D. That is easy: http://en.wikipedia.org/wiki/Rotation_matrix

Did you calculate the center (of gravity), because you have to rotate around that center rather than around origo (0,0)?

v' = R*(v-c) + c

Where v is a point, c is the center-vector, R is the matrix, and v' is the rotated point.
closed account (375jz8AR)
you think like that is okei?

for (int i = 0; i < 5; i++)
{
double rot_x, rot_y;
printf("rotate points are:");
rot_x = arrX[i] * cos(0.122) - arrY[i] * sin(0.122);
rot_y = arrX[i] * cos(0.122) + arrY[i] * sin(0.122);
printf("%lf %lf\n", rot_x, rot_y);
}
Yes. That rotates around (0,0).

Lets say that rotation is 90 degrees and the point is (3,1).
Rotation around (0,0) transforms it to (-1,3).
Rotation around (4,1) would transform to (4,0).


PS. Your code sample gives mixed signals:
(1) You do include <iostream> that is a C++ -only feature.
(2) You use only C's functions.
closed account (375jz8AR)
What should i change to rotate it arround the center of gravity point?
What your code would do now is:
v' = R*v


The other equation was already in my first comment.

Letsa work it out with that example:
The point v is (3,1) and the center c is (4,1).
v-c == (-1,0)
Rotating that by 90 degrees (around origo) gives: (0,-1)
Add c and you get (0,-1) + (4,1) == (4,0)

Do you see the idea now?
closed account (375jz8AR)
I think i got it, look at my code.
rot_x = ((arrX[i] - cen_x) * cos(0.122) + cen_x) - ((arrY[i] + cen_y) * sin(0.122) + cen_y);
rot_y = ((arrX[i] - cen_x) * cos(0.122) + cen_x) + ((arrY[i] + cen_y) * sin(0.122) + cen_y);
thank you very much!
I think that you have a + where there should be a - and that is not the only error.

Do the operation in steps. Use helper functions and perhaps a struct too.
Topic archived. No new replies allowed.