Re: [ruby-102] [QUIZ] Mathematical Image Generator (#191)

From: David W.
Sent on: Monday, February 9, 2009 3:05 PM
I wrote two versions of this, one using Shoes and the other using Ruby
Processing. Both use pretty much the same graphics model so they're
nearly identical. Though, I didn't find a way to draw individual
pixels in Shoes directly so I used 1x1 rects instead. I also ran into
a problem with processing where about half the time immediately after
rendering the image it would blank the windows I'm not sure if it's a
problem with my code, Processing, Java or my system. I also didn't do
anything fancy with the functions. I just used the examples provided.

Anyway, here's the Shoes code:

Shoes.app :width => 200, :height => 200 do
  r_proc = lambda { |xf, yf| Math.sin(Math::PI * Math.cos(Math::PI *
yf * xf)).abs }
  g_proc = lambda { |xf, yf| Math.sin(Math::PI * (Math.sin(Math::PI *
yf)) ** 3).abs }
  b_proc = lambda { |xf, yf| ((Math.cos(Math::PI * Math.cos(Math::PI *
yf))) ** 3).abs }

  (0...width).each do |x|
    (0...height).each do |y|
      xf = x/width.to_f
      yf = y/height.to_f
      r = r_proc.call(xf, yf)
      g = g_proc.call(xf, yf)
      b = b_proc.call(xf, yf)
      # puts "r: #{r}, g: #{g}, b: #{b}"
      stroke rgb(r, g, b)
      fill rgb(r, g, b)
      rect(x, y, 1, 1)
      #point x, y
    end
  end
end

And the Processing code:

require 'ruby-processing'

class RubyQuiz191 < Processing::App
  def setup
    r_proc = lambda { |xf, yf| Math.sin(Math::PI * Math.cos(Math::PI *
yf * xf)).abs }
    g_proc = lambda { |xf, yf| Math.sin(Math::PI * (Math.sin(Math::PI
* yf)) ** 3).abs }
    b_proc = lambda { |xf, yf| ((Math.cos(Math::PI * Math.cos(Math::PI
* yf))) ** 3).abs }

    color_mode RGB, 1

    (0...width).each do |x|
      (0...height).each do |y|
        xf = x/width.to_f
        yf = y/height.to_f
        r = r_proc.call(xf, yf).abs
        g = g_proc.call(xf, yf).abs
        b = b_proc.call(xf, yf).abs
        stroke r, g, b
        point x, y
      end
    end
  end

  def draw; end
end

RubyQuiz191.new :title => "RubyQuiz191", :width => 200, :height => 200

I haven't tried either of these at 1600x1200 as they're both dog slow.

Spartacus (aka David Whittington)

On Fri, Feb 6, 2009 at 12:14 PM, Mark Wilkins <[address removed]> wrote:
> Greetings, all:
>
> Here's the quiz.  I'm not sure how far I'm going to get on this one.
> Please hold your solutions until Monday.
>
>
> ## Mathematical Image Generator (#191)
>
> This week's quiz is about the generation of images based on
> mathematical functions. The red, green, and blue values at each point
> in the image will each be determined by a separate function based on
> the coordinates at that position.
>
> Here are some functions to get started:
>
> Math.sin(Math::PI * ?)
> Math.cos(Math::PI * ?)
> (? + ?)/2
> ? * ?
> (?) ** 3
>
> The question marks represent the parameters to the functions (x, y, or
> another function). The x and y values range between 0 and 1: the
> proportion of the current position to the total width or height.
>
> Feel free to add your own functions, but keep in mind that they work
> best when the inputs and output are between -1 and 1.
>
> Example Output:
>
> http://strd6.com/...­
>
> depth: 3
> red: Math.sin(Math::PI * Math.cos(Math::PI * y * x))
> green: Math.sin(Math::PI * (Math.sin(Math::PI * y)) ** 3)
> blue: (Math.cos(Math::PI * Math.cos(Math::PI * y))) ** 3
>
> The depth is how many layers of functions to combine. The bottom layer
> is always x or y. Depth 3 is where things begin to get a little bit
> interesting, but 5 and higher is much more exciting.
>
> Performance can be an issue with so many computations per pixel,
> therefore the solution that performs quickest on a 1600x1200 image
> with depth of 7 will be the winner of this quiz!
>
> Good luck!
>
>
>
> --
> Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
> http://ruby.meetu...­
> This message was sent by Mark Wilkins ([address removed]) from Harrisburg Ruby Group.
> To learn more about Mark Wilkins, visit his/her member profile: http://ruby.meetu...­
> To unsubscribe or to update your mailing list settings, click here: http://www.meetup...­
> Meetup Support: [address removed]
> 632 Broadway, New York, NY 10012 USA
>
>

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy