# Ruby-tk question

**URL:** <https://rubytalk.org/t/ruby-tk-question/18173>\
**Category:** ruby-talk\
**Created:** [6 May 2005 00:24 UTC](https://rubytalk.org/t/ruby-tk-question/18173 "2005-05-06T00:24:01Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![J-Van](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@J-Van](https://rubytalk.org/u/J-Van)\
**Post date:** [6 May 2005 00:24 UTC](https://rubytalk.org/t/ruby-tk-question/18173/1 "2005-05-06T00:24:01Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![J-Van](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@J-Van](https://rubytalk.org/u/J-Van)\
**Post date:** [6 May 2005 00:43 UTC](https://rubytalk.org/t/ruby-tk-question/18173/2 "2005-05-06T00:43:13Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Hidetoshi\_NAGAI](https://avatars.discourse-cdn.com/v4/letter/h/49beb7/32.png) [@Hidetoshi\_NAGAI](https://rubytalk.org/u/Hidetoshi_NAGAI)\
**Post date:** [6 May 2005 01:31 UTC](https://rubytalk.org/t/ruby-tk-question/18173/3 "2005-05-06T01:31:53Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![J-Van](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@J-Van](https://rubytalk.org/u/J-Van)\
**Post date:** [6 May 2005 01:41 UTC](https://rubytalk.org/t/ruby-tk-question/18173/4 "2005-05-06T01:41:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Hidetoshi\_NAGAI](https://avatars.discourse-cdn.com/v4/letter/h/49beb7/32.png) [@Hidetoshi\_NAGAI](https://rubytalk.org/u/Hidetoshi_NAGAI)\
**Post date:** [6 May 2005 01:54 UTC](https://rubytalk.org/t/ruby-tk-question/18173/5 "2005-05-06T01:54:27Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![J-Van](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@J-Van](https://rubytalk.org/u/J-Van)\
**Post date:** [6 May 2005 05:45 UTC](https://rubytalk.org/t/ruby-tk-question/18173/6 "2005-05-06T05:45:18Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [6 May 2005 08:59 UTC](https://rubytalk.org/t/ruby-tk-question/18173/7 "2005-05-06T08:59:33Z")

</div>

Well according to a random math site:

def deg2rad(x)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(x \* Math::PI) / 180.0  
end

def rotate(x, y, deg)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r = Math.sqrt(x\*\*2 + y\*\*2)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theta = Math.atan(y/x)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;u = r \* Math.cos(theta + deg2rad(deg))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;v = r \* Math.sin(theta + deg2rad(deg))  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[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](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.

---

<div class="post-metadata">

**Author:** ![Hidetoshi\_NAGAI](https://avatars.discourse-cdn.com/v4/letter/h/49beb7/32.png) [@Hidetoshi\_NAGAI](https://rubytalk.org/u/Hidetoshi_NAGAI)\
**Post date:** [6 May 2005 14:41 UTC](https://rubytalk.org/t/ruby-tk-question/18173/8 "2005-05-06T14:41:53Z")

</div>

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)  
> &nbsp;&nbsp;rad = (deg \* Math::PI)/180.0  
> &nbsp;&nbsp;s\_rad = Math::sin(rad)  
> &nbsp;&nbsp;c\_rad = Math::cos(rad)
> 
> &nbsp;&nbsp;x -= c\_x  
> &nbsp;&nbsp;y -= c\_y
> 
> &nbsp;&nbsp;[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,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;center[0] - 2, center[1] - 2,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;center[0] + 2, center[1] + 2,  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:fill=\>'black')
> 
> deg = 0  
> TkTimer.start(50, -1, proc{  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;deg = (deg + 5) % 360  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;poly.coords(coords.collect{|x, y| rotate(deg, x, y, \*center)})  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;})
> 
> Tk.mainloop  
> -----------------------------------------------  
> --  
> Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [6 May 2005 09:08 UTC](https://rubytalk.org/t/ruby-tk-question/18173/9 "2005-05-06T09:08:51Z")

</div>

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)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(x \* Math::PI) / 180.0  
> > end
> > 
> > def rotate(x, y, deg)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r = Math.sqrt(x\*\*2 + y\*\*2)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;theta = Math.atan(y/x)  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;u = r \* Math.cos(theta + deg2rad(deg))  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;v = r \* Math.sin(theta + deg2rad(deg))  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[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:  
> > [Classroom Resources - National Council of Teachers of Mathematics](http://mathforum.org/library/drmath/view/63184.html)

---

<div class="post-metadata">

**Author:** ![J-Van](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@J-Van](https://rubytalk.org/u/J-Van)\
**Post date:** [6 May 2005 18:03 UTC](https://rubytalk.org/t/ruby-tk-question/18173/10 "2005-05-06T18:03:33Z")

</div>

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)  
> > &nbsp;&nbsp;rad = (deg \* Math::PI)/180.0  
> > &nbsp;&nbsp;s\_rad = Math::sin(rad)  
> > &nbsp;&nbsp;c\_rad = Math::cos(rad)
> > 
> > &nbsp;&nbsp;x -= c\_x  
> > &nbsp;&nbsp;y -= c\_y
> > 
> > &nbsp;&nbsp;[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,  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;center[0] - 2, center[1] - 2,  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;center[0] + 2, center[1] + 2,  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:fill=\>'black')
> > 
> > deg = 0  
> > TkTimer.start(50, -1, proc{  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;deg = (deg + 5) % 360  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;poly.coords(coords.collect{|x, y| rotate(deg, x, y, \*center)})  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;})
> > 
> > Tk.mainloop

---

<div class="post-metadata">

**Author:** ![tony\_summerfelt](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@tony\_summerfelt](https://rubytalk.org/u/tony_summerfelt)\
**Post date:** [8 May 2005 23:20 UTC](https://rubytalk.org/t/ruby-tk-question/18173/11 "2005-05-08T23:20:20Z")

</div>

> > > 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](http://home.cogeco.ca/~tsummerfelt1)  
> telnet://ventedspleen.dyndns.org

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello1](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@Martin\_DeMello1](https://rubytalk.org/u/Martin_DeMello1)\
**Post date:** [6 May 2005 09:54 UTC](https://rubytalk.org/t/ruby-tk-question/18173/12 "2005-05-06T09:54:25Z")

</div>

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.
