# Ruby Tk and X/Y coordinates

**URL:** <https://www.ruby-forum.com/t/ruby-tk-and-x-y-coordinates/154469>\
**Category:** Ruby\
**Created:** [November 30, 2008, 4:37am UTC](https://www.ruby-forum.com/t/ruby-tk-and-x-y-coordinates/154469 "2008-11-30T04:37:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![newrubynewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e79b87/32.png) [@newrubynewbie](https://www.ruby-forum.com/u/newrubynewbie)\
**Post date:** [November 30, 2008, 4:37am UTC](https://www.ruby-forum.com/t/ruby-tk-and-x-y-coordinates/154469/1 "2008-11-30T04:37:28Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![newrubynewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e79b87/32.png) [@newrubynewbie](https://www.ruby-forum.com/u/newrubynewbie)\
**Post date:** [November 30, 2008, 5:32am UTC](https://www.ruby-forum.com/t/ruby-tk-and-x-y-coordinates/154469/2 "2008-11-30T05:32:28Z")

</div>

Johnny J. 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.

---

<div class="post-metadata">

**Author:** ![newrubynewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e79b87/32.png) [@newrubynewbie](https://www.ruby-forum.com/u/newrubynewbie)\
**Post date:** [December 1, 2008, 3:25am UTC](https://www.ruby-forum.com/t/ruby-tk-and-x-y-coordinates/154469/3 "2008-12-01T03:25:56Z")

</div>

From: Johnny J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Ruby Tk and X/Y coordinates  
Date: Sun, 30 Nov 2008 12:32:13 +0900  
Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> 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.

---

<div class="post-metadata">

**Author:** ![newrubynewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e79b87/32.png) [@newrubynewbie](https://www.ruby-forum.com/u/newrubynewbie)\
**Post date:** [December 1, 2008, 5:51am UTC](https://www.ruby-forum.com/t/ruby-tk-and-x-y-coordinates/154469/4 "2008-12-01T05:51:09Z")

</div>

Hidetoshi NAGAI wrote:

> From: Johnny J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> Subject: Ruby Tk and X/Y coordinates  
> Date: Sun, 30 Nov 2008 12:32:13 +0900  
> Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
> 
> > 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

---

<div class="post-metadata">

**Author:** ![newrubynewbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/e79b87/32.png) [@newrubynewbie](https://www.ruby-forum.com/u/newrubynewbie)\
**Post date:** [December 1, 2008, 8:52am UTC](https://www.ruby-forum.com/t/ruby-tk-and-x-y-coordinates/154469/5 "2008-12-01T08:52:21Z")

</div>

From: Johnny J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: Ruby Tk and X/Y coordinates  
Date: Mon, 1 Dec 2008 13:45:46 +0900  
Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> 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
