I'm trying to write an algorithm for rotating pixels on a screen at my work office and I don't have any graph paper to check my math. Math has never been my strongest suit in the first place, and I need to make sure my algorithm for rotating around an original set of pixels is sound. Does this look right?
X' = (X*cos(theta)) - (Y*sin(theta))
Y' = (Y*sin(theta)) - (X*cos(theta))
where X' and Y' are the new pixel locations from the original X and Y, and theta is in degrees. Anybody better at math than I want to give this a look? I took it from the following rotation matrix formula:
[X', Y'] = [cos(theta) - sin(theta), sin(theta) - cos(theta)]*[X, Y]
General math rotation?
Re: General math rotation?
From wiki:
x' = x cos theta - y sin theta
y' = x sin theta + y cos theta
Also, you need to make sure your coordinants are with respect to the point you are trying to rotate around (i.e. 0,0 is the rotation point) and that theta is correct for the orientation of the axes. The standard axes for pixels measurements are 0,0 in the upper left corner, with Y being positive as you go down. So you'll need to adjust for that.
x' = x cos theta - y sin theta
y' = x sin theta + y cos theta
Also, you need to make sure your coordinants are with respect to the point you are trying to rotate around (i.e. 0,0 is the rotation point) and that theta is correct for the orientation of the axes. The standard axes for pixels measurements are 0,0 in the upper left corner, with Y being positive as you go down. So you'll need to adjust for that.
Blizzard Entertainment Software Developer - All comments and views are my own and not representative of the company.
Re: General math rotation?
Ah, I mixed up the x and y in my y' calculation, I always suck at transpositionMrPopo wrote:From wiki:
x' = x cos theta - y sin theta
y' = x sin theta + y cos theta
Also, you need to make sure your coordinants are with respect to the point you are trying to rotate around (i.e. 0,0 is the rotation point) and that theta is correct for the orientation of the axes. The standard axes for pixels measurements are 0,0 in the upper left corner, with Y being positive as you go down. So you'll need to adjust for that.
With respect to rotation point, how would I modify the formula to account for this? I figured 0,0 would be my rotation point in the upper left corner, and that I could manually reposition to the sprite after rotation if I needed to rotate around, say, the center of the sprite, but it'd be much easier if I could select a point of rotation from within the formula. Even if my rotation would typically be around the origin.
Re: General math rotation?
Thinking about it, I'm guessing something like this?
X' = ((X-oX)*(cos*theta)) - ((Y-oY)*(sin*theta))
Y' = ((X-oX)*(sin*theta)) + ((Y-oY)*(cos*theta))
where oX and oY are the x and y of the point of rotation. Are there any glaring flaws with this?
X' = ((X-oX)*(cos*theta)) - ((Y-oY)*(sin*theta))
Y' = ((X-oX)*(sin*theta)) + ((Y-oY)*(cos*theta))
where oX and oY are the x and y of the point of rotation. Are there any glaring flaws with this?
Re: General math rotation?
Nope, that looks good. Just remember that since Y increases as you go down, theta increases as you go clockwise.
Blizzard Entertainment Software Developer - All comments and views are my own and not representative of the company.
Re: General math rotation?
That should by accounted for, however, because sin and cos are swapped in the Y' calculations, though, right?MrPopo wrote:Nope, that looks good. Just remember that since Y increases as you go down, theta increases as you go clockwise.
Re: General math rotation?
No, the formula you're working off of is the basic rotation formula based on standard coordinate axes. The cosine terms will be unaffected, but the sine terms will be inverted.
So if you want theta to grow counter-clockwise you would negate the sine terms, giving you:
x' = xcos + ysin
y' = ycos - xsin
So if you want theta to grow counter-clockwise you would negate the sine terms, giving you:
x' = xcos + ysin
y' = ycos - xsin
Blizzard Entertainment Software Developer - All comments and views are my own and not representative of the company.
- BoringSupreez
- Next-Gen
- Posts: 9738
- Joined: Wed Feb 11, 2009 10:09 pm
- Location: Tokyo
Re: General math rotation?
This thread reminds me of why I don't miss school, and how I'm so not looking forward to college. Seriously, that stuff sucks.
prfsnl_gmr wrote:There is nothing feigned about it. What I wrote is a display of actual moral superiority.
Re: General math rotation?
Much appreciate for the help!MrPopo wrote:No, the formula you're working off of is the basic rotation formula based on standard coordinate axes. The cosine terms will be unaffected, but the sine terms will be inverted.
So if you want theta to grow counter-clockwise you would negate the sine terms, giving you:
x' = xcos + ysin
y' = ycos - xsin
Re: General math rotation?
I miss having time to spend learning new tricks, especially from when I was young and in highschool. Highschool was a breeze for me and I had tons of free time I spent reading books on programming. I learned more about programming in highschool in my free time, than I did in college. And I got my BS in software engineering lol.BoringSupreez wrote:This thread reminds me of why I don't miss school, and how I'm so not looking forward to college. Seriously, that stuff sucks.
