Ruby Tk and X/Y coordinates

J

Johnny Johnnybgood

Hi everyone,

I'm totally new to Ruby...I was wondering if anyone could help me out
with a little problem I'm having. I want to be able to draw X/Y
coordinates on my screen using Tk library. I tried to decipher the docs
available online but I guess my skills aren't where they need to be yet
in order to understand them.

Any help would be greatly appreciated (a simple example would be
awesome). Thanks!
 
J

Joel VanderWerf

Johnny said:
Hi everyone,

I'm totally new to Ruby...I was wondering if anyone could help me out
with a little problem I'm having. I want to be able to draw X/Y
coordinates on my screen using Tk library. I tried to decipher the docs
available online but I guess my skills aren't where they need to be yet
in order to understand them.

Any help would be greatly appreciated (a simple example would be
awesome). Thanks!

Dig around in the tk samples dir in the ruby dsitribution.... look for
ones using tk canvas. It makes life pretty easy, but ask back here if
you get stuck.
 
H

Hidetoshi NAGAI

From: Johnny Johnnybgood <[email protected]>
Subject: Ruby Tk and X/Y coordinates
Date: Sun, 30 Nov 2008 12:32:13 +0900
Message-ID: said:
with a little problem I'm having. I want to be able to draw X/Y
coordinates on my screen using Tk library. I tried to decipher the docs

What do you mean with the word "screen" ?
If you mean "draw lines or figures on the *application* window",
a canvas widget can help you.
Of course, you can convert screen (desktop) coordinates to
window coordinates, and control (move) Ruby/Tk windows by
screen coordinates.
But, if you mean "draw on the desktop directry",
it is not Ruby/Tk's business.
 
J

Johnny Johnnybgood

Hidetoshi said:
From: Johnny Johnnybgood <[email protected]>
Subject: Ruby Tk and X/Y coordinates
Date: Sun, 30 Nov 2008 12:32:13 +0900


What do you mean with the word "screen" ?
If you mean "draw lines or figures on the *application* window",
a canvas widget can help you.
Of course, you can convert screen (desktop) coordinates to
window coordinates, and control (move) Ruby/Tk windows by
screen coordinates.
But, if you mean "draw on the desktop directry",
it is not Ruby/Tk's business.

What I'm trying to do is this: I have an array of x,y coordinates
(example=[[0,0], [1,1], [2,2], [3,3], [50,50], etc]...I want my script
to create an *application* window the size of my screen (1024, 768) and
draw dots (pixels) at the X/Y coordinates provided by my array.

I know that with lines, "to create a line, the one piece of information
you'll need to specify is where the line should be. This is done by
using the coordinates of the starting and ending point, expressed as a
list of the form x0 y0 x1 y1." I guess if I set the x1 y1 to x0 y0 then
I should just get dots and not lines (I don't know if that will even
work but I'll test it). Is there an easier way (something that's
specifically created for drawing x/y coordinates)? Thanks
 
H

Hidetoshi NAGAI

From: Johnny Johnnybgood <[email protected]>
Subject: Re: Ruby Tk and X/Y coordinates
Date: Mon, 1 Dec 2008 13:45:46 +0900
Message-ID: said:
What I'm trying to do is this: I have an array of x,y coordinates
(example=[[0,0], [1,1], [2,2], [3,3], [50,50], etc]...I want my script
to create an *application* window the size of my screen (1024, 768) and
draw dots (pixels) at the X/Y coordinates provided by my array.

To draw dots, please use an Oval item or a very short Line item.

There are some ways to fill your screen; safe or danger,
depend on OS/window-manager or not, and dened on Tcl/Tk version or not.

One, which is safe and not depend on an environment, is
-------------------------------------------------------------------
root = Tk.root
width, height = root.maxsize
root.geometry "#{width}x#{height}+0+0"
-------------------------------------------------------------------

But, it sometimes fails to fill the screen because of a border given
by a window-manager.

If you use Windows or MacOS X, you can use "Tk.root.state:)zoomed)".
Or, if you use Ruby/Tk with Tcl/Tk8.5 on X Window System
and your window-manager support 'zoomed', you can use
"Tk.root.attributes:)zoomed=>true)".

A danger one is,
-------------------------------------------------------------------
root = Tk.root
root.bind('1'){exit} # for SAFE; Click the left button, and exit.
width = root.winfo_screenwidth
height = root.winfo_screenheight
root.withdraw
root.overrideredirect true # NOT controlled by a window-manager.
root.deiconify
root.geometry "#{width}x#{height}+0+0"
-------------------------------------------------------------------

This will fill your screen completely.
The application is NOT under control of a window-manager.
So, if you forget to make the way to exit the application,
you may have to shutdown your window system.

If you use Tcl/Tk8.5, another version of the danger one is,
-------------------------------------------------------------------
root = Tk.root
root.bind('1'){exit} # for SAFE; Click the left button, and exit.
root.attributes:)fullscreen=>true)
-------------------------------------------------------------------
I know that with lines, "to create a line, the one piece of information
you'll need to specify is where the line should be. This is done by
using the coordinates of the starting and ending point, expressed as a
list of the form x0 y0 x1 y1." I guess if I set the x1 y1 to x0 y0 then
I should just get dots and not lines (I don't know if that will even
work but I'll test it). Is there an easier way (something that's
specifically created for drawing x/y coordinates)? Thanks

If you want dashed lines instead of solid lines,
"dash" option may be usefull.

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

c = TkCanvas.new:)width=>200, :height=>400).pack

# put dots
samples = [
[10,10], [15,15], [20,20], [25,25], [30,30], [35,35], [40,40], [45,45],
[50,50], [55,55], [60,60], [65,65], [70,70], [75,75], [80,80], [85,85],
[90,90], [95,95], [100,100], [105,105], [110,110], [115,115], [120,120],
[125,125], [130,130], [135,135], [140,140], [145,145], [150,150]
]
delta = 0.5
samples.each{|x,y|
TkcOval.new(c, x, y, x, y, :fill=>'red', :eek:utline=>'red')
TkcOval.new(c, x - delta, y + 15 - delta, x + delta, y + 15 + delta,
:fill=>'red', :eek:utline=>'red')
TkcOval.new(c, x - delta*3, y + 30 - delta*3, x + delta*3, y + 30 + delta*3,
:fill=>'red', :eek:utline=>'red')
}

# very short lines
samples.each{|x,y|
TkcLine.new(c, x+30, y, x+30+0.5, y+0.5, :fill=>'black', :width=>1)
}

# dash line
coords = [[10, 80], [150, 220]]
TkcLine.new(c, coords, :fill=>'red', :width=>1, :dash=>'.')

coords.map!{|x,y| [x, y+15]}
TkcLine.new(c, coords, :fill=>'red', :width=>3, :dash=>'.')

coords.map!{|x,y| [x, y+15]}
TkcLine.new(c, coords, :fill=>'red', :width=>1, :dash=>'-')

coords.map!{|x,y| [x, y+15]}
TkcLine.new(c, coords, :fill=>'red', :width=>3, :dash=>'-')

coords.map!{|x,y| [x, y+15]}
TkcLine.new(c, coords, :fill=>'red', :width=>1, :dash=>'-.')

coords.map!{|x,y| [x, y+15]}
TkcLine.new(c, coords, :fill=>'red', :width=>3, :dash=>'-.')

coords.map!{|x,y| [x, y+15]}
TkcLine.new(c, coords, :fill=>'red', :width=>1, :dash=>[2,2])

coords.map!{|x,y| [x, y+15]}
TkcLine.new(c, coords, :fill=>'red', :width=>3, :dash=>[2,2])

coords.map!{|x,y| [x, y+15]}
TkcLine.new(c, coords, :fill=>'red', :width=>1, :dash=>[6,2,2,2])

coords.map!{|x,y| [x, y+15]}
TkcLine.new(c, coords, :fill=>'red', :width=>3, :dash=>[6,2,2,2])

coords.map!{|x,y| [x, y+15]}
TkcLine.new(c, coords, :fill=>'red', :width=>3, :dash=>[12,4,4,4])

Tk.mainloop
 

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,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top