tcl/tk translate

Discussion in 'Ruby' started by Ed Redman, Sep 14, 2005.

  1. Ed Redman

    Ed Redman Guest

    I am still fairly new to ruby/tk
    I am trying to translate some simple programs from tcl/tk to ruby/tk.
    The following stumps me however. Could someone help me translate.
    Specifically, the foreach {x y} [$w coords $item]. The $w refers to
    TkcPolygons.

    proc poly'rotate {w item angle} {
    set delta [expr {$angle/180.*acos(-1)}]
    foreach {x y} [$w coords $item] {
    set r [expr {hypot($y,$x)}]
    set a [expr {atan2($y,$x)+$delta}]
    lappend coords [expr {cos($a)*$r}] [expr {sin($a)*$r}]

    thank you
     
    Ed Redman, Sep 14, 2005
    #1
    1. Advertising

  2. From: Ed Redman <>
    Subject: tcl/tk translate
    Date: Wed, 14 Sep 2005 10:56:33 +0900
    Message-ID: <>
    > proc poly'rotate {w item angle} {
    > set delta [expr {$angle/180.*acos(-1)}]
    > foreach {x y} [$w coords $item] {
    > set r [expr {hypot($y,$x)}]
    > set a [expr {atan2($y,$x)+$delta}]
    > lappend coords [expr {cos($a)*$r}] [expr {sin($a)*$r}]


    For example,
    ---------------------------------------------------------------
    require 'tk'
    require 'enumerator'

    def poly_rotate(citem, angle)
    delta = (angle * Math::pI)/180.0
    citem.coords = citem.coords.enum_slice(2).collect{|x, y|
    r = Math::hypot(y, x)
    a = Math::atan2(y, x) + delta
    [Math::cos(a) * r, Math::sin(a) * r]
    }
    end

    coords = [[100, 30], [200, 30], [100, 80]]

    c = TkCanvas.new:)height=>480, :width=>480,
    :scrollregion=>[-240, -240, 240, 240]).pack
    poly = TkcPolygon.new(c, coords, :fill=>'red')
    p poly.coords

    TkcOval.new(c, [[-2, -2], [2, 2]], :fill=>'black')

    TkTimer.start(20, -1, proc{poly_rotate(poly, 5)})

    Tk.mainloop
    ---------------------------------------------------------------
    --
    Hidetoshi NAGAI ()
     
    Hidetoshi NAGAI, Sep 14, 2005
    #2
    1. Advertising

  3. Ed Redman

    gregarican Guest

    Hidetoshi NAGAI wrote:

    > TkTimer.start(20, -1, proc{poly_rotate(poly, 5)})



    When I pasted your sample code into a text file and ran it on my
    One-Click Windows install of Ruby 1.8.2 I received the following error
    message:

    undefined method `start' for TkTimer:Class (NoMethodError)

    Then when I tried creating a new instance of a TkTimer class object and
    use the start method like this:

    timer=TkTimer.new
    timer.start(20, -1, proc{poly_rotate(poly, 5)})

    I received an error stating Argument '-1' need to be Proc
    (ArgumentError).

    Just curious if this might be due to the version of Tk that's bundled
    with my Ruby distro...
     
    gregarican, Sep 14, 2005
    #3
  4. From: "gregarican" <>
    Subject: Re: tcl/tk translate
    Date: Wed, 14 Sep 2005 23:21:34 +0900
    Message-ID: <>
    > > TkTimer.start(20, -1, proc{poly_rotate(poly, 5)})

    > When I pasted your sample code into a text file and ran it on my
    > One-Click Windows install of Ruby 1.8.2 I received the following error
    > message:
    > undefined method `start' for TkTimer:Class (NoMethodError)


    Hmmm... I commited TkTimer.start at 2004/10/15.
    Ruby 1.8.2 must have the method.

    TkTimer.start is TkTimer.new + TkTimer#start.
    So, instead of TkTimer.start, you can write:

    TkTimer.new(20, -1, proc{poly_rotate(poly, 5)}).start

    --
    Hidetoshi NAGAI ()
     
    Hidetoshi NAGAI, Sep 15, 2005
    #4
  5. Ed Redman

    gregarican Guest

    Hidetoshi NAGAI wrote:

    > Hmmm... I commited TkTimer.start at 2004/10/15.
    > Ruby 1.8.2 must have the method.


    The start method is present, but I think what's being passed along is
    wrong. Note the error message I receive when creating a new TkTimer
    object and then using the start method:

    timer=TkTimer.new
    timer.start(20, -1, proc{poly_rotate(poly, 5)})


    ArgumentError: Argument '-1' need to be Proc
    (ArgumentError).
    from c:/ruby/lib/ruby/1.8/tk/timer.rb:338:in 'start'
     
    gregarican, Sep 15, 2005
    #5
  6. From: "gregarican" <>
    Subject: Re: tcl/tk translate
    Date: Fri, 16 Sep 2005 02:51:34 +0900
    Message-ID: <>
    > The start method is present, but I think what's being passed along is
    > wrong. Note the error message I receive when creating a new TkTimer
    > object and then using the start method:
    >
    > timer=TkTimer.new
    > timer.start(20, -1, proc{poly_rotate(poly, 5)})


    You'll misunderstand about arguments of those methods.
    Please read my last mail again.

    From: Hidetoshi NAGAI <>
    Subject: Re: tcl/tk translate
    Date: Thu, 15 Sep 2005 11:31:32 +0900
    Message-ID: <>
    > TkTimer.start is TkTimer.new + TkTimer#start.
    > So, instead of TkTimer.start, you can write:
    >
    > TkTimer.new(20, -1, proc{poly_rotate(poly, 5)}).start


    That is,
    -----------------------------------------------------------------
    timer = TkTimer.new(20, -1, proc{poly_rotate(poly, 5)})
    timer.start
    -----------------------------------------------------------------
    --
    Hidetoshi NAGAI ()
     
    Hidetoshi NAGAI, Sep 16, 2005
    #6
  7. Ed Redman

    gregarican Guest

    Hidetoshi NAGAI wrote:

    > You'll misunderstand about arguments of those methods.
    > Please read my last mail again.


    Sorry. I forgot to require enumerable. That's why it bombed. Doooh!
     
    gregarican, Sep 16, 2005
    #7
    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. Sridhar Hegde
    Replies:
    2
    Views:
    2,573
    Ajeetha Kumari
    Sep 13, 2004
  2. Rakesh Sharma
    Replies:
    2
    Views:
    2,999
    Thomas Rudloff
    Oct 22, 2004
  3. Nisheeth

    post translate simulation

    Nisheeth, Apr 29, 2005, in forum: VHDL
    Replies:
    2
    Views:
    877
    Mike Treseler
    Apr 29, 2005
  4. Replies:
    3
    Views:
    632
    Bert Cuzeau
    Jun 22, 2005
  5. Mumia W.

    Inline::Tcl vs. Inline::Tcl

    Mumia W., Aug 23, 2006, in forum: Perl Misc
    Replies:
    0
    Views:
    189
    Mumia W.
    Aug 23, 2006
Loading...

Share This Page