Ruby Tk Canvas Drawing

  • Thread starter Bryan Richardson
  • Start date
B

Bryan Richardson

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?
 
P

Phlip

Bryan said:
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.
 
J

Jayson Williams

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

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
 
H

Hidetoshi NAGAI

From: Bryan Richardson <[email protected]>
Subject: Ruby Tk Canvas Drawing
Date: Fri, 4 Apr 2008 00:09:18 +0900
Message-ID: said:
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
 
B

Bryan Richardson

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

--
Bryan

Hidetoshi said:
From: Bryan Richardson <[email protected]>
Subject: Ruby Tk Canvas Drawing
Date: Fri, 4 Apr 2008 00:09:18 +0900
Message-ID: said:
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
 
H

Hidetoshi NAGAI

From: Bryan Richardson <[email protected]>
Subject: Re: Ruby Tk Canvas Drawing
Date: Fri, 4 Apr 2008 06:35:55 +0900
Message-ID: said:
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
 
B

Bryan Richardson

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!
:)
 

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

Similar Threads


Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,196
Latest member
ScottChare

Latest Threads

Top