Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Image rotation (math question kind of)

Status
Not open for further replies.

zackiv31

Programmer
May 25, 2006
148
US
So I have an array of points (x,y) values, that represent the pixels of the image. The first value is 0,0, and the rest of the values are relative to the first.

Ex: 0,0 1,0 -2,2 1,1 would produce 4 pixels like this:
__xx
___x
x___

where the top left 'x' is the first point. the second sequence just means 1 point to the right, and 0 points down... works the same for negatives.


What I'm now trying to do is convert my original array, to all the permutations of rotating the rest of the pixels 360 degrees about the anchor point (0,0). So I woule have a 90 degree rotate left and right, a 180 degree rotate, and the odd angles in between (those are the tricky ones)

Ideally it would obviously have to round to solid pixel dimensions, so there would only be ~10 or so for this (total estimation)


I'm guessing it's gonna have to do with some trig, but mine is a little rusty.

Anyone have any ideas?
 
I guess I should have mentioned this, the array of pixels could be represented in another way if it makes the math easier, as in a simple coordinate system.

So the original could also be represented like this:
2,0 3,0 3,1 0,2
 
What have you tried so far?

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Umm.. nothing really.. trying to see if you guys have any good ideas... I really hated math in school, so it's not really my forte... I'm just a coder.
 
The transformation of coordinates for a rotation [θ] around a common origin is:
xnew=x sin[θ]-y cos[θ]
ynew=x cos[θ]+y sin[θ]
Of course you'll have to round the results to the nearest integer. Not sure that the transformation will give an image very similar to the original one in all cases.

prex1
: Online tools for structural design
: Magnetic brakes for fun rides
: Air bearing pads
 
Basically, what prex said. The only other thing you need to do is to determine the point you will be rotating around (the common origin). Usually ( (xmin+xmax)/2 , (ymin+ymax)/2 )
 
prex, is this for my second example, where it's based off of a simple coordinate system, and not the relative one?

This info seems great btw, I'll try it out in a little when I have time.
 
Actually, no. This is based around the relative system you originally stated. You said that you wanted to rotate around (0,0) in your first co-ordinate system, and that is the "common origin" (you called it your anchor point).
 
Ok, I'll work off of your solution brigmar, I'll let you know how it goes...
 
I don't think its for the relative ones, consider two points:

0,0 1,0 :
xx

rotate 90:
xnew=(1) sin(90)- (0) cos? = 1
ynew=(1) cos(90)+ (0) sin? = 0

yields the same result:
0,0 1,0
xx

Where it should yield two points one on top of the other:
x
x


?
 
I just noticed that fails for both systems..., relative or not.

Any Ideas?
 
I think it's suppose to be this:

xnew=x cos?+y sin?
ynew=-x sin?+y cos?
 
Sorry, tried to recover the formulae from memory...
The correct ones are:
[tt]xnew=x cos[θ]-y sin[θ]
ynew=x sin[θ]+y cos[θ][/tt]
For your example you get correctly for point (1,0):
[tt]xnew=0
ynew=1[/tt]
Note that the angles should be measured counterclockwise.
And, as also noted by brigmar, you can use any system of coordinates, your first one or the second one, simply the rotation will be about point (or pixel) (0,0).

prex1
: Online tools for structural design
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top