Ruby-tk question

Discussion in 'Ruby' started by Joe Van Dyk, May 6, 2005.

  1. Joe Van Dyk

    Joe Van Dyk Guest

    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?
    Joe Van Dyk, May 6, 2005
    #1
    1. Advertising

  2. Joe Van Dyk

    Joe Van Dyk Guest

    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 <> 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?
    >
    Joe Van Dyk, May 6, 2005
    #2
    1. Advertising

  3. From: Joe Van Dyk <>
    Subject: Ruby-tk question
    Date: Fri, 6 May 2005 09:24:01 +0900
    Message-ID: <>
    > What's the easiest way to rotate a Tk Canvas Polygon?


    Probably, you'll have to calculate coords by yourself.
    --
    Hidetoshi NAGAI ()
    Hidetoshi NAGAI, May 6, 2005
    #3
  4. Joe Van Dyk

    Joe Van Dyk Guest

    On 5/5/05, Hidetoshi NAGAI <> wrote:
    > From: Joe Van Dyk <>
    > Subject: Ruby-tk question
    > Date: Fri, 6 May 2005 09:24:01 +0900
    > Message-ID: <>
    > > 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.
    Joe Van Dyk, May 6, 2005
    #4
  5. From: Joe Van Dyk <>
    Subject: Re: Ruby-tk question
    Date: Fri, 6 May 2005 10:41:39 +0900
    Message-ID: <>

    > On 5/5/05, Hidetoshi NAGAI <> wrote:
    > > From: Joe Van Dyk <>
    > > Subject: Ruby-tk question
    > > Date: Fri, 6 May 2005 09:24:01 +0900
    > > Message-ID: <>
    > > > 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 ()
    Hidetoshi NAGAI, May 6, 2005
    #5
  6. Joe Van Dyk

    Joe Van Dyk Guest

    On 5/5/05, Hidetoshi NAGAI <> wrote:
    > From: Joe Van Dyk <>
    > Subject: Re: Ruby-tk question
    > Date: Fri, 6 May 2005 10:41:39 +0900
    > Message-ID: <>
    >
    > > On 5/5/05, Hidetoshi NAGAI <> wrote:
    > > > From: Joe Van Dyk <>
    > > > Subject: Ruby-tk question
    > > > Date: Fri, 6 May 2005 09:24:01 +0900
    > > > Message-ID: <>
    > > > > 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 ()
    >


    It's horribly difficult for someone who hasn't done math in years.
    Joe Van Dyk, May 6, 2005
    #6
  7. On 5/6/05, Joe Van Dyk <> wrote:
    > On 5/5/05, Hidetoshi NAGAI <> wrote:
    > > From: Joe Van Dyk <>
    > > Subject: Re: Ruby-tk question
    > > Date: Fri, 6 May 2005 10:41:39 +0900
    > > Message-ID: <>
    > >
    > > > On 5/5/05, Hidetoshi NAGAI <> wrote:
    > > > > From: Joe Van Dyk <>
    > > > > Subject: Ruby-tk question
    > > > > Date: Fri, 6 May 2005 09:24:01 +0900
    > > > > Message-ID: <>
    > > > > > 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 ()
    > >

    >
    > 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
    Logan Capaldo, May 6, 2005
    #7
  8. On 5/6/05, Logan Capaldo <> wrote:
    > On 5/6/05, Joe Van Dyk <> wrote:
    > > On 5/5/05, Hidetoshi NAGAI <> wrote:
    > > > From: Joe Van Dyk <>
    > > > Subject: Re: Ruby-tk question
    > > > Date: Fri, 6 May 2005 10:41:39 +0900
    > > > Message-ID: <>
    > > >
    > > > > On 5/5/05, Hidetoshi NAGAI <> wrote:
    > > > > > From: Joe Van Dyk <>
    > > > > > Subject: Ruby-tk question
    > > > > > Date: Fri, 6 May 2005 09:24:01 +0900
    > > > > > Message-ID: <>
    > > > > > > 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 ()
    > > >

    > >
    > > 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.
    Logan Capaldo, May 6, 2005
    #8
  9. Logan Capaldo <> 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.
    Martin DeMello, May 6, 2005
    #9
  10. From: Joe Van Dyk <>
    Subject: Re: Ruby-tk question
    Date: Fri, 6 May 2005 14:45:18 +0900
    Message-ID: <>
    > 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
    -----------------------------------------------
    --
    Hidetoshi NAGAI ()
    Hidetoshi NAGAI, May 6, 2005
    #10
  11. Joe Van Dyk

    Joe Van Dyk Guest

    On 5/6/05, Hidetoshi NAGAI <> wrote:
    > From: Joe Van Dyk <>
    > Subject: Re: Ruby-tk question
    > Date: Fri, 6 May 2005 14:45:18 +0900
    > Message-ID: <>
    > > 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.
    Joe Van Dyk, May 6, 2005
    #11
  12. tony summerfelt, May 9, 2005
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    204
  2. anne001
    Replies:
    1
    Views:
    402
  3. Phrogz
    Replies:
    4
    Views:
    220
    Austin Ziegler
    Sep 6, 2006
  4. roschler
    Replies:
    0
    Views:
    174
    roschler
    Oct 16, 2006
  5. Nicholas
    Replies:
    3
    Views:
    375
    Ryan Davis
    Jan 28, 2007
Loading...

Share This Page