[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby Tk Canvas Drawing

Bryan Richardson

4/3/2008 3:09:00 PM

Hello all,

I'm trying to draw a network graph in Ruby using Tk. I've figured out
how to create the canvas and draw the nodes and edges. I'm in the
process of implementing a spring graph layout algorithm, and I'm
wondering how I can redraw the nodes and edges at new coordinates when
the new coordinates are calculated using the spring algorithm. I had
planned on creating a new thread every half second or so to call the
spring algorithm and repaint the nodes and edges.

Can anyone tell me how to 'move' an existing TckOval in Ruby Tk?

--
Thanks!
Bryan
--
Posted via http://www.ruby-....

6 Answers

Phlip

4/3/2008 3:26:00 PM

0

Bryan Richardson wrote:

> I'm trying to draw a network graph in Ruby using Tk. I've figured out
> how to create the canvas and draw the nodes and edges. I'm in the
> process of implementing a spring graph layout algorithm, and I'm
> wondering how I can redraw the nodes and edges at new coordinates when
> the new coordinates are calculated using the spring algorithm. I had
> planned on creating a new thread every half second or so to call the
> spring algorithm and repaint the nodes and edges.

I have some nice SvgCanvas example code out there. It uses GraphViz to calculate
the geometry for a graph, then it outputs the result to SVG, and renders this
into a Canvas. This is much better than springing the nodes about yourself,
because you are about to get tangled up (in more ways than one) in all kinds of
constraint heuristics. GraphViz produces camera-ready output, with all kinds of
options for ranking and paring.

Google for my name and SvgCanvas. If my sample code has slipped off the next,
ping me and I will republish it.

--
Phlip

Jayson Williams

4/3/2008 4:54:00 PM

0

On Thu, Apr 3, 2008 at 11:09 AM, Bryan Richardson
<btrichardson@gmail.com> wrote:
> Hello all,
>

> Can anyone tell me how to 'move' an existing TckOval in Ruby Tk?
>
> --
> Thanks!
> Bryan
> --
> Posted via http://www.ruby-....
>
>

I believe you can tag objects on the canvas, and designate x & y
positions for the tags. If you tag your 'oval' you should be able to
move it wherever you want. There are also two very good resources for
Tk development in Ruby. They are both groups on Google. The first
group is called "Ruby and the Tk Toolkit". The other is called "Tk
Documentation and Resources". You should be able to get considerable
help between these two groups.

--Jay

Hidetoshi NAGAI

4/3/2008 5:16:00 PM

0

From: Bryan Richardson <btrichardson@gmail.com>
Subject: Ruby Tk Canvas Drawing
Date: Fri, 4 Apr 2008 00:09:18 +0900
Message-ID: <c60598da19be2f08aeb92d8ab1e6fc1b@ruby-forum.com>
> Can anyone tell me how to 'move' an existing TckOval in Ruby Tk?

For example,
------------------------------------------------------------
canvas = TkCanvas.new.pack

tag = TkcTag.new(canvas)

oval = TkcOval.new(canvas, [100,100], [150,150], :tags=>tag)
line = TkcLine.new(canvas, [100,100], [150,150], :tags=>tag)

oval.move(20, -15) # move
line.move(20, -15) # move

tag.move(30, 20) # move items with tag

oval.coords([50,100], [200,140]) # transform
line.coords([50,50], [100,50], [50,100], [200,140]) # transform
------------------------------------------------------------
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

Bryan Richardson

4/3/2008 9:36:00 PM

0

Thanks for the help guys. Is it possible to only move one end of a line
without moving the other end?

--
Bryan

Hidetoshi NAGAI wrote:
> From: Bryan Richardson <btrichardson@gmail.com>
> Subject: Ruby Tk Canvas Drawing
> Date: Fri, 4 Apr 2008 00:09:18 +0900
> Message-ID: <c60598da19be2f08aeb92d8ab1e6fc1b@ruby-forum.com>
>> Can anyone tell me how to 'move' an existing TckOval in Ruby Tk?
>
> For example,
> ------------------------------------------------------------
> canvas = TkCanvas.new.pack
>
> tag = TkcTag.new(canvas)
>
> oval = TkcOval.new(canvas, [100,100], [150,150], :tags=>tag)
> line = TkcLine.new(canvas, [100,100], [150,150], :tags=>tag)
>
> oval.move(20, -15) # move
> line.move(20, -15) # move
>
> tag.move(30, 20) # move items with tag
>
> oval.coords([50,100], [200,140]) # transform
> line.coords([50,50], [100,50], [50,100], [200,140]) # transform

--
Posted via http://www.ruby-....

Hidetoshi NAGAI

4/4/2008 1:44:00 AM

0

From: Bryan Richardson <btrichardson@gmail.com>
Subject: Re: Ruby Tk Canvas Drawing
Date: Fri, 4 Apr 2008 06:35:55 +0900
Message-ID: <b5c8f55b404ca2eaf9aaef3291d3f857@ruby-forum.com>
> Thanks for the help guys. Is it possible to only move one end of a line
> without moving the other end?

Of course. :-)
---------------------------------------------------------------------
def move_tail(line, dx, dy)
coords = line.coords
coords[-2] += dx
coords[-1] += dy
line.coords = coords
end

def set_tail(line, x, y)
coords = line.coords
coords[-2] = x
coords[-1] = y
line.coords = coords
end

def add_tail(line, x, y)
line.coords <<= [x, y]
end

def remove_tail(line)
coords = line.coords
coords.pop; coords.pop
line.coords = coords
end
---------------------------------------------------------------------
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

Bryan Richardson

4/4/2008 2:55:00 AM

0

Ah yes, I see. I guess I should have asked if there was a 'built-in'
method for moving just one end of a line. Thanks for the code though!
:)
--
Posted via http://www.ruby-....