Rotate one pixel around another pixel...
Author Message
Rotate one pixel around another pixel...

Ack. I've been trying to find some mathematical algorithm which will allow
me to develop a function (In VB, go figure) rotate pixels around one
specified pixel in the image. No such luck. I've been trying to develop my
own algorithm. I didn't program the two results I got, because of the
obvious shortcomings I could see in them - the first one didn't rotate it
"in a circle", but "in a square" if you get what I mean. The second one was
largely based on trigonometry, and after the 4th Sin() I saw that it would
prolly be unbearably slow - that could always be fixed with a lookup table,
of course.. Also, it would only manage to calculate the new pixel position
within the first 90 degrees.

So, now I'm wondering if any one of your guys know an algorithm that does
this? It's rather irrelevant what speed it runs at as that can always be
sped up. The language it is written in (if it isn't mathematical) is also
rather irrelevant as long as I understand it. I don't understand Assembly
:-)

Thanks :-)

--
Cheers,
The Profound Eol

Homepage: http://www.*-*-*.com/

ICQ: 12177609

Mon, 29 Apr 2002 03:00:00 GMT
Rotate one pixel around another pixel...

Quote:
>Ack. I've been trying to find some mathematical algorithm which will allow
>me to develop a function (In VB, go figure) rotate pixels around one
>specified pixel in the image. No such luck. I've been trying to develop my
>own algorithm. I didn't program the two results I got, because of the
>obvious shortcomings I could see in them - the first one didn't rotate it
>"in a circle", but "in a square" if you get what I mean. The second one was
>largely based on trigonometry, and after the 4th Sin() I saw that it would
>prolly be unbearably slow - that could always be fixed with a lookup table,
>of course.. Also, it would only manage to calculate the new pixel position
>within the first 90 degrees.

>So, now I'm wondering if any one of your guys know an algorithm that does
>this? It's rather irrelevant what speed it runs at as that can always be
>sped up. The language it is written in (if it isn't mathematical) is also
>rather irrelevant as long as I understand it. I don't understand Assembly

x = r * cos(angle)
y = r * sin(angle)

angle is in radians.  I don't remember the formula to convert radians
to degrees, but you probably know that.

So if your point is at 320, 240, and you want to rotate a point around
it 10 pixels away...

angle = loop from 0 to 359 converted to radians
x = 10 * cos(angle)
y = 10 * sin(angle)

pixelx = 320 + x
pixely = 240 + y

And of course, you could calculate the vlaues for each degree and
disctance once and use those values to just plot the points.  I don't
think a lookup table is neccessary though unless you plan to rotate
LOTS and LOTS of points in realtime.

If you're trying to use this to rotate a bitmap, then you're using the
wrong approach. :)

Mon, 29 Apr 2002 03:00:00 GMT
Rotate one pixel around another pixel...

Quote:

>x = r * cos(angle)
>y = r * sin(angle)

>angle is in radians.  I don't remember the formula to convert radians
>to degrees, but you probably know that.

Therefore 1 degree = 2pi/360, or pi/180
or 1 radian = 360/2pi or 180/pi

HTH

Ron
--
Manifest Destiny -- The Race for World Domination
Demo now available!
http://www.*-*-*.com/ {*filter*}ware.com

Mon, 29 Apr 2002 03:00:00 GMT
Rotate one pixel around another pixel...

Quote:

> >Ack. I've been trying to find some mathematical algorithm which will
allow
> >me to develop a function (In VB, go figure) rotate pixels around one
> >specified pixel in the image. No such luck. I've been trying to develop
my
> >own algorithm. I didn't program the two results I got, because of the
> >obvious shortcomings I could see in them - the first one didn't rotate it
> >"in a circle", but "in a square" if you get what I mean. The second one
was
> >largely based on trigonometry, and after the 4th Sin() I saw that it
would
> >prolly be unbearably slow - that could always be fixed with a lookup
table,
> >of course.. Also, it would only manage to calculate the new pixel
position
> >within the first 90 degrees.

> >So, now I'm wondering if any one of your guys know an algorithm that does
> >this? It's rather irrelevant what speed it runs at as that can always be
> >sped up. The language it is written in (if it isn't mathematical) is also
> >rather irrelevant as long as I understand it. I don't understand Assembly

> x = r * cos(angle)
> y = r * sin(angle)

> angle is in radians.  I don't remember the formula to convert radians
> to degrees, but you probably know that.

> So if your point is at 320, 240, and you want to rotate a point around
> it 10 pixels away...

> angle = loop from 0 to 359 converted to radians
> x = 10 * cos(angle)
> y = 10 * sin(angle)

> pixelx = 320 + x
> pixely = 240 + y

> And of course, you could calculate the vlaues for each degree and
> disctance once and use those values to just plot the points.  I don't
> think a lookup table is neccessary though unless you plan to rotate
> LOTS and LOTS of points in realtime.

> If you're trying to use this to rotate a bitmap, then you're using the
> wrong approach. :)

Thanks. I'll have a try. I dunno much about radians, though... But I'm sure
I'll be able to find something. And as for what I'm rotationg - voxels, and
yes, there's LOTS of them.

--
Cheers,
The Profound Eol

Homepage: http://agts.mine.nu/

ICQ: 12177609

Mon, 29 Apr 2002 03:00:00 GMT
Rotate one pixel around another pixel...

<snip>

Quote:
> x = r * cos(angle)
> y = r * sin(angle)

> angle is in radians.  I don't remember the formula to convert radians
> to degrees, but you probably know that.

> So if your point is at 320, 240, and you want to rotate a point around
> it 10 pixels away...

Hmm... Say I wanted to rotate 334, 237 then? Would it be something like
this?

x = 14 * cos(angle)
y = -3 * sin(angle)

or do I need to find some brilliant HowFarAwayIsReallyThisPoint() function?

Quote:
> angle = loop from 0 to 359 converted to radians
> x = 10 * cos(angle)
> y = 10 * sin(angle)

> pixelx = 320 + x
> pixely = 240 + y

> And of course, you could calculate the vlaues for each degree and
> disctance once and use those values to just plot the points.  I don't
> think a lookup table is neccessary though unless you plan to rotate
> LOTS and LOTS of points in realtime.

> If you're trying to use this to rotate a bitmap, then you're using the
> wrong approach. :)

Mon, 29 Apr 2002 03:00:00 GMT
Rotate one pixel around another pixel...

Quote:

> >x = r * cos(angle)
> >y = r * sin(angle)

> >angle is in radians.  I don't remember the formula to convert radians
> >to degrees, but you probably know that.

> 360 degrees = 2pi radians
> Therefore 1 degree = 2pi/360, or pi/180
> or 1 radian = 360/2pi or 180/pi

Ah, thank ye.

--
Cheers,
The Profound Eol

Homepage: http://agts.mine.nu/

ICQ: 12177609

Mon, 29 Apr 2002 03:00:00 GMT
Rotate one pixel around another pixel...

To rotate a single point around the point X with the distance to that point
from your rotation pt is Xr
you should use, assume centr_x and centr_y is the x, y coordinates of the
point you want to rotate around:

new_x  = Xr * cos(angle) + centr_x;
new_y  = Xr * sin(angle) + centr_y;

but if you'r rotating alot of vectors you should consider doing a lookup
table instead of using cos and sin

float look_sin[360]
float look_cos[360]

for(int x =0; x <360; x++)
{
look_cos[x] = cos(x*(PI/180));
look_sin[x] = sin(x*(PI/180));

Quote:
}

am i right?

Mon, 29 Apr 2002 03:00:00 GMT
Rotate one pixel around another pixel...
Try this as a demo:

for angle = 0 to 360
a=2*3.1415*angle/360
xcenter=500
ycenter=500
next angle

If I did not make a mistake it draws a line that rotates around in one cirle
like a hand on a clock.

All the best,

Pieter

Mon, 29 Apr 2002 03:00:00 GMT
Rotate one pixel around another pixel...

Quote:

>but if you'r rotating alot of vectors you should consider doing a lookup
>table instead of using cos and sin

>float look_sin[360]
>float look_cos[360]

>for(int x =0; x <360; x++)
>{
>look_cos[x] = cos(x*(PI/180));
>look_sin[x] = sin(x*(PI/180));
>}

>am i right?

Well, actually you should do performance testing before such
optimizations.  I've found lately that on just about every platform I've
tried it on, the table-lookup like this is actually *slower* than
calling the library function.  (My test cases have been runs of order
10^6 calls or higher to the functions.)
--
|  "If hard data were the filtering criterion
Mark Ping                     |   you could fit the entire contents of the

| - Cecil Adams, The Straight Dope Tells All

Tue, 30 Apr 2002 03:00:00 GMT
Rotate one pixel around another pixel...

Quote:

><snip>

>> x = r * cos(angle)
>> y = r * sin(angle)

>> angle is in radians.  I don't remember the formula to convert radians
>> to degrees, but you probably know that.

>> So if your point is at 320, 240, and you want to rotate a point around
>> it 10 pixels away...

>Hmm... Say I wanted to rotate 334, 237 then? Would it be something like
>this?

>x = 14 * cos(angle)
>y = -3 * sin(angle)

No.

negative radius, then the position the point was supposed to appear at
would be reversed... it would be on the opposite side of the circle.

Quote:
>or do I need to find some brilliant HowFarAwayIsReallyThisPoint() function?

I thought you knew how far away the points you were going to rotate
were.

If you plot a point, and then want to rotate it, you'll need to find
it's distance from the center point.  For that you'd need to use the
distance equation:

d = sqrt((y1-y2) * (x1-x2))

Where x1, y1 are the first point, and x2, y2 are the second.

Of course, you might not need to do it that way... Do you just want to
plot some points and rotate them?  You could just assign them random
distances and angles to start at.  That wouldn't require a distance
equation.

Tue, 30 Apr 2002 03:00:00 GMT
Rotate one pixel around another pixel...

Quote:

> <snip>

> > x = r * cos(angle)
> > y = r * sin(angle)

> > angle is in radians.  I don't remember the formula to
> > convert radians to degrees, but you probably know that.

> > So if your point is at 320, 240, and you want to rotate
> > a point around it 10 pixels away...

You also need an angle delta, otherwise you're just rotating
a line through the origin...

Quote:
> Hmm... Say I wanted to rotate 334, 237 then? Would it
> be something like this?

> x = 14 * cos(angle)
> y = -3 * sin(angle)

> or do I need to find some brilliant
> HowFarAwayIsReallyThisPoint() function?

Well, to use the distance, you'd need a way to find the
distance and angle to the point and then use:
x = r * cos(angle + angle_to_point)
Not fun, and rather inaccurate.

A *much* better way is to simply transform your axes.
You can imagine that the x coordinate of a point is made
up of many vectors joined end to end. When the vectors
are (1,0), the coordinate lies along the x axis. What you
want to do is change this vector:

float ix,iy,jx,jy;    // i = 'x' vector, j = 'y' vector.
ix = 1.0 * cos(angle);
iy = 1.0 * sin(angle);
jx = 1.0 * -sin(angle);
jy = 1.0 * cos(angle);

> x = 14 * cos(angle)
> y = -3 * sin(angle)
You do:
x = (14 * ix) + (-3 * jx);
y = (14 * iy) + (-3 * jy);

Obviously, when angle = 0, you get:
ix = 1.0, iy = 0.0, jx = 0.0, jy = 1.0
So the formula simplifies to:
x = (14 * ix) + (-3 * jx) = 14 * 1.0 = 14
y = (14 * iy) + (-3 * jy) = -3 * 1.0 = -3
ie: no rotation.

Basically, this is just calculating and applying 2d rotation
matrix...

---
Russ

Tue, 30 Apr 2002 03:00:00 GMT
Rotate one pixel around another pixel...

Quote:
> To rotate a single point around the point X with the distance to that
point
> from your rotation pt is Xr
> you should use, assume centr_x and centr_y is the x, y coordinates of the
> point you want to rotate around:

> new_x  = Xr * cos(angle) + centr_x;
> new_y  = Xr * sin(angle) + centr_y;

> but if you'r rotating alot of vectors you should consider doing a lookup
> table instead of using cos and sin

> float look_sin[360]
> float look_cos[360]

> for(int x =0; x <360; x++)
> {
> look_cos[x] = cos(x*(PI/180));
> look_sin[x] = sin(x*(PI/180));
> }

> am i right?

You may not need 360 elements in the array.   Depending on the resolution
and how coarse/fine you want the rotation to be you don't need to use 360
degrees.  Instead you could dimension your table to 100 elements and define
a circle as having 100 "whatsits."    And math using longs may be faster
(warning untested code just to illustrate ideas):

Dim MyCos(0 To 99) As Long
Dim MySin(0 To 99) As Long
Dim Pi As Single

Pi = 3.14 ....
For I = 0 To 99
MyCos(I) = Cos(i * (Pi/50)) * 1000
MySin(I) = Sin(i * (Pi/50)) * 1000
Next

A = AngleInDegrees / 3.6

NewX = (OldX * MyCos(A)) / 1000 + CenterX
NewY = (OldY * MySin(A)) / 1000 + CenterY

-- Roger
Arrow Mailing List Server for Windows: http://www.jadebox.com/arrow/

Tue, 30 Apr 2002 03:00:00 GMT
Rotate one pixel around another pixel...
I'm trying to do something similar and I don't understand what is happening.
The problem is that instead of taking 360 iterations to complete a circle,
it only takes 30.

I create a look-up table in the following manner:

for(int x =0; x <360; x++)
{
look_cos[x] = cos(x*(PI/180));
look_sin[x] = sin(x*(PI/180));
}

Later I "plot" some points (actually I am blitting an image but just using
the left, top coordinate as the point that is being rotated) in the
following manner (not optimized at all because I'm trying to keep it simple
until it works):
For this example I just use 3 "steps" and am attempting to have them be just
1 degree appart but they aren't.  They are WAY more than just 1 degree off
(actually look something closer to 90 degrees apart).  Any ideas why?

FYI - 166,165 that I use are the width and height of the image being
"rotated"

XPos = 300 * look_cos[0] + (g_DisplayWidth / 2 - (166 / 2));
YPos = 300 * look_sin[0] + (g_DisplayHeight / 2 - (165 / 2));

SetRect(&OutputRect,XPos, YPos, XPos + 166, YPos + 165);
g_pDDSBack->Blt(&OutputRect, g_pDDSSprite, NULL, DDBLT_WAIT | DDBLT_KEYSRC,
&ddbltfx);

XPos = 300 * look_cos[1] + (g_DisplayWidth / 2 - (166 / 2));
YPos = 300 * look_sin[1] + (g_DisplayHeight / 2 - (165 / 2));

SetRect(&OutputRect,XPos, YPos, XPos + 166, YPos + 165);
g_pDDSBack->Blt(&OutputRect, g_pDDSSprite, NULL, DDBLT_WAIT | DDBLT_KEYSRC,
&ddbltfx);

XPos = 300 *  look_cos[2] + (g_DisplayWidth / 2 - (166 / 2));
YPos = 300 * look_sin[2] + (g_DisplayHeight / 2 - (165 / 2));

SetRect(&OutputRect,XPos, YPos, XPos + 166, YPos + 165);
g_pDDSBack->Blt(&OutputRect, g_pDDSSprite, NULL, DDBLT_WAIT | DDBLT_KEYSRC,
&ddbltfx);

Quote:

> ><snip>

> >> x = r * cos(angle)
> >> y = r * sin(angle)

> >> angle is in radians.  I don't remember the formula to convert radians
> >> to degrees, but you probably know that.

> >> So if your point is at 320, 240, and you want to rotate a point around
> >> it 10 pixels away...

> >Hmm... Say I wanted to rotate 334, 237 then? Would it be something like
> >this?

> >x = 14 * cos(angle)
> >y = -3 * sin(angle)

> No.

> negative radius, then the position the point was supposed to appear at
> would be reversed... it would be on the opposite side of the circle.

> >or do I need to find some brilliant HowFarAwayIsReallyThisPoint()
function?

> I thought you knew how far away the points you were going to rotate
> were.

> If you plot a point, and then want to rotate it, you'll need to find
> it's distance from the center point.  For that you'd need to use the
> distance equation:

> d = sqrt((y1-y2) * (x1-x2))

> Where x1, y1 are the first point, and x2, y2 are the second.

> Of course, you might not need to do it that way... Do you just want to
> plot some points and rotate them?  You could just assign them random
> distances and angles to start at.  That wouldn't require a distance
> equation.

Wed, 01 May 2002 03:00:00 GMT
Rotate one pixel around another pixel...

Quote:

>I'm trying to do something similar and I don't understand what is happening.
>The problem is that instead of taking 360 iterations to complete a circle,
>it only takes 30.

>I create a look-up table in the following manner:

>for(int x =0; x <360; x++)
> {
>  look_cos[x] = cos(x*(PI/180));
>  look_sin[x] = sin(x*(PI/180));
> }

Are your arrays the right number format?  A float, not an integer?

The degrees to radians conversion seems to be okay... That is, if PI
is equal 3.14159265

Why don't you try something even simpler, and just try to plot 360
points around in a circle?

Quote:
>FYI - 166,165 that I use are the width and height of the image being
>"rotated"

> XPos = 300 * look_cos[0] + (g_DisplayWidth / 2 - (166 / 2));
> YPos = 300 * look_sin[0] + (g_DisplayHeight / 2 - (165 / 2));

Are you sure the order of operations with your multiplication and
addition are right there?  I forget which goes first... I always
seperate stuff with parentheses to be sure.

Thu, 02 May 2002 03:00:00 GMT
Rotate one pixel around another pixel...
After finally giving up for the night and getting some sleep I took a look
at it again with "fresh" eyes and figured out that the problem was because I

const PI = 3.1415926535897932384626433832795;

when I should have done

const float PI = 3.1415926535897932384626433832795;

After making the change everything works great

Quote:

> >I'm trying to do something similar and I don't understand what is
happening.
> >The problem is that instead of taking 360 iterations to complete a
circle,
> >it only takes 30.

> >I create a look-up table in the following manner:

> >for(int x =0; x <360; x++)
> > {
> >  look_cos[x] = cos(x*(PI/180));
> >  look_sin[x] = sin(x*(PI/180));
> > }

> Are your arrays the right number format?  A float, not an integer?

> The degrees to radians conversion seems to be okay... That is, if PI
> is equal 3.14159265

> Why don't you try something even simpler, and just try to plot 360
> points around in a circle?

> >FYI - 166,165 that I use are the width and height of the image being
> >"rotated"

> > XPos = 300 * look_cos[0] + (g_DisplayWidth / 2 - (166 / 2));
> > YPos = 300 * look_sin[0] + (g_DisplayHeight / 2 - (165 / 2));

> Are you sure the order of operations with your multiplication and
> addition are right there?  I forget which goes first... I always
> seperate stuff with parentheses to be sure.

Thu, 02 May 2002 03:00:00 GMT

 Page 1 of 2 [ 24 post ] Go to page: [1] [2]

Relevant Pages