ruby challenge 9

connect the dots

After struggling with the last challenge for so long (before giving up and using python), I wanted to study up on ruby some more. I took a break from the python ruby challenge and started reading Programing Ruby: the pragmatic programmers guide, the free online edition. The free online edition is outdated, so I would probably be well served by purchasing a copy that reflects the current state of the language.

I couldn’t stay away for long, especially since challenge 9 is another ImageMagick challenge, and ImageMagick and RMagick are pretty well documented. The challenge presents you with a picture of a tree with large black dots outlining it. Since the Title is ‘connect the dots’,  it’s a good assumtion that the long lists of numbers are ‘dot’ coordinates. It didn’t take me to long to figure out how to take the coordinates given and convert them into polygons, but that required hand editing of the data given since the img.polygon(x1,y1,x2,y2…) only accepts numbers explicitly. Since I wanted the computer to deal with the data raw, it seemed easier to just draw lines. Here is what I came up with;

#!/usr/bin/ruby
require 'RMagick'
include Magick
first = "lots, of, number"
second = "lots, of, numbers"
def lineDraw(xyList)
  nAr = xyList.split(',')
  canvas = ImageList.new('./ruby9.png')
  x = 0
  nLine = Draw.new
  while x+3 <= nAr.length
    nLine.line(nAr[x].to_i, nAr[x+1].to_i, nAr[x+2].to_i, nAr[x+3].to_i)
    nLine.draw(canvas)
    x += 2
  end
  canvas.write("./ruby9.png")
end
Image.new(500, 500).write("./ruby9.png")
lineDraw(first)
lineDraw(second)

Which draws a picture as a hint to the next challenge page which presents you with the image that they mapped the coordinates from, and this text; ‘len(a[30]) = ?’. This is as far as I got last time (with python), and though I have a pretty good idea how to solve it, I have yet to try.

Next: challenge 10; what are you lookin at?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s