[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby-tk question

Joe Van Dyk

5/6/2005 12:24:00 AM

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?



11 Answers

Joe Van Dyk

5/6/2005 12:43:00 AM

0

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?
>



Hidetoshi NAGAI

5/6/2005 1:32:00 AM

0

Joe Van Dyk

5/6/2005 1:42:00 AM

0

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.



Hidetoshi NAGAI

5/6/2005 1:54:00 AM

0

Joe Van Dyk

5/6/2005 5:45:00 AM

0

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.



Logan Capaldo

5/6/2005 9:00:00 AM

0

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
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/...



Logan Capaldo

5/6/2005 9:09:00 AM

0

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
> 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/...
>

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.



Martin DeMello

5/6/2005 9:52:00 AM

0

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.

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

m.

Hidetoshi NAGAI

5/6/2005 2:42:00 PM

0

Joe Van Dyk

5/6/2005 6:04:00 PM

0

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)]
> 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.