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?
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?
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.
On 5/5/05, Joe Van Dyk <joevandyk@gmail.com> wrote:
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?
Message-ID: <c715e64050505172319751570@mail.gmail.com>
What's the easiest way to rotate a Tk Canvas Polygon?
Probably, you'll have to calculate coords by yourself.
From: Joe Van Dyk <joevandyk@gmail.com>
Subject: Ruby-tk question
Date: Fri, 6 May 2005 09:24:01 +0900
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
Hm, I have no idea how to do that. Trig was so long ago.
On 5/5/05, Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp> wrote:
From: Joe Van Dyk <joevandyk@gmail.com>
Subject: Ruby-tk question
Date: Fri, 6 May 2005 09:24:01 +0900
Message-ID: <c715e64050505172319751570@mail.gmail.com>
> What's the easiest way to rotate a Tk Canvas Polygon?Probably, you'll have to calculate coords by yourself.
Message-ID: <c715e6405050518413e8952ea@mail.gmail.com>
From: Joe Van Dyk <joevandyk@gmail.com>
Subject: Re: Ruby-tk question
Date: Fri, 6 May 2005 10:41:39 +0900
On 5/5/05, Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp> wrote:
> From: Joe Van Dyk <joevandyk@gmail.com>
> Subject: Ruby-tk question
> Date: Fri, 6 May 2005 09:24:01 +0900
> Message-ID: <c715e64050505172319751570@mail.gmail.com>
> > What's the easiest way to rotate a Tk Canvas Polygon?
> Probably, you'll have to calculate coords by yourself.
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.
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
It's horribly difficult for someone who hasn't done math in years.
On 5/5/05, Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp> wrote:
From: Joe Van Dyk <joevandyk@gmail.com>
Subject: Re: Ruby-tk question
Date: Fri, 6 May 2005 10:41:39 +0900
Message-ID: <c715e6405050518413e8952ea@mail.gmail.com>> On 5/5/05, Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp> wrote:
> > From: Joe Van Dyk <joevandyk@gmail.com>
> > Subject: Ruby-tk question
> > Date: Fri, 6 May 2005 09:24:01 +0900
> > Message-ID: <c715e64050505172319751570@mail.gmail.com>
> > > What's the easiest way to rotate a Tk Canvas Polygon?
> > Probably, you'll have to calculate coords by yourself.
> 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.
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
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
On 5/6/05, Joe Van Dyk <joevandyk@gmail.com> wrote:
On 5/5/05, Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp> wrote:
> From: Joe Van Dyk <joevandyk@gmail.com>
> Subject: Re: Ruby-tk question
> Date: Fri, 6 May 2005 10:41:39 +0900
> Message-ID: <c715e6405050518413e8952ea@mail.gmail.com>
>
> > On 5/5/05, Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp> wrote:
> > > From: Joe Van Dyk <joevandyk@gmail.com>
> > > Subject: Ruby-tk question
> > > Date: Fri, 6 May 2005 09:24:01 +0900
> > > Message-ID: <c715e64050505172319751570@mail.gmail.com>
> > > > What's the easiest way to rotate a Tk Canvas Polygon?
> > > Probably, you'll have to calculate coords by yourself.
> > 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.
> --
> Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
>It's horribly difficult for someone who hasn't done math in years.
Message-ID: <c715e6405050522443f393a8c@mail.gmail.com>
It's horribly difficult for someone who hasn't done math in years.
Here is an example. ![]()
From: Joe Van Dyk <joevandyk@gmail.com>
Subject: Re: Ruby-tk question
Date: Fri, 6 May 2005 14:45:18 +0900
-----------------------------------------------
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
-----------------------------------------------
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
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.
On 5/6/05, Logan Capaldo <logancapaldo@gmail.com> wrote:
On 5/6/05, Joe Van Dyk <joevandyk@gmail.com> wrote:
> On 5/5/05, Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp> wrote:
> > From: Joe Van Dyk <joevandyk@gmail.com>
> > Subject: Re: Ruby-tk question
> > Date: Fri, 6 May 2005 10:41:39 +0900
> > Message-ID: <c715e6405050518413e8952ea@mail.gmail.com>
> >
> > > On 5/5/05, Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp> wrote:
> > > > From: Joe Van Dyk <joevandyk@gmail.com>
> > > > Subject: Ruby-tk question
> > > > Date: Fri, 6 May 2005 09:24:01 +0900
> > > > Message-ID: <c715e64050505172319751570@mail.gmail.com>
> > > > > What's the easiest way to rotate a Tk Canvas Polygon?
> > > > Probably, you'll have to calculate coords by yourself.
> > > 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.
> > --
> > Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
> >
>
> 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
enddef 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]
endseems 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:
Classroom Resources - National Council of Teachers of Mathematics
Perfect! Thanks.
On 5/6/05, Hidetoshi NAGAI <nagai@ai.kyutech.ac.jp> wrote:
From: Joe Van Dyk <joevandyk@gmail.com>
Subject: Re: Ruby-tk question
Date: Fri, 6 May 2005 14:45:18 +0900
Message-ID: <c715e6405050522443f393a8c@mail.gmail.com>
> 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)]
endcoords = [[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
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'
--
http://home.cogeco.ca/~tsummerfelt1
telnet://ventedspleen.dyndns.org
Usual idiom is to translate the coordinates so that the point you want
to rotate around becomes (0,0), rotate, then translate back.
m.
Logan Capaldo <logancapaldo@gmail.com> wrote:
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.