From: Hidetoshi NAGAI Date: 2008-12-01T16:46:10+09:00 Subject: Re: Ruby Tk and X/Y coordinates From: Johnny Johnnybgood 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 ------------------------------------------------------------------ -- Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)