Forum: Ruby Ruby Tk and X/Y coordinates

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
194346791b17f1875e177d81e7f095bf?d=identicon&s=25 Johnny Johnnybgood (newrubynewbie)
on 2008-11-30 04:37
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!
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2008-11-30 05:32
(Received via mailing list)
Johnny Johnnybgood wrote:
> 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.
E4f967492dbd03c526cc9b397e68021d?d=identicon&s=25 Hidetoshi NAGAI (Guest)
on 2008-12-01 03:25
(Received via mailing list)
From: Johnny Johnnybgood <jarek123@comcast.net>
Subject: Ruby Tk and X/Y coordinates
Date: Sun, 30 Nov 2008 12:32:13 +0900
Message-ID: <ffbd77e2087923a110f8e87022dc8f2f@ruby-forum.com>
> 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.
194346791b17f1875e177d81e7f095bf?d=identicon&s=25 Johnny Johnnybgood (newrubynewbie)
on 2008-12-01 05:51
Hidetoshi NAGAI wrote:
> From: Johnny Johnnybgood <jarek123@comcast.net>
> Subject: Ruby Tk and X/Y coordinates
> Date: Sun, 30 Nov 2008 12:32:13 +0900
> Message-ID: <ffbd77e2087923a110f8e87022dc8f2f@ruby-forum.com>
>> 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.

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
E4f967492dbd03c526cc9b397e68021d?d=identicon&s=25 Hidetoshi NAGAI (Guest)
on 2008-12-01 08:52
(Received via mailing list)
From: Johnny Johnnybgood <jarek123@comcast.net>
Subject: Re: Ruby Tk and X/Y coordinates
Date: Mon, 1 Dec 2008 13:45:46 +0900
Message-ID: <34bb72b14d3241af75850c11c3951c66@ruby-forum.com>
> 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', :outline=>'red')
  TkcOval.new(c, x - delta, y + 15 - delta, x + delta, y + 15 + delta,
              :fill=>'red', :outline=>'red')
  TkcOval.new(c, x - delta*3, y + 30 - delta*3, x + delta*3, y + 30 +
delta*3,
              :fill=>'red', :outline=>'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
This topic is locked and can not be replied to.