Reputation: 31
I generate 1000 random points, and I'd like to rotate the points, around some point defined by x and y.
I've used:
px = Math.cos(theta) * (px-ox) - Math.sin(theta) * (py-oy) + ox
py = Math.sin(theta) * (px-ox) + Math.cos(theta) * (py-oy) + oy
but the problem is that points start to converge, to rotation point after rotating for some time.
Here is the javascript code. http://www.kaotik.si/rotation1.html
what I am doing wrong ?
the iteration section of code:
for (var i = 0; i < tocke.length; i++)
{
px = tocke[i]["x"];
py = tocke[i]["y"];
r = tocke[i]["r"];
g = tocke[i]["g"];
b = tocke[i]["b"];
theta = 0.1;
ox = centerX;
oy = centerY;
px = Math.cos(theta) * (px-ox) - Math.sin(theta) * (py-oy) + ox
py = Math.sin(theta) * (px-ox) + Math.cos(theta) * (py-oy) + oy
tocke[i]["x"] = px;
tocke[i]["y"] = py;
draw_point(px,py,r,g,b,1);
}
Edit: Thanks for solving the problem: I did have to change to:
theta = 0.1;
ox = centerX;
oy = centerY;
tmpX = Math.cos(theta) * (px-ox) - Math.sin(theta) * (py-oy) + ox
tmpY = Math.sin(theta) * (px-ox) + Math.cos(theta) * (py-oy) + oy
tocke[i]["x"] = tmpX;
tocke[i]["y"] = tmpY;
draw_point(px,py,r,g,b,1);
Upvotes: 0
Views: 749
Reputation: 272507
You're reassigning px
before it gets used in the calculation for py
.
Upvotes: 4