Joe Van Dyk
5/6/2005 6:04:00 PM
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.