tcl/tk translate

E

Ed Redman

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
 
H

Hidetoshi NAGAI

From: Ed Redman <[email protected]>
Subject: tcl/tk translate
Date: Wed, 14 Sep 2005 10:56:33 +0900
Message-ID: said:
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
 
G

gregarican

Hidetoshi said:
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...
 
H

Hidetoshi NAGAI

From: "gregarican" <[email protected]>
Subject: Re: tcl/tk translate
Date: Wed, 14 Sep 2005 23:21:34 +0900
Message-ID: said:
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
 
G

gregarican

Hidetoshi said:
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'
 
H

Hidetoshi NAGAI

From: "gregarican" <[email protected]>
Subject: Re: tcl/tk translate
Date: Fri, 16 Sep 2005 02:51:34 +0900
Message-ID: said:
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 <[email protected]>
Subject: Re: tcl/tk translate
Date: Thu, 15 Sep 2005 11:31:32 +0900
Message-ID: said:
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,
 
G

gregarican

Hidetoshi said:
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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top