Ruby-tk question

J

Joe Van Dyk

What's the easiest way to rotate a Tk Canvas Polygon?

Do I need to just manually manipulate the coordinate points? If so,
what's the most straightforward way of doing that?
 
J

Joe Van Dyk

Hm.

Based on research, it looks like Tk doesn't support rotatation very well.

Any ideas on how to do this manually?

I can get an array of points like:

0.4669 0.0000
0.3584 0.0479
0.2499 0.0743
0.0796 0.0933
-0.4669 0.0706
-0.4669 -0.0706
0.0796 -0.0933
0.2499 -0.0743
0.3584 -0.0479
0.4669 0.0000

And I'd need to be able to rotate a polygon made from those points in
some direction.
 
H

Hidetoshi NAGAI

From: Joe Van Dyk <[email protected]>
Subject: Re: Ruby-tk question
Date: Fri, 6 May 2005 10:41:39 +0900
Message-ID: said:
Hm, I have no idea how to do that. Trig was so long ago.

That is field of Mathematics. :)
I think that is not so difficult.
 
J

Joe Van Dyk

From: Joe Van Dyk <[email protected]>
Subject: Re: Ruby-tk question
Date: Fri, 6 May 2005 10:41:39 +0900


That is field of Mathematics. :)
I think that is not so difficult.

It's horribly difficult for someone who hasn't done math in years.
 
L

Logan Capaldo

It's horribly difficult for someone who hasn't done math in years.

Well according to a random math site:

def deg2rad(x)
(x * Math::pI) / 180.0
end

def rotate(x, y, deg)
r = Math.sqrt(x**2 + y**2)
theta = Math.atan(y/x)
u = r * Math.cos(theta + deg2rad(deg))
v = r * Math.sin(theta + deg2rad(deg))
[u, v]
end

seems to work pretty well

rotate(1.0, 0.0, 90.0) #=> [6.12323399573677e-17, 1.0]

that is of course if you except 6.blah times 10 to the negative 17 is
close enough to zero for governement work.

This method is SUPPOSED to convert them into polar coordinates and
then rotate them by deg degrees. I don't prentend to know that its
correct
Math site formula was stolen from:
http://mathforum.org/library/drmath/view/63184.html
 
L

Logan Capaldo

It's horribly difficult for someone who hasn't done math in years.

Well according to a random math site:

def deg2rad(x)
(x * Math::pI) / 180.0
end

def rotate(x, y, deg)
r = Math.sqrt(x**2 + y**2)
theta = Math.atan(y/x)
u = r * Math.cos(theta + deg2rad(deg))
v = r * Math.sin(theta + deg2rad(deg))
[u, v]
end

seems to work pretty well

rotate(1.0, 0.0, 90.0) #=> [6.12323399573677e-17, 1.0]

that is of course if you except 6.blah times 10 to the negative 17 is
close enough to zero for governement work.

This method is SUPPOSED to convert them into polar coordinates and
then rotate them by deg degrees. I don't prentend to know that its
correct
Math site formula was stolen from:
http://mathforum.org/library/drmath/view/63184.html

One last note, this function is gonna rotate like it was on a
cartesian plane (origin in the middle), if Tk's Canvas is like every
other computer coordinate system i've come across, the coordinates are
gonna be only in quadrant IV with the y-value negated, so you may have
to keep that in mind before just pasting this code in.
 
M

Martin DeMello

Logan Capaldo said:
One last note, this function is gonna rotate like it was on a
cartesian plane (origin in the middle), if Tk's Canvas is like every
other computer coordinate system i've come across, the coordinates are
gonna be only in quadrant IV with the y-value negated, so you may have
to keep that in mind before just pasting this code in.

Usual idiom is to translate the coordinates so that the point you want
to rotate around becomes (0,0), rotate, then translate back.

m.
 
H

Hidetoshi NAGAI

From: Joe Van Dyk <[email protected]>
Subject: Re: Ruby-tk question
Date: Fri, 6 May 2005 14:45:18 +0900
Message-ID: said:
It's horribly difficult for someone who hasn't done math in years.

Here is an example. :)
-----------------------------------------------
require 'tk'

def rotate(deg, x, y, c_x = 0, c_y = 0)
rad = (deg * Math::pI)/180.0
s_rad = Math::sin(rad)
c_rad = Math::cos(rad)

x -= c_x
y -= c_y

[c_x + (x * c_rad - y * s_rad), c_y + (x * s_rad + y * c_rad)]
end

coords = [[100, 100], [200, 100], [100, 140]]
center = [120, 120]

c = TkCanvas.new.pack
poly = TkcPolygon.new(c, coords, :fill=>'red')
TkcOval.new(c,
center[0] - 2, center[1] - 2,
center[0] + 2, center[1] + 2,
:fill=>'black')

deg = 0
TkTimer.start(50, -1, proc{
deg = (deg + 5) % 360
poly.coords(coords.collect{|x, y| rotate(deg, x, y, *center)})
})

Tk.mainloop
 
J

Joe Van Dyk

From: Joe Van Dyk <[email protected]>
Subject: Re: Ruby-tk question
Date: Fri, 6 May 2005 14:45:18 +0900
Message-ID: said:
It's horribly difficult for someone who hasn't done math in years.

Here is an example. :)
-----------------------------------------------
require 'tk'

def rotate(deg, x, y, c_x = 0, c_y = 0)
rad = (deg * Math::pI)/180.0
s_rad = Math::sin(rad)
c_rad = Math::cos(rad)

x -= c_x
y -= c_y

[c_x + (x * c_rad - y * s_rad), c_y + (x * s_rad + y * c_rad)]
end

coords = [[100, 100], [200, 100], [100, 140]]
center = [120, 120]

c = TkCanvas.new.pack
poly = TkcPolygon.new(c, coords, :fill=>'red')
TkcOval.new(c,
center[0] - 2, center[1] - 2,
center[0] + 2, center[1] + 2,
:fill=>'black')

deg = 0
TkTimer.start(50, -1, proc{
deg = (deg + 5) % 360
poly.coords(coords.collect{|x, y| rotate(deg, x, y, *center)})
})

Tk.mainloop

Perfect! Thanks.
 
T

tony summerfelt

That is field of Mathematics. :)
Well according to a random math site:

this sounds like a good idea for a 'numerical recipies in ruby' page.

along the lines of 'numerical recipies in C' and 'numerical recipies
in pascal'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top