第1-4章まとめ課題

  1. (00-550197C)

    def show_color_picture()
      a=make2d(50,50)
      for y in 0..49
        for x in 0..49
            a[y][x]=[0.5,(1.0-y)**2/49.0**2,(x+y)**2/(49.0*2)**2]
        end
      end
      show(a)
    end
    
  2. (00-640215J)

    授業で作った球を描くプログラムを少し変えて光が斜めから当たっているように見えるようにしました。
    def b(s,x,y)
      if distance(x,y,s/2,s/2) <= s/2
        (2*s/3-distance(x,y,2*s/3,s/3))/(2*s/3)
      else
        1.0
      end
    end
    
    def sphere(s)
      image = make2d(s, s)
      for y in 0..(s -1)
        for x in 0..(s -1)
          image[y][x] = b(s,x,y)
        end
      end
      image
    end
    
  3. (00-460024F)

    ウェーブレット変換による画像への電子署名の埋め込み
    大まかな流れのコードはできましたが、 残念ながら、細かいところがうまくいっていないせいで、 画像に縞模様ができてしまっています。 時間があれば、いつか修正しようと思います。
    # HAARウェーブレット変換によるデータの画像への隠蔽
    # コピープロテクトとして用いることができる。
    # 購入者のIDをwatermarkとして画像に埋め込み
    # 画像が流出した際にその画像からwatermarkを取り出し
    # 流出させた人物を特定することができる。
    # robustなwatermarkにするために、周波数成分に埋め込む。
    # jpeg圧縮、ガウスノイズ、回転、縮小などによりデータを失わない。
    
    # 3level dwt, and hide watermark in HL area.
    # watermark = [1,0,1,0,1,0,1,0,1]
    
    def array
            [[ 161.798, 161.357, 160.617, 160.802, 161.83, 158.157, 158.054, 161.684
    ....
           ]]
    end
    
    x,y = array[0].length,array.length
    new_array = make2d(x,y)
    array.each_with_index do |ar,j|
            ar.each_with_index do |a,k|
                    new_array[j][k]=a/255
            end
    end
    
    def dwt_yoko(array)
            x,y = array[0].length,array.length
            new_array = make2d(x,y)
            (x-1).times do |l|
                    (y-1).times do |m|
                            if l < x/2
                                    new_array[l][m] = (array[2*l][m]+array[2*l+1][m])/2
                            elsif l < x-1
                                    new_array[l][m] = (array[2*(l-x/2)][m]-array[2*(l-x/2)+1][m]+1)/2
                            end
                    end
            end
            return new_array
    end
    
    def dwt_tate(array)
            x,y = array[0].length,array.length
            new_array = make2d(x,y)
            (x-1).times do |l|
                    (y-1).times do |m|
                            if m < y/2
                                    new_array[l][m] = (array[l][2*m]+array[l][2*m+1])/2
                            elsif m < y-1
                                    new_array[l][m] = (array[l][2*(m-y/2)]-array[l][2*(m-y/2)+1]+1)/2
                            end
                    end
            end
            return new_array
    end
    
    def dwt(array)
            return dwt_tate(dwt_yoko(array))
    end
    
    def idwt_yoko(array)
            x,y = array[0].length,array.length
            new_array = make2d(x,y)
            (x/2-1).times do |l|
                    (y-1).times do |m|
                            array[l+x/2][m] = 0 if !array[l+x/2][m]
                            array[l][m] = 0 if !array[l][m]
                            new_array[2*l-1][m] = array[l][m]+array[l+x/2][m]-1/2
                            new_array[2*l][m] = array[l][m]-array[l+x/2][m]+1/2
                    end
            end
            return new_array
    end
    
    def idwt_tate(array)
            x,y = array[0].length,array.length
            new_array = make2d(x,y)
            (x-1).times do |l|
                    (y/2-1).times do |m|
                            array[l][m+y/2] = 0 if !array[l][m+y/2]
                            array[l][m] = 0 if !array[l][m]
                            new_array[l][2*m-1] = array[l][m]+array[l][m+y/2]-1/2
                            new_array[l][2*m] = array[l][m]-array[l][m+y/2]+1/2
                    end
            end
            return new_array
    end
    
    def idwt(array)
            return idwt_yoko(idwt_tate(array))
    end
    
    3.times do |i|
            new_array = dwt(new_array)
    end
    
    data = [1,0,1,0,1,0,1,0,1]
    watermark = make2d(x/8,y/8)
    (x/8).times do |xx|
            (y/8).times do |yy|
                    new_array[(7*x/8)+xx][yy]=data[(xx/(64/3)).floor*3+(yy/(64/3)).floor]
            end
    end
    
    3.times do |i|
            new_array = idwt(new_array)
    end
    show(new_array)
    
  4. (00-640101D)

    3波の干渉/簡易版
    平面上の任意の3点から発せられた、同種の波の干渉を、波元の位相が等しくいずれも変位が0である時刻について表す。 3点から遠い領域や、さまざまな時刻・波長・振幅に対応することができていないが、その分入力すべき変数が少ないことは評価できると思う。 添付した図は interference(60,1,3,5)に対するものである。
    def wave(x,y,u,v)
      (1+sin(distance(x,y,u,v)))*0.5
    end
    
    def interference3_simple(s,a,b,c)
      z = make2d(2*s,2*s)
      for y in 0..2*s-1
        for  x in 0..2*s-1
          z[y][x] =
            [wave(x,y, s*(1+cos(a)) , s*(1+sin(a)) ),
             wave(x,y, s*(1+cos(b)) , s*(1+sin(b)) ),
             wave(x,y, s*(1+cos(c)) , s*(1+sin(c)) )]
        end
      end
      show(z)
    end
    
    3波の干渉/複雑版
    まとめ課題:3波の干渉/複雑版」を改良し、あらゆる状況下での3波の干渉を作図できるようにした。具体的には、3波元の座標とそれぞれから発せられる波の性質、および時刻を自由に設定することができる。ただし、その分変数を入力するのが煩雑になるため、特に目的なく使用する場合、たとえば次の制限を考慮してやるといいだろう。
    ha=hb=hc=1
    frequencya=frequencyb=frequencyc=1
    lengtha=lengthb=lengthc
    
    これにより17あった変数を9にまで減らすことができる。

    なお、添付画像は interference3_complex(200,20,50,0.4,10,10,110,140,0.6,15,24,50,190,1,24,8,18) である。

    def pi
      3.14159265358979323846264  #円周率
    end 
    
    def wave(h,t,x,y,u,v,l,f)
      h*(1+sin(2*pi*(distance(x,y,u,v)/l - t/f)))*0.5
    end
    
    def interference3_complex(s,xa,ya,ha,lengtha,frequencya,xb,yb,hb,lengthb,frequencyb,xc,yc,hc,lengthc,frequencyc,t)
      z = make2d(s,s)
      for y in 0..s-1
        for  x in 0..s-1
          z[y][x] =
            [wave(ha,t,x,y,xa,ya,lengtha,frequencya),
             wave(hb,t,x,y,xb,yb,lengthb,frequencyb),
             wave(hc,t,x,y,xc,yc,lengthc,frequencyc)]
        end
      end
      show(z)
    end
    
  5. (00-560087B)

    グラデーションで円を作っていたことを思い出して作りました。1999年に公布・施行された「国旗及び国歌に関する法律」の規定によれば、旗の形は縦が横の3分の2の長方形。日章の直径は縦の5分の3で中心は旗の中心。色地は白色、日章は紅色とされているとのことです。それに準じた比率で作りました。
    def japan(s)
      t=s*3/2
      image=make2d(s,t)
      for x in 0..(s-1)
        for y in 0..(t-1)
          if distance(x,y,s/2,t/2) <= 3*s/10
            image[x][y]=[1,0,0]
          else
            image[x][y]=[1,1,1]
          end
        end
      end
      image
    end
    
  6. (00-640135I)

    1作目「RGBグラデーション」
    三角関数を使って周期的なグラデーションを描く。乱数によって毎回違ったグラデーションが出力される。各乱数を返り値とすることで、気に入ったグラデーションを出力させるパラメータを知ることができる。 添付画像は一例。
    def random_gradient_rgb(width = 256, height = 256)
        canvas = make2d(height, width)
        rxy = rand()
        rxd = rand() * 2 * PI
        ryd = rand() * 2 * PI
        gxy = rand()
        gxd = rand() * 2 * PI
        gyd = rand() * 2 * PI
        bxy = rand()
        bxd = rand() * 2 * PI
        byd = rand() * 2 * PI
        for y in 0..canvas.length()-1
            for x in 0..canvas[y].length()-1
                r = 0.5 + (rxy * sin(x * 0.05 + rxd) + (1 - rxy) * cos(y * 0.05 + ryd)) / 2.0
                g = 0.5 + (gxy * sin(x * 0.05 + gxd) + (1 - gxy) * cos(y * 0.05 + gyd)) / 2.0
                b = 0.5 + (bxy * sin(x * 0.05 + bxd) + (1 - bxy) * cos(y * 0.05 + byd)) / 2.0
                canvas[y][x] = [r, g, b]
            end
        end
        show(canvas)
        return [rxy, rxd, ryd, gxy, gxd, gyd, bxy, bxd, byd]
    end
    
    2作目「HSVグラデーション」
    三角関数を使って周期的なグラデーションを描く。これをrgb(赤・緑・青)の値を直接操作するのではなくhsv(色相・彩度・明度)の値を三角関数で表したところ、とても不安になる画像が得られた。
    def hsv_to_rgb(h, s, v)
      h *= 360
      c = v * s
      x = c * (1 - ((h / 60.0) % 2 - 1).abs)
      m = v - c
      r, g, b = case
        when h < 60  then [c, x, 0]
        when h < 120 then [x, c, 0]
        when h < 180 then [0, c, x]
        when h < 240 then [0, x, c]
        when h < 300 then [x, 0, c]
        else              [c, 0, x]
      end
      [r, g, b].map{|channel| (channel + m)}
    end
    
    def random_gradient_hsv(width = 256, height = 256)
      canvas = make2d(height, width)
      hxy = rand()
      hxd = rand() * 2 * PI
      hyd = rand() * 2 * PI
      sxy = rand()
      sxd = rand() * 2 * PI
      syd = rand() * 2 * PI
      vxy = rand()
      vxd = rand() * 2 * PI
      vyd = rand() * 2 * PI
      for y in 0..canvas.length()-1
        for x in 0..canvas[y].length()-1
          h = 0.5 + (hxy * sin(x * 0.05 + hxd) + (1 - hxy) * cos(y * 0.05 + hyd)) / 2.0
          s = 0.5 + (sxy * sin(x * 0.05 + sxd) + (1 - sxy) * cos(y * 0.05 + syd)) / 2.0
          v = 0.5 + (vxy * sin(x * 0.05 + vxd) + (1 - vxy) * cos(y * 0.05 + vyd)) / 2.0
          canvas[y][x] = hsv_to_rgb(h, s, v)
        end
      end
      show(canvas)
      return [hxy, hxd, hyd, sxy, sxd, syd, vxy, vxd, vyd]
    end
    3作目「虹
    光の波長からrgbに変換するテーブルを拾ってきたので、それを用いて虹のグラデーションを描いた。両端は紫外線と赤外線のため黒くなっている。 (グラデーションしか作っていない気がする。)
    def rainbow_gradient(width = 256, height = 256)
        table = [[0.08777,0.00000,0.19463],中略,[0.13614,0.00000,0.00000]]
        canvas = make2d(height, width)
        for y in 0..canvas.length()-1
            for x in 0..canvas[y].length()-1
                p = (table.length() - 1) * (x + y) / (width + height)
                r = table[p.floor][0] * (1 - (p - p.floor)) + table[p.ceil][0] * (p - p.floor)
                g = table[p.floor][1] * (1 - (p - p.floor)) + table[p.ceil][1] * (p - p.floor)
                b = table[p.floor][2] * (1 - (p - p.floor)) + table[p.ceil][2] * (p - p.floor)
                canvas[y][x] = [r, g, b]
            end
        end
        show(canvas)
        return true
    end
    
    4作目「花火」
    虹を作るときに使用したテーブルを用いて花火を描いた。大きさと色と光の進む方向がランダムである。過剰なほど細かく点を打っているので時間tの係数をキャッシュして高速化した。 (ランダム要素を入れると完成した後色々なものができて楽しい。)
    def firework(width = 256, height = 256)
      canvas = fill_in(make2d(height, width), [0, 0, 0, 1])
      length = (rand() * [width, height].min / 2).to_i
      number = ((length + [width, height].min / 4) / 2).to_i
      table = [[0.08777,0.00000,0.19463],中略,[0.13614,0.00000,0.00000]]
      p = (table.length() - 1) * rand()
      r = table[p.floor][0] * (1 - (p - p.floor)) + table[p.ceil][0] * (p - p.floor)
      g = table[p.floor][1] * (1 - (p - p.floor)) + table[p.ceil][1] * (p - p.floor)
      b = table[p.floor][2] * (1 - (p - p.floor)) + table[p.ceil][2] * (p - p.floor)
      for i in 0..number
        max = length ** 2
        angle = rand() * 2 * PI
        xa = cos(angle) * length / max
        ya = sin(angle) * length / max
        yb = 0.05 * height * sqrt(height / length) / length ** 4 
        x0 = width / 2
        y0 = height / 2
        for t in 0..max
          x = (x0 + xa * t).to_i
          y = (y0 + ya * t + yb * t ** 2).to_i
          if(y > height - 1)
            next
          end
          a = 1.0 * t / max
          canvas[y][x] = [r, g, b, a]
        end
      end
      show(canvas)
      return [length, number, [r, g, b]]
    end
    
    5作目「パワーアップ花火」
    花火の光の筋を考えると先端が明るく中心が暗いような気がするので、これを手っ取り早くピクセルの透明度で表現してみたら案外うまくいった。欲を言えばもう少し色にコントラストが大きい方が花火らしさが出た。
    def firework_gradient(width = 256, height = 256)
      image = fill_in(make2d(height, width), 0)
      for k in 0..7
        canvas = fill_in(make2d(height, width), [0, 0, 0, 0])
        length = (rand() * [width, height].min / 2).to_i
        number = ((length + [width, height].min / 4) / 2).to_i
        table = 略
        p = (table.length() - 1) * rand()
        r = table[p.floor][0] * (1 - (p - p.floor)) + table[p.ceil][0] * (p - p.floor)
        g = 略
        b = 略
        c = hsv_to_rgb(rgb_to_hsv(r, g, b)[0], rgb_to_hsv(r, g, b)[1], 1)
        r = c[0]
        g = c[1]
        b = c[2]
        for i in 0..number
          max = length ** 2
          angle = rand() * 2 * PI
          xa = cos(angle) * length / max
          ya = sin(angle) * length / max
          yb = 0.05 * height * sqrt(height / length) / length ** 4 
          x0 = width / 2
          y0 = height / 2
          for t in 0..max
            x = (x0 + xa * t).to_i
            y = (y0 + ya * t + yb * t ** 2).to_i
            if(y > height - 1)
              next
            end
            a = 1.0 * t / max
            canvas[y][x] = [r, g, b, a]
          end
        end
        image = alpha_blend(canvas, image)
      end
      show(image)
      return true
    end
    
    6作目「空に架かる虹」
    虹を作ったので曲げてみた。元のテーブルで両端が黒になっているので、黒を透明にしたいと思ったが難しかった。明度を不透明度と見なし、背景画像とアルファブレンドするという方法をとった。
    def rainbow_in_the_sky(width = 256, height = 256)
      table = [[0,0,0],[0.03215,0.00000,0.06704],中略,[0.07337,0.00000,0.00000],[0,0,0]]
      canvas = fill_in(make2d(height, width), [0, 0, 0, 0])
      for y in 0..canvas.length()-1
        for x in 0..canvas[y].length()-1
          r = sqrt((x - width / 2.0) ** 2 + (y - height) ** 2)
          p = 2.0 * r / height - 1.0
          if(p < 0 || p > 1)
            next
          end
          p = p * (table.length() - 1)
          r = table[p.floor][0] * (1 - (p - p.floor)) + table[p.ceil][0] * (p - p.floor)
          g = table[p.floor][1] * (1 - (p - p.floor)) + table[p.ceil][1] * (p - p.floor)
          b = table[p.floor][2] * (1 - (p - p.floor)) + table[p.ceil][2] * (p - p.floor)
          a = rgb_to_hsv(r, g, b)[2]
          if(a < 0.6)
            a = a ** 2 / 0.6
          end
          if(a < 0.4)
            a = a ** 2 / 0.4
          end
          if(a < 0.2)
            a = a ** 2 / 0.2
          end
          canvas[y][x] = [r, g, b, a]
        end
      end
      show(alpha_blend(canvas, fill_in(make2d(height, width), [0.62, 0.84, 0.93, 1])))
      return true
    end
    
  7. (00-640219B)

    青い三日月です
    def b(s,x,y)
      if distance(x,y,s/2,s/2) <= s/2
        (2*s/2-distance(x,y,2*s/2,s/2))/(2*s/2)
      else
        1.0
      end
    end
    
    def sphere(s)
      image = make2d(s, s)
      for y in 0..(s -1)
        for x in 0..(s -1)
          if distance(x,y,s/4,s/4) <= s/2
            image[y][x] = [1,1,1]
          else
            image[y][x] = [b(s,x,y), 1, 1]
          end
        end
      end
      image
    end
    
  8. (00-640097H)

    自分には絵と数学のセンスがないのかsinやcosを使ってみてもめまいを催すような画像しか出力できなかったので、しょうがなく円を使ってサバンナっぽいような国旗っぽいような画像を作ってみました。
    def show_color_picture()
      a=make2d(200,200)
      for y in 0..199
        for x in 0..199
          if (x-99)**2+(y-99)**2>2500.0
            r=1.0
          else
            r=0.0
          end
          a[y][x]=[r,(200.0-y)/200.0,0.0]
        end 
      end
      show(a)
    end
    
  9. (00-640097H)

    投票が済んだので画像は非公開とします.

    自分が過去に作ったものの流用ですが、提出しないのももったいないと思ったので提出しておきます。
    r=[1,0,0]
    s=[1, 0.7, 0.4]
    b=[0.5, 0.4, 0]
    w=[1,1,1]
    
    mario=[[w,w,w,w,w,w,w,w,w,w,w,w,w,w],
    ...,
    [w,w,w,w,w,w,w,w,w,w,w,w,w,w]]
    
    show(mario)
    
    # 参考画像:http://ks.c.yimg.jp/res/chie-que-13110/13/110/798/618/i320
    
  10. (00-640134F)

    ドンキとかにこんな服売ってそうだなと感じました。ソースコードが比較的長いので添付ファイルで貼り付けます。 draw(1辺の長さ(4の倍数))で配列を確保して使います。
    #r=600
    
    def magatama(r)
      rr=r*0.4
      rrr=r*0.43
      sr=r*0.07
      array=make2d(r,r)
      for i in 0..(r-1)
        for j in 0..(r-1)
          d=distance(i,j,(r-1)/2.0,(r-1)/2.0)
          if d>rrr
            array[i][j]=0
          elsif rr<=d && d <= rrr
            array[i][j]=1
          else
            if i<=(r-1)/2.0
              if distance(i,j,0.3*r,0.5*r)<=sr
                array[i][j]=1
              else
                if j>=(0.5*r)&&(rr/2.0)<=distance(i,j,0.3*r,0.5*r)
                  array[i][j]=1
                else
                  array[i][j]=0
                end
              end
            else
              array[i][j]=(1-array[r-1-i][r-1-j])
            end
          end
        end
      end
        array
    end
    
    def ura(r)
      array=magatama(r)
      for i in 0..(r-1)
        for j in 0..(r-1)
          array[i][j]=1-array[i][j]
        end
      end
      array
    end
    
    def draw_bool(r)
      array=make2d(r,r)
      u=r/4
      sub_array=magatama(u)
      for i in 0..3
        for j in 0..3
          for k in 0..(u-1)
            for m in 0..(u-1)
              array[i*u+k][j*u+m]=sub_array[k][m]
            end
          end
        end
      end
      array
    end
    
    def coloring(r)
      t=10.0
      array=make2d(r,r)
      for i in 0..(r-1)
        for j in 0..(r-1)
          d1=distance(i,j,0,0)
          d2=distance(i,j,r-1,r-1)
          d3=distance(i,j,r-1,0)
          d4=distance(i,j,0,r-1)
          array[i][j]=[abs((sin(d1/t)+sin(d2/t))/2.0),abs((sin(d1/t)+sin(d3/t))/2.0),abs((sin(d1/t)+sin(d4/t))/2.0)]
        end
      end
      array
    end
    
    def draw(r)
      color=coloring(r)
      draw=draw_bool(r)
      for i in 0..(r-1)
        for j in 0..(r-1)
          if draw[i][j]==0
            color[i][j]=[color[i][j][0]/8.0,color[i][j][1]/8.0,color[i][j][2]/8.0]
          end
        end
       end
       color
    end
    
  11. (00-510334B)

    用意されていたグラデーションのプログラムを自分なりに改造してみました。
    def scp(k)
      a=make2d(k,k)
      for y in 0..k-1
        for x in 0..k-1
          a[y][x]=
           [x.to_f/k.to_f+(k.to_f-2.0*distance(x,y,k*0.25,k*0.75))/k.to_f+rand()*0.08-rand()*0.08,
            1.0-y.to_f/k.to_f+(k.to_f-2.0*distance(x,y,k*0.25,k*0.75))/k.to_f+rand()*0.08-rand()*0.08,
            1.0+(k.to_f-2.0*distance(x,y,k*0.25,k*0.75))/k.to_f+rand()*0.08-rand()*0.08]
        end 
      end
      show(a)
    end
    
  12. (00-660065E)

     ライフゲームというシミュレーションをつくってみました。緑の点は生命で、周りの環境で生まれたり死んだりします。詳しいことはググってください。  初期配置は各位置1/2でランダムに決まります。関数lifegame(n,t)では、nが全体のサイズ(n>3),tが実行時間で、毎秒2回の処理をします。プログラムの終了はCtrl+C、画像を閉じるにはターミナルを閉じると早いです。画像はn=50です。
    def color(b)
      if b == 1
        [0,1,0]
      else
        [0,0,0]
      end
    end
    
    # 生きているセルは緑、死んでいるセルは黒で表示する
    def check(d)
      if d == [0,1,0]
        1
      else
        0
      end
    end
    
    # セルの色から生=1、死=0に戻す
    def lifegame(n,t)
      a= make2d(n, n)
      for y in 0..n-1
        for x in 0..n-1
           a[y][x]= color(0)
        end
      end
      for y in 1..n-2
        for x in 1..n-2
          a[y][x] = color((2*rand()).to_i)
        end
      end
    # 初期配置
      while t > 0
        show(a)
        t = t - 0.5
        c= make2d(n, n)
        for y in 0..n-1
          for x in 0..n-1
             if (x == 0||x==(n-1))||(y==0||y==(n-1))
               c[x][y] = color(0)
             else
               e = check(a[x-1][y-1])+check(a[x-1][y])+check(a[x-1][y+1])+check(a[x][y-1])+check(a[x][y+1])+check(a[x+1][y-1])+check(a[x+1][y])+check(a[x+1][y+1])
               if check(a[x][y]) == 1
                 if e == 2 || e == 3
                   c[x][y] = color(1)
                 else
                   c[x][y] = color(0)
                 end
               else
                 if e == 3
                   c[x][y] = color(1)
                 else
                   c[x][y] = color(0)
                 end
               end
             end
          end
        end
      a = c
      sleep(0.5)
      end
    # t繰り返す 
    end
    
  13. (00-630284A)

    1d6(6面サイコロ1つ)を振ることができるプログラムです。 まず背景を出力し、randによってダイス目を決定、 その目に応じて白円が背景の上に表示されるようにしました。 画像自体はつまらないですし、 プログラムも添付にするほど無駄に長いですが、 ロードして実行してみると楽しくなれます。私は楽しい。
    image = make2d(165,165)
    for y in 0..164
      for x in 0..164
        image[y][x]=[0.5,1,1]
      end
    end
    if rand(1..6) == 1
      print(1)
      for y in 0..164
        for x in 0..164
          image[y][x]=[1,0,0]
        end
      end
      for y in 64..99
        for x in 64..99
          if distance(x,y,82.5,82.5)<=17.5
            image[y][x]=[1,1,1]
          else
            image[y][x]=[1,0,0]
          end
        end
      end
    elsif rand(2..6) == 2
      print(2)
      for y in 30..64
        for x in 30..64
          if distance(x,y,47.5,47.5)<=17.5
            image[y][x]=[1,1,1]
          else
            image[y][x]=[0.5,1,1]
          end
        end
      end
      for y in 100..134
        for x in 100..134
          if distance(x,y,117.5,117.5)<=17.5
            image[y][x]=[1,1,1]
          else
            image[y][x]=[0.5,1,1]
          end
        end
      end
    elsif rand(3..6) == 3
      print(3)
      for y in 30..64
        for x in 30..64
          if distance(x,y,47.5,47.5)<=17.5
            image[y][x]=[1,1,1]
          else
            image[y][x]=[0.5,1,1]
          end
        end
      end
      for y in 65..99
        for x in 65..99
          if distance(x,y,82.5,82.5)<=17.5
            image[y][x]=[1,1,1]
          else
            image[y][x]=[0.5,1,1]
          end
        end
      end
      for y in 100..134
        for x in 100..134
          if distance(x,y,117.5,117.5)<=17.5
            image[y][x]=[1,1,1]
          else
            image[y][x]=[0.5,1,1]
          end
        end
      end
    elsif rand(4..6) == 4
      print(4)
      for y in 30..64
        for x in 30..64
          if distance(x,y,47.5,47.5)<=17.5
            image[y][x]=[1,1,1]
          else
            image[y][x]=[0.5,1,1]
          end
        end
      end
      for y in 100..134
        for x in 30..64
          if distance(x,y,47.5,117.5)<=17.5
            image[y][x]=[1,1,1]
          else
            image[y][x]=[0.5,1,1]
          end
        end
      end
      for y in 30..64
        for x in 100..134
          if distance(x,y,117.5,47.5)<=17.5
            image[y][x]=[1,1,1]
          else
            image[y][x]=[0.5,1,1]
          end
        end
      end
      for y in 100..134
        for x in 100..134
          if distance(x,y,117.5,117.5)<=17.5
            image[y][x]=[1,1,1]
          else
            image[y][x]=[0.5,1,1]
          end
        end
      end
    elsif rand(5..6) == 5
      print(5)
      for y in 30..64
        for x in 30..64
          if distance(x,y,47.5,47.5)<=17.5
            image[y][x]=[1,1,1]
          else
            image[y][x]=[0.5,1,1]
          end
        end
      end
      for y in 65..99
        for x in 65..99
          if distance(x,y,82.5,82.5)<=17.5
            image[y][x]=[1,1,1]
          else
            image[y][x]=[0.5,1,1]
          end
        end
      end
      for y in 100..134
        for x in 100..134
          if distance(x,y,117.5,117.5)<=17.5
            image[y][x]=[1,1,1]
          else
            image[y][x]=[0.5,1,1]
          end
        end
      end
      for y in 100..134
        for x in 30..64
          if distance(x,y,47.5,117.5)<=17.5
            image[y][x]=[1,1,1]
          else
            image[y][x]=[0.5,1,1]
          end
        end
      end
      for y in 30..64
        for x in 100..134
          if distance(x,y,117.5,47.5)<=17.5
            image[y][x]=[1,1,1]
          else
            image[y][x]=[0.5,1,1]
          end
        end
      end
    elsif rand(6..6) == 6
      print(6)
      for y in 25..59
        for x in 30..64
          if distance(x,y,47.5,42.5)<=17.5
            image[y][x]=[1,1,1]
          else
            image[y][x]=[0.5,1,1]
          end
        end
      end
      for y in 65..99
        for x in 30..64
          if distance(x,y,47.5,82.5)<=17.5
            image[y][x]=[1,1,1]
          else
            image[y][x]=[0.5,1,1]
          end
        end
      end
      for y in 105..139
        for x in 30..64
          if distance(x,y,47.5,122.5)<=17.5
            image[y][x]=[1,1,1]
          else
            image[y][x]=[0.5,1,1]
          end
        end
      end
      for y in 25..59
        for x in 100..134
          if distance(x,y,117.5,42.5)<=17.5
            image[y][x]=[1,1,1]
          else
            image[y][x]=[0.5,1,1]
          end
        end
      end
      for y in 65..99
        for x in 100..134
          if distance(x,y,117.5,82.5)<=17.5
            image[y][x]=[1,1,1]
          else
            image[y][x]=[0.5,1,1]
          end
        end
      end
      for y in 105..139
        for x in 100..134
          if distance(x,y,117.5,122.5)<=17.5
            image[y][x]=[1,1,1]
          else
            image[y][x]=[0.5,1,1]
          end
        end
      end
    end
    show(image)
    
  14. (00-630297A)

    投票が済んだので画像は非公開とします.

    北斗の拳修羅の国篇パチスロ化おめでとう!
    1982年より週刊少年ジャンプに連載された不朽の名作『北斗の拳』。その第2部(TVアニメで言うところの『北斗の拳2』)より、とくに修羅の国篇をモチーフにしたパチスロの新台がこのたびSammyよりリリースされました。長らくマイナー扱いされてきた2部が脚光をあびることとなったのは大変喜ばしいことであります。おめでとうございます。その祝福の意を込めてメインキャラ3人のバストアップのドット絵を作ってみました。主人公であるにもかかわらずケンシロウが端っこなのは公式のリーフレットに準拠したものです。

    プログラムは長いので添付しました。ソース自体は何の変哲も面白みもない、単なる力技の産物です。

    def hkt
    #make base
            hkt = make2d(162,144)
    #color_definition
    #                #background
                     b1 = [0,150/255.0,230/255.0]
                     b2 = [0,160/255.0,70/255.0]
                     b3 = [80/255.0,0,40/255.0]
    #                #common
                     s1 = [255/255.0,226/255.0,177/255.0] #skin
                     s2 = [235/255.0,170/255.0,85/255.0]
                     s3 = [210/255.0,140/255.0,45/255.0]
                     e1 = [145/255.0,88/255.0,33/255.0] #eye
                     e2 = [200/255.0,185/255.0,158/255.0]
                      w = [1,1,1] #white
                      b = [0,0,0] #black
    #                #kenshiro
                    kh1 = [50/255.0,40/255.0,20/255.0] #hair
                    kh2 = [6/255.0,5/255.0,4/255.0]
                    kh3 = [110/255.0,90/255.0,50/255.0]
                    ke1 = [0,132/255.0,147/255.0] #eye:ke1 is the same color as shachi's listband
                    ke2 = [0,90/255.0,105/255.0]
                    ke3 = [0,30/255.0,50/255.0]
                    kc1 = [0,0,130/255.0] #clothes
                    kc2 = [0,84/255.0,180/255.0]
                    kc3 = [0,0,42/255.0]
    #                #shachi
                    sh1 = [1,1,212/255.0] #hair
                    sh2 = [240/255.0,230/255.0,160/255.0]
                    sh3 = [210/255.0,200/255.0,110/255.0]
                    sh4 = [180/255.0,165/255.0,80/255.0]
                    sh5 = [150/255.0,130/255.0,50/255.0]
                    se1 = [0,185/255.0,61/255.0] #eye
                    se2 = [0,150/255.0,49/255.0]
                    se3 = [0,80/255.0,25/255.0]
                    sl1 = [0,176/255.0,191/255.0] #listband
                    sl2 = [84/255.0,231/255.0,248/255.0]
                    #notice:sl3 is the same color as kenshiro's eye(ke1)
    #                #kaioh
                    kp1 = [192/255.0,0,1] #purple part
                    kp2 = [88/255.0,0,115/255.0]
                    ky1 = [1,243/255.0,54/255.0] #yellow part
                    ky2 = [1,168/255.0,42/255.0]
                    kr1 = [1,37/255.0,37/255.0] #red part
                    kr2 = [1,177/255.0,177/255.0]
                    kb1 = [80/255.0,80/255.0,80/255.0] #black part
                    kb2 = [16/255.0,16/255.0,16/255.0]
    
    
    #show picture
    #chikarawaza nimo hodo ga aru
    hkt = 
    [[b1,b1,b1,...,kp2],
    ...
    [b,b,b,...b]]
    
    
    end
    
    show (hkt)
    
  15. (00-640153E)

    光の三原色による加法混色の図です。円が重なる場所で混色される様子を思い通りに表現できました。添付画像はs=300としたときのものです。
    def r(s, x, y)
            if distance(x, y, s/2.0, s/3.0) <= (0.9/3)*s
                    1
            else
                    0
      end
    end
    
    def g(s, x, y)
      if distance(x, y, s/3.0, 2*s/3) <= (0.9/3)*s
              1
            else
              0
      end
    end
    
    def b(s, x, y)
      if distance(x, y, 2*s/3.0, 2*s/3.0) <= (0.9/3)*s
              1
            else
              0
      end
    end
    
    def color1(s, x, y)
    [r(s, x, y), g(s, x, y), b(s, x, y)]
    end
    
    def  rgb(s)
      image1 = make2d(s, s)
            for y in 0..(s-1.0)
              for x in 0..(s-1.0)
                     image1[y][x] = color1(s, x, y)
                    end
            end
            image1
    end
    
    加法混色に引き続き、色の三原色による減法混色の図です。加法混色の図のコードを少し書き換えるだけで作ることができましたが、減法混色の名の通り、引き算(-1の足し算)を意識したものとなりました。添付画像はs=300としたときのものです。
    def inb(s, x, y)
            if distance(x, y, s/2.0, s/3.0) <= (0.9/3)*s
                    -1
            else
                    0
      end
    end
    
    def ing(s, x, y)
      if distance(x, y, s/3.0, 2*s/3) <= (0.9/3)*s
              -1
            else
              0
      end
    end
    
    def inr(s, x, y)
      if distance(x, y, 2*s/3.0, 2*s/3.0) <= (0.9/3)*s
              -1
            else
              0
      end
    end
    
    def color2(s, x, y)
    [1 + inr(s, x, y), 1 + ing(s, x, y), 1 + inb(s, x, y)]
    end
    
    def  ymc(s)
      image2 = make2d(s, s)
            for y in 0..(s-1.0)
              for x in 0..(s-1.0)
                     image2[y][x] = color2(s, x, y)
                    end
            end
            image2
    end
    
  16. (00-640240H)

    波動関数 制作日に構造化学の講義を受けたので波動関数を表してみたくなった。 早速実装。角度依存性のないS軌道が簡単そう。

    これで完成。関数show()で表示。 ただし、3つの引数は順に表示範囲の大きさ(a単位)、ボーア半径(pixel),補正値(正数)である(添付図は20,10,3)。この図は水素原子の3s軌道に対応した波動関数の二乗を濃淡で表し、正が赤、負が青。濃い場所ほど電子が存在する確率が高いらしい。なお、波動関数は構造化学の教科書より引用し、そのままだと色が薄いから対数関数で補正してある。bはその程度を指定する。

    def Image (s,a,b)
      l=a*s*2
      a = a.to_f
      max = 0.0
      image = Array.new(l)
      for i in 0..(l-1)
        image[i] = Array.new(l)
      end
      for i in 0..(l-1)
        for j in 0..(l-1)
          r = distance(i,j,l/2,l/2).to_f
          v = (27-18*r/a+2*(r**2)/(a**2))*exp(-r/3/a)
          image[i][j] = v
          if v.abs > max
            max = v.abs
          end
        end
      end
      for i in 0..(l-1)
        for j in 0..(l-1)
          v = image[i][j]
          if b == 0
            vv = (v.abs/max)**2
          else
            vv = log((v.abs/max)**2*(E**b-1)+1)/b
          end
          if v >= 0.0
            image[i][j] = [1,1-vv,1-vv]
          else
            image[i][j] = [1-vv,1-vv,1]
          end
        end
      end
      return image
    end
    
  17. (00-640231J)

    三角関数を用いて四つの波源からの波の干渉を表そうとしたところ、思ったより面白くなかったので、積をとったら、気持ち悪くなりました。
    三角関数を使った際、値が負になることがあるので、absを用いて負にならないようにしました。
    もうすこし波の間隔がひろくなるイメージでしたが、とても狭くなり、気持ち悪い画像になりましたが、錯視っぽい部分もあり面白かったのでそのまま提出しました。
    def color(l,x,y)
      s=(sin((distance(l/3.0,l/3.0,x,y)+distance(l/3.0,2*l/3.0,x,y)+distance(2*l/3.0,l/3.0,x,y)+distance(2*l/3.0,2*l/3.0,x,y)))).abs
      t=(cos((distance(l/3.0,l/3.0,x,y)+distance(l/3.0,2*l/3.0,x,y)+distance(2*l/3.0,l/3.0,x,y)+distance(2*l/3.0,2*l/3.0,x,y)))).abs
      [s*s*t*t,s*s*t,s*t*t]
    end
    
    def picture(l)
      a=make2d(l,l)
      for x in 0..(l-1)
        for y in 0..(l-1)
          a[x][y]=color(l,x,y)
        end
      end
      show(a)
    end
    
  18. (00-660061C)

    このプログラムに3枚の画像を入力すると2枚の砂嵐の画像を出力します。 与えられた最初の2枚の画像は砂嵐の画像に変換され、この2枚の画像が重なるともう1枚の画像が現れる仕組みです。

    今回は正方形と円の画像を重ねると、正三角形が現れるプログラムを書いてみました。 画像を見ただけだと分からないかもしれませんが、透明なシートに1枚目と2枚目の画像を印刷して重ねると、本当に正方形と円が消えて正三角形が浮かび上がります。

    def square1(n) # 黒の正方形を生成する関数。
     a=make2d(10*n,10*n)
     for i in 3*n .. 7*n
       for j in 3*n .. 7*n
         a[i][j] = 1
       end
     end
     for i in 0 .. 10*n-1
       for j in 0 .. 10*n-1
         a[i][j] = 1-a[i][j]
       end
     end
     return a
    end
    
    #########################################################
    
    def round(n)  # 黒の円を生成する関数。
      a = make2d(10*n,10*n)
      s = 5*n
      t = 5*n
      for i in 0..10*n-1
        for j in 0..10*n-1
          if sqrt((i-s)**2+(j-t)**2) >=3*n
            a[i][j]=1
          end
        end
      end
      return a
    end
    
    def region(x0,y0,x1,y1,x2,y2,picture)
      if (x1-x2)*(y0-y2)>=(x0-x2)*(y1-y2)
        for i in 0..picture.length()-1
          for j in 0..picture[0].length()-1
            if (x1-x2)*(i-y2)<(j-x2)*(y1-y2)
              picture[i][j]= picture[i][j]+1
            end
          end
        end
      else
        for i in 0..picture.length()-1
          for j in 0..picture[0].length()-1
            if (x1-x2)*(i-y2)>=(j-x2)*(y1-y2)
              picture[i][j]= picture[i][j]+1
            end
          end
        end
      end
    end  
    
    #########################################################
    
    def triangle(n) # 黒の三角形を生成する関数。絵を出力するたびに向きが変化する。
      a = make2d(10*n,10*n)
      b = PI*rand(0.0..2.0)
      x0 = 5*n
      y0 = 5*n
      x1 = x0 + 3*n*cos(b)
      x2 = x0 + 3*n*cos(2*PI/3.0+b)
      x3 = x0 + 3*n*cos(4*PI/3.0+b)
      y1 = y0 + 3*n*sin(b)
      y2 = y0 + 3*n*sin(2*PI/3.0+b)
      y3 = y0 + 3*n*sin(4*PI/3.0+b)
      region(x0,y0,x1,y1,x2,y2,a)
      region(x0,y0,x1,y1,x3,y3,a)
      region(x0,y0,x2,y2,x3,y3,a)
      for i in 0..10*n-1
        for j in 0..10*n-1
          if a[i][j]>=1
            a[i][j]=1
          end
        end
      end
      return a
    end
    
    #########################################################
    
    def black_white(i,j,picture,both) # 2×2のマスのうち、both=0のときは白いマスの一つをランダムに黒くするプログラム。both=1のときはさらに黒いマスの一つをランダムに白くする。
      if picture[2*i+1][2*j]==1 && picture[2*i+1][2*j+1]==1  # p0で番号1のマスに対応
        a = rand(0..1)
        b = rand(0..1)
        picture[2*i+1][2*j]        = a 
        picture[2*i+1][2*j+1]        = 1-a
        picture[2*i][2*j]        = both * b 
        picture[2*i][2*j+1]        = both *(1-b)
      else
        if picture[2*i][2*j]==1 && picture[2*i][2*j+1]==1  # p0で番号2のマスに対応
          a = rand(0..1)
          b = rand(0..1)
          picture[2*i][2*j]        = a 
          picture[2*i][2*j+1]        = 1-a
          picture[2*i+1][2*j]        = both * b 
          picture[2*i+1][2*j+1]        = both *(1-b)
        else
          if picture[2*i][2*j+1]==1 && picture[2*i+1][2*j+1]==1  # p0で番号3のマスに対応
            a = rand(0..1)
            b = rand(0..1)
            picture[2*i][2*j+1]        = a 
            picture[2*i+1][2*j+1]        = 1-a
            picture[2*i+1][2*j]        = both * b 
            picture[2*i][2*j]        = both *(1-b)
          else
            if picture[2*i][2*j]==1 && picture[2*i+1][2*j]==1  # p0で番号4のマスに対応
              a = rand(0..1)
              b = rand(0..1)
              picture[2*i][2*j]        = a 
              picture[2*i+1][2*j]        = 1-a
              picture[2*i][2*j+1]        = both * b 
              picture[2*i+1][2*j+1]        = both *(1-b)
            else
              if picture[2*i][2*j+1]==1 && picture[2*i+1][2*j]==1  # p0で番号5のマスに対応
                a = rand(0..1)
                b = rand(0..1)
                picture[2*i][2*j+1]        = a 
                picture[2*i+1][2*j]        = 1-a
                picture[2*i][2*j]        = both * b 
                picture[2*i+1][2*j+1]        = both *(1-b)
              else
                if picture[2*i][2*j]==1 && picture[2*i+1][2*j+1]==1  # p0で番号6のマスに対応
                  a = rand(0..1)
                  b = rand(0..1)
                  picture[2*i][2*j]        = a 
                  picture[2*i+1][2*j+1]        = 1-a
                  picture[2*i+1][2*j]        = both * b 
                  picture[2*i][2*j+1]        = both *(1-b)
                end
              end
            end
          end
        end
      end
    end                
    
    #########################################################
    
    def imagecode(n) # 絵を暗号化するプログラム。nの10倍の画素数で出力される。
      or1 = square1(n)  # ここに重ねる前の1枚目の画像の配列を貼り付ける
      or2 = round(n)    # ここに重ねる前の2枚目の画像の配列を貼り付ける
      or3 = triangle(n) # ここに重ねた後に現れる画像の配列を貼り付ける
      h = or1.length()    # or1の縦の長さ
      w = or1[0].length() # or1の横の長さ
      or0 = make2d(h,w)  # 砂嵐の元の配列を作る。1〜6の数字を代入。
      for i in 0..h-1
        for j in 0..w-1
          or0[i][j] = rand(1..6)
        end
      end        
      p0 = make2d(h*2,w*2) # or0を利用して砂嵐の画像を作る。
      p1 = make2d(h*2,w*2) # or1に対応する画像を生成
      p2 = make2d(h*2,w*2) # or2に対応する画像を生成
      p3 = make2d(h*2,w*2) # or3に対応する画像を生成
    
    # 目的の画像p0,p1,p2,p3は元の画像or0,or1,or2,or3の縦横2倍の長さである。以下、or0の[i,j]成分がp0の4つの成分[2*i][2*j],[2*i][2*j+1],[2*i+1][2*j],[2*i+1][2*j+1]に対応する。
      for i in 0..h-1  # or0の配列に対応する砂嵐をp0に入力する。6種類の数字に2×2のすべてのパターンを対応させた。
        for j in 0..w-1
          c = or0[i][j]
          if c==2 || c==4 || c==6
            p0[2*i][2*j]=1
          end
          if c==2 || c==3 || c==5
            p0[2*i][2*j+1]=1
          end
          if c==1 || c==4 || c==5
            p0[2*i+1][2*j]=1
          end
          if c==1 || c==3 || c==6
            p0[2*i+1][2*j+1]=1
          end
        end
      end
    # p1にp0と同じ配列を代入。
      for i in 0..2*h-1  
        for j in 0..2*w-1
          p1[i][j]=p0[i][j]
        end
      end
      for i in 0..h-1
        for j in 0..w-1
          if or1[i][j]==0                # or1の黒いピクセルについて、p1の対応するマスに関数black_whiteを適用
            black_white(i,j,p1,0)
          end
        end
      end
      for i in 0..h-1
        for j in 0..w-1
          if or2[i][j]==0 && or3[i][j]==0  # or2,or3の対応するピクセルが共に黒
            
            p2[2*i][2*j]        = 1 - p0[2*i][2*j]                        # p0の白黒を反転させてp2に代入
            p2[2*i+1][2*j]        = 1 - p0[2*i+1][2*j]
            p2[2*i][2*j+1]        = 1 - p0[2*i][2*j+1]
            p2[2*i+1][2*j+1]= 1 - p0[2*i+1][2*j+1]        
            black_white(i,j,p2,0)                                        # 1つだけ黒くする
    
          else
            if or2[i][j]==1 && or3[i][j]==0  # or2の対応するピクセルが白,or3の対応するピクセルが黒
    
              p2[2*i][2*j]        = 1 - p0[2*i][2*j]                        # p0の白黒を反転させてp2に代入
              p2[2*i+1][2*j]        = 1 - p0[2*i+1][2*j]
              p2[2*i][2*j+1]        = 1 - p0[2*i][2*j+1]
              p2[2*i+1][2*j+1]= 1 - p0[2*i+1][2*j+1]        
    
            else
              if or1[i][j]==1 && or2[i][j]==0 && or3[i][j]==1  # or1の対応するピクセルが白、or2の対応するピクセルが黒、or3の対応するピクセルが白
    
                p2[2*i][2*j]        = p0[2*i][2*j]                        # p0をそのままp2に代入
                p2[2*i+1][2*j]        = p0[2*i+1][2*j]
                p2[2*i][2*j+1]        = p0[2*i][2*j+1]
                p2[2*i+1][2*j+1]= p0[2*i+1][2*j+1]        
                black_white(i,j,p2,0)                                # 1つだけ黒くする
              else
                if or1[i][j]==0 && or2[i][j]==0 && or3[i][j]==1  # or1の対応するピクセルが黒、or2の対応するピクセルが黒、or3の対応するピクセルが白        
                  p2[2*i][2*j]        = p1[2*i][2*j]                        # p1をそのままp2に代入
                  p2[2*i+1][2*j]        = p1[2*i+1][2*j]
                  p2[2*i][2*j+1]        = p1[2*i][2*j+1]
                  p2[2*i+1][2*j+1]= p1[2*i+1][2*j+1]        
                else
                  if or1[i][j]==1 && or2[i][j]==1 && or3[i][j]==1  # or1,or2,or3の対応するピクセルが全て白
                    p2[2*i][2*j]        = p0[2*i][2*j]                        # p0をそのままp2に代入
                    p2[2*i+1][2*j]        = p0[2*i+1][2*j]
                    p2[2*i][2*j+1]        = p0[2*i][2*j+1]
                    p2[2*i+1][2*j+1]= p0[2*i+1][2*j+1]                
                    black_white(i,j,p2,1)                                # 1つだけ黒く、1つだけ白くする
                  else
                    if or1[i][j]==0 && or2[i][j]==1 && or3[i][j]==1  # or1の対応するピクセルが黒、or2の対応するピクセルが白、or3の対応するピクセルが白
                      p2[2*i][2*j]        = p0[2*i][2*j]                        # p0をそのままp2に代入
                     p2[2*i+1][2*j]        = p0[2*i+1][2*j]
                     p2[2*i][2*j+1]        = p0[2*i][2*j+1]
                     p2[2*i+1][2*j+1]= p0[2*i+1][2*j+1]        
                    end
                  end
                end
              end
            end
          end
        end
      end
      for i in 0..2*h-1
        for j in 0..2*w-1
          if p1[i][j]==1 && p2[i][j]==1        
            p3[i][j]=1
          else
            p3[i][j]=0
          end
        end
      end
      show(p1)
      show(p2)
      show(p3)
    end
    
    (田中のコメント) 駒場の山口泰先生の研究室でもこのような研究をしています. http://www.graco.c.u-tokyo.ac.jp/yama-lab/index.php#prettyPhoto の「Visual cryptography」に例がありますね.
  19. (00-510139A)

    領域だけ使えば簡単そうだなと思って作りました。直線と円だけだし。 一応野球場のつもりです。ホームベース以外は面倒だったので省略しました。 いい感じの色にするのに意外と苦労しました。
    def l(x,y)
      if y >= (-x) + 75 || y >= x + 25
       if y <= (-x) + 75.5 && y <= x + 25.5
         [1,1,1]
       else
         [0.8,0.4,0]
       end
      else
       r = sqrt((x-25)**2 + (y-50)**2)
       if r <= sqrt( 2 * 25**2)
         if y >= 44 && x >= 22 && x <= 28
           [1,1,1]
         else
           [0.3,0.1,0]
         end
       else
         if r >= 50
           [0,0,0.5]
         else
           [0,0.7,0]
         end
       end
      end
    end
    
    def ground()
      image = make2d(50,50)
       for y in 0..49
        for x in 0..49 
         image[y][x] = l(x,y)
        end
       end
      image
    end
    
  20. (00-640230G)

    対称的な絵を作りたかったので、割り算のあまりで分類して、適当に配色した。
    def show_color_picture()
      a=make2d(100,100)
      for x in 0..99
        for y in 0..99
          if distance(x,y,50,50)%2==0
            a[y][x]=[0,1.0,0]
          else
            if (distance(x,y,50,50)%2!=0) && (distance(x,y,50,50)%3==0)
              a[y][x]=[0,0,1.0]
            else
              if (distance(x,y,50,50)%2!=0) && (distance(x,y,50,50)%3!=0) && (distance(x,y,50,50)%5==0)
                a[y][x]=[0,0,0]
              else 
                if  (distance(x,y,50,50)%2!=0) && (distance(x,y,50,50)%3!=0) && (distance(x,y,50,50)%5!=0) && (distance(x,y,50,50)%7==0) 
                  a[y][x]=[1.0,0,0]
                else
                  a[y][x]=[0.5,0.5,0.5]
                end
              end
            end
          end
        end
      end
      show(a)
    end
    
  21. (00-640125H)

    水面波のイメージと、それに手を加えた、顔を近づけたり遠ざけると動いて見える不思議な画像です。
    def circlewave()
     a=make2d(501,501)
     for y in 0..500
      for x in 0..500
       a[y][x]=[0,0,(cos(distance(250,250,y,x)/10.0)/(1.1**(distance(250,250,y,x)/10.0)))+0.5]
            end
     end
     a
    end
    
    二枚目です。
    def circlewave()
     a=make2d(501,501)
     for y in 0..500
      for x in 0..500
       a[y][x]=[(-1*(cos(distance(250,250,y,x)/10.0)/(1.1**(distance(250,250,y,x)/10.0)))+0.5),0,(cos(distance(250,250,y,x)/10.0)/(1.1**(distance(250,250,y,x)/10.0)))+0.5]
            end
     end
     a
    end
    
  22. (00-440460D)

    数学的に考えるのが苦手だったので色々な関数を色々試して一番気に入ったものを投稿します。
    def fish()
      image=make2d(100,100)
      for y in 0..99
        for x in 0..99
          image[y][x]=[0,cos(y)+sin(x),sin(y)+cos(x)]
        end
      end
      show(image)
    end
    
  23. (00-640093F)

    昨年の課題も参考にしながら作成した。 <その1> まずは適当な初等関数を色々いじってみようと考え、適当に作ってみた。 添付した画像は n=500 の場合である。 sin, cos は-1から1の間で動くため扱いやすかったと思う。 また、乱数を使って表面を少しラフな質感にしてみた。 乱数の効き具合が大きすぎると(0から1まで全ての値をとるなど)画像が荒れた感じになるので、2割だけ効いてくるようにしたが、この部分を変数にするのも面白いかもしれない。 このままではcosが負の値を返しているところがあり、そこはどう処理されているかわからないができた画像が綺麗だったのでそのままにしておいた。
    # coding: utf-8
    def show_picture(n)
      a = make2d(n,n)
      n = n.to_f
      for y in 0 .. n-1
        for x in 0 .. n-1
          a[y][x]=[sin(x*PI/n)*(0.8+rand()/5), cos(y*PI/n)*(0.8+rand()/5) ,cos(x*PI/n)*sin(y*PI/n)]
        end
      end
      show(a)
    end
    
    <その2> それぞれハーマングリッド、きらめき格子と呼ばれる錯視図形を作成した。 ハーマングリッドは白線の交わる部分が黒く、きらめき格子は格子点の部分の白い円が黒く見えるというもの。 掲示板の画像の大きさだとちょっとわかりにくいかもしれない。

    きらめき格子は円を描く部分の処理にやや時間がかかってしまうが、個人的にはこちらの方がより目が「騙されている」感じがする。

    # coding: utf-8
    def hermann()
      a = make2d(1000,1000)
      for x in 0 .. 999
        for y in 0 .. 999
          a[x][y] = [0, 0, 0] #一度全て黒と設定する 
          for n in 1 .. 9 
            if x < (n * 100) + 5 && x > (n * 100) - 5
              a[x][y] = [1, 1, 1]
            elsif y < (n * 100)+5 && y > (n * 100) - 5
              a[x][y] = [1, 1, 1]
            end
          end #格子状に白線を引いた
        end
      end
      show(a)
    end
    
      
    def kirameki()
      a = make2d(1000,1000)
      for x in 0 .. 999
        for y in 0 .. 999
          a[x][y] = [0, 0, 0] #一度全て黒と設定する 
          for n in 1 .. 9 
            if x < (n * 100) + 5 && x > (n * 100) - 5
              a[x][y] = [0.5, 0.5, 0.5]
            elsif y < (n * 100)+5 && y > (n * 100) - 5
              a[x][y] = [0.5, 0.5, 0.5]
            end
          end #格子状に灰色の線を引いた
          for s in 1 .. 9 
            for t in 1 .. 9
              if (x-(s*100)) ** 2 + (y-(t*100)) ** 2 < 50
                a[x][y] = [1, 1, 1]
              end
            end
          end #各格子点に白い点を配置
        end
      end 
      show(a)
    end
    
    さきほど思いつきで書いたことをやってみた。 nが画像の大きさ、rが「ランダム度合」で、rは1から100までの間で指定する。 例によって r= 1000 などいい加減な値を代入しても出力はしてくれるが... 画像はn=500, r=75のときのもの。 rをいじって遊ぶとそれなりに楽しめる。
    # coding: utf-8
    def show_picture(n,r)
      a = make2d(n,n)
      n = n.to_f
      r = r.to_f
      r = r / 100
      for y in 0 .. n-1
        for x in 0 .. n-1
          a[y][x]=[sin(x*PI/n)*((1-r)+rand()*r), cos(y*PI/n)*(1-r+rand()*r) ,cos(x*PI/n)*sin(y*PI/n)]
        end
      end
      show(a)
    end
    
  24. (00-620298A)

    以上、スペードマークをモチーフにした画像のプログラミングでした。 関数の式を求めたり、多くの関数から区切られる範囲を指定するのはとても大変でした。 関数の式にはエクセルを使ったり地道に計算したりしました。三次関数2つ、円2つ、楕円2つ、一次関数8つ。文系ながらなんとか最後まで計算しました… 模様は割り算の余りを利用しました。 かなり初歩的なところからやり方がわからない状態でしたので、合計でおよそ8時間くらいはかかりましたが、いろいろ試行錯誤していくうちに楽しくなりました。 投稿した画像は保存してから開くと見やすいかと思います。
    def p(s,x,y)
      if 2*(y-s/4.0-75)/3.0>=-(4.8*((x-3*s/10.0)**3)/(10**5)-8.4*((x-3*s/10.0)**2) /(10**19)+55*(x-3*s/10.0)/100) && 2*(y-s/4.0-75)/3.0>=4.8*((x-7*s/10.0)**3)/(10**5)-8.4*((x-7*s/10.0)**2) /(10**19)+55*(x-7*s/10.0)/100 && y-75<=180*s/400.0 && (!(y-s/2.0-75>=-(x-130*s/400.0) && y-s/2.0-75>=x-270*s/400.0))
        1.0-(((distance(x,y,s/2.0,s/2.0+75))%45)/45)
      else
        if (distance(x,y,127*s/400.0,227*s/400.0+75)<=89*s/400.0 || distance(x,y,273*s/400.0,227*s/400.0+75)<=89*s/400.0) && (!(y-s/2.0-75<=x-130*s/400.0 && y-s/2.0-75<=-(x-270*s/400.0) && y-s/2.0-75>=-(x-130*s/400.0) && y-s/2.0-75>=x-270*s/400.0))
          1.0-(((distance(x,y,s/2.0,s/2.0+75))%45)/45)
        else
          if y-227*s/400.0-75>=-(4*(x-s/2.0)) && y-227*s/400.0-75>=4*(x-s/2.0) && (y-334*s/400.0-75)**2>=(1.0-(x-157*s/400.0)**2/36**2)*66**2 && (y-334*s/400.0-75)**2>=(1.0-(x-243*s/400.0)**2/36**2)*66**2 && y<=s+75 && (!(y-s/2.0-75<=x-130*s/400.0 && y-s/2.0-75<=-(x-270*s/400.0)))
            1.0-(((distance(x,y,s/2.0,s/2.0+75))%45)/45)
          else
            if y-s/2.0-75<=x-130*s/400.0 && y-s/2.0-75<=-(x-270*s/400.0) && y-s/2.0-75>=-(x-130*s/400.0) && y-s/2.0-75>=x-270*s/400.0
              1.0-(((distance(x,y,s/2.0,s/2.0+75))%21)/21)
            else
              0
            end
          end
        end
      end
    end
    
    def kadai(s)
      image = make2d(s+150,s) 
      for y in 0..(s+149)
        for x in 0..(s-1)
          image[y][x] = [11*p(s,x,y)/14.0,0,0]
        end        
      end        
      image
    end
    
    def kadai14
      kadai14 = kadai(400)
    end
    
  25. (00-640232C)

    四隅からの正弦波の干渉的なものです。
    def wave4corner(s)
      image = make2d(s,s)
      for y in 0..(s-1) 
        for x in 0..(s-1)
          image[y][x] = 0.25*((sin(distance(0,0,x,y)))+(sin(distance(s,s,x,y)))+sin(distance(s,0,x,y))+sin(distance(0,s,x,y))).abs
        end        
      end        
      image
    end
    
  26. (00-620295B)

    オオウチ錯視と呼ばれるものを作成しました。この錯視は真ん中の円が動いて見えるというものです。通常の状態では、実はそこまで見えるというわけではありません(もともとの原作の方も。。。)あまり動いているように見えなければ、画像か顔を動かしてみるといいかもしれません。

    中心からの距離、rの設定は、yとxを繰り返しで指定行ってから、書かないといけないというところがわからず苦労しました。また、ifの設定も数式をどう作っていいかわからず、苦労しましたが、昨年度のプログラムを参考にした結果、除数のあまりを使えばいいという発想に至りました。

    また、ファイル名、プログラム名がhomework2となっていますが、これは一つ目を作り始めたあとに、また違ったものを作ろうと思い、完成したものだからです。一つ目の作品は三角関数を使用して作ろうとしましたが、あまりうまく行かなかったため、今回は投稿していません。また時間があったら作り直して、投稿しようと思います。関数を用いたものを作ってみたいです。

    def homework2()
      a=make2d(200,220)
      for y in 0..199
        for x in 0..219
          r=distance(y,x,99,109)
          if r>70
            if y%10>4
              if x%50>24
                a[y][x]=[1,1,1]
              else
                a[y][x]=[0,0,0]
              end
            else
              if x%50>24
                a[y][x]=[0,0,0]
              else
                a[y][x]=[1,1,1]
              end
            end
          else
            if x%10>4
              if y%50>24
                a[y][x]=[0,0,0]
              else
                a[y][x]=[1,1,1]
              end
            else
              if y%50>24
                a[y][x]=[1,1,1]
              else
                a[y][x]=[0,0,0]
              end
            end
          end
        end
      end
      show(a)
    end
    
  27. (00-640239D)

    市松模様を作ってみました。
    def ichimatsu(s)
      image=make2d(s,s)
      for y in 0..s-1
        for x in 0..s-1
          image[y][x]=a(x,y)
        end
      end
      image
    end
    
    
    def a(x,y)
      if (x+y)%2 ==0
        1
      else
        0
      end
    end
    
    黒と白の縦ラインを作ってみた。
    def linear(s)
      image=make2d(s,s)
      for y in 0..s-1
        for x in 0..s-1
          image[y][x]=sin(x/4.0)
        end
      end
      image
    end
    
    レーザービームー
    対称的で美しいものをデザインしてみたいと思った。緑色をベースとした空間に赤色のレーザーが突き刺さる。しかしそのレーザーも謎めいた緑空間を通過していくに従って、目を見張るごとくの赤色はかすみ、ぼやけていく。気がつけば、レーザーは空間に同化され、もはや判別つかず、その先にはまるでまた別な未知なる世界が広がっているかのよう。
    def a(x,y)
      if x >= y
        y*1.0 / x*1.0
      else
        x*1.0 / y*1.0
      end
    end
    
    def color(s)
      image=make2d(s,s)
      for y in 0..s-1
        for x in 0..s-1
          image[y][x] = [a(x,y) ,1.0-a(x,y), (x+y)*0.5/(s-1)]
        end
      end
      image
    end
    
  28. (00-640138H)

    極座標で言うr(theta)=sin(5theta)のグラフを基本として、大きさを調節したり、色のグラデーションをつけたりした画像です。色のrgbを色々変えてみて、一番可愛らしい色になったのを選びました。
    画像はshow(hana(1000))を実行した時のものです。
    def hana(s)
      image = make2d(s,s)
      for y in 0..s-1
        for x in 0..s-1
          col =  (((x-s/2.0)**2+(y-s/2.0)**2)**0.5 - s/2*sin(5*asin((x-s/2.0)/((x-s/2.0)**2+(y-s/2.0)**2)**0.5)))/s
          image[y][x]=[((sin(8*col))**2)**0.5, 0.8 , ((cos(16*col))**2)**0.5]
        end
      end
      image
    end
  29. (00-640142A)

    色相環風の幾何学模様の描画です。アルゴリズムを大まかに述べると、小さい円の中心を大きい円の周上で動かし、位相ごとに円の内部を異なる色で塗るというものです。R,G,Bの値の大小を2*PI/3ずつずらすことで虹色のようにしています。

     ソースコードは添付しました(添付画像ではn=20,l=1000としています)。

    
    def colorcircle(n,l)
      a=make2d(l+1,l+1) # 座標の配列作成とRGB値入力の準備
      for i in 0..l
        for j in 0..l
          a[i][j]=[0, 0, 0]
        end
      end
      for color in 0..2 # RGB値をR,G,Bの順でそれぞれ入力する
        for k in 0..n-1 # 小さな円の中心Pの位相をずらしていく
          px=(l/2+l/4*cos(2*PI*k/n+2*PI/3*color)).to_i # (l/2,l/2) は大きい円の中心、P(px,py) は小さい円の中心 # R,G,Bでそれぞれ2*PI/3ごと位相をずらす
          py=(l/2+l/4*sin(2*PI*k/n+2*PI/3*color)).to_i
          for i in -l/4..l/4 # 小さい円の半径をl/4とする
            for j in -(sqrt(l**2/16-i**2)).to_i..(sqrt(l**2/16-i**2)).to_i # 円の内部のみ処理
              a[px+i][py+j][color]+=(l/2-sqrt(sqrt((px+i-l/2)**2+(py+i-l/2)**2)))/(l/2)*((k-n/2)/(n/2)).abs/n*2.5 # 大きい円の中心からの距離とPの位相に応じてRGB値を        end
          end
        end
      end
      show(a) # 画像を表示
    end
    
  30. (00-640131G)

    日本国旗とバングラデシュ国旗
    日本国旗からバングラデシュ国旗へとグラデーションで変化するような国旗をプログラムで書きました。
    def a(s,x,y)
      if x>(2.0/3.0)*y && distance(x,y,s,(3.0/2.0)*s)>s/2.0
        [0.9-0.1*(2.0*y-3.0*x)/(6.0*s),0.9-0.1*(2.0*y-3.0*x)/(6.0*s),0.85-0.15*(2.0*y-3.0*x)/(6.0*s)]
      elsif distance(x,y,s,(3.0/2.0)*s)>s/2.0
        [0.9-0.9*(2.0*y-3.0*x)/(6.0*s),0.9-0.4*(2.0*y-3.0*x)/(6.0*s),0.85-0.6*(2.0*y-3.0*x)/(6.0*s)]
      elsif x>(2.0/3.0)*y
        [1,0,0]
      else
        [1,(2.0*y-3.0*x)/(6.0*s),(2.0*y-3.0*x)/(6.0*s)]
      end
    end  
    
    def Flag_Japan_Bangladesh(s)
      image = make2d(2*s,3*s)
      for x in 0..(2*s-1)
        for y in 0..(3*s-1)
        image[x][y] = a(s,x,y)
        end
      end
      image
    end
    
  31. (00-640102G)

    三角関数を使って描きました。色合いは適当な数字を掛けて調整しました。
    def picture()
      a=make2d(512,512)
      for y in 0..511
        for x in 0..511
          t=
            if x+y>511
              7*tan(x+y)
            else
              2/sin(x+y)
            end
          s=
            if x>y
              7*tan(x+y)
            else
              2/sin(x+y)
            end
          a[x][y]=[t-s,tan(x*y*0.2),s+t]
        end
      end
      show(a)
    end
    
  32. (00-640120C)

    三角関数と距離を絡めてシンプルなものをつくりました。 この画像はk=500でやっていますが、k=1000でやると全ての曲線が円弧になり、それはそれで綺麗です。
    def picture_distance(k) 
      a=make2d(k,k)
      for y in 0..k-1
        for x in 0..k-1
       r=distance(0,0,x,y)/(x**(0.5)+y**(0.5))
       a[y][x]=[cos(r)%1.0,sin(r)%1.0,tan(r)%1.0]
      end
      end
      show(a)
    end
    
  33. (00-640112H)

    綺麗なグラデーション
    極座標表示の式で作りました
    def color_distort02(size)
      picture = make2d(size,size)
      center = size/2.0
    
      def arg(x,y,c)
        angle = acos((x-c)/distance(x,y,c,c))/(2*PI)
        if y < c
          then angle
        else 1 - angle
        end
      end
      def gap02(x,y,c)
        c / distance(x,y,c,c)
      end
      for y in 0 .. size-1
        for x in 0 .. size-1
          r = distance(x,y,center,center)
          theta = arg(x,y,center)
          e = gap02(x,y,center)
          picture[y][x] = [cos(2*PI*theta + e/100) + e,cos(2*PI*(1/6.0 + theta) + e/100) + e,cos(2*PI*(1/3.0 + theta) + e) + e/100]
        end
      end
      picture
    end
    
  34. (00-640103J)

    Minecraftというゲームのキャラクターであるクリーパーという敵mobをかいてみた。

    色は適当に目と口と足回り用に2色とそれ以外用に10色Illustratorのカラーパッドから緑系統の色選び、RGBで表した。 それを、列ベクトルに格納し、randを用いて色がランダムに現れるようにした。

    そして、for文を用いて中身が空の8×26の行列を作り、for文を用いてランダムな色をそれぞれの要素に格納した。 その後、目、口に個別に色を書き換え、足の部分に剰余とfor文を用いて色を書き換えた。

    a = [203.0/255, 217.0/255, 195.0/255]
    b = [112.0/255, 137.0/255, 109.0/255]
    c = [52.0/255, 122.0/255, 56.0/255]
    d = [186.0/255, 204.0/255, 183.0/255]
    e = [15.0/255, 107.0/255, 55.0/255]
    f = [98.0/255, 167.0/255, 87.0/255]
    g = [214.0/255, 219.0/255, 215.0/255]
    h = [81.0/255, 135.0/255, 82.0/255]
    i = [143.0/255, 201.0/255, 133.0/255]
    j = [78.0/255, 93.0/255, 74.0/255]
    k = [0.0/255, 0.0/255, 0.0/255]
    l = [34.0/255, 59.0/255, 30.0/255]
    
    color = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    
    color[0] = a
    color[1] = b
    color[2] = c
    color[3] = d
    color[4] = e
    color[5] = f
    color[6] = g
    color[7] = h
    color[8] = i
    color[9] = j
    
    color[rand(0..9)]
    
    creeper = Array.new(26)
    
    for i in 0..25 do
      creeper[i] = Array.new(8)
    end
    
    for i in 0..25 do
      for j in 0..7 do
        creeper[i][j] = color[rand(0..9)]
      end
    end
    
    creeper[3][2] = k
    creeper[3][5] = k
    creeper[2][1] = l
    creeper[3][1] = l
    creeper[2][2] = l
    creeper[2][5] = l
    creeper[2][6] = l
    creeper[3][6] = l #eyes
    
    creeper[6][2] = k
    creeper[5][3] = k
    creeper[6][3] = k
    creeper[5][4] = k
    creeper[6][4] = k
    creeper[6][5] = k
    creeper[5][2] = l
    creeper[7][2] = l
    creeper[4][3] = l
    creeper[4][4] = l
    creeper[5][5] = l
    creeper[7][5] = l #mouth
    
    for i in 0..7 do
      if i % 2 == 0
        creeper[24][i] = l
        creeper[25][i] = k
      else
        creeper[24][i] = k
        creeper[25][i] = l
      end
    end #feet
    
    show(creeper)
    
    初めに定義したRGBを入れ替えて、緑系統のクリーパーだけでなく他の色のバージョンについても作ってみた。

    ここでは、その一例として赤系統のものをあげた。

    画像として小さくなってしまうことを考慮していなかったのは失敗だった。

    a = [217.0/255, 203.0/255, 195.0/255]
    b = [137.0/255, 112.0/255, 109.0/255]
    c = [122.0/255, 52.0/255, 56.0/255]
    d = [204.0/255, 186.0/255, 183.0/255]
    e = [107.0/255, 15.0/255, 55.0/255]
    f = [167.0/255, 98.0/255, 87.0/255]
    g = [219.0/255, 214.0/255, 215.0/255]
    h = [135.0/255, 81.0/255, 82.0/255]
    i = [201.0/255, 143.0/255, 133.0/255]
    j = [93.0/255, 78.0/255, 74.0/255]
    k = [0.0/255,0.0/255,  0.0/255]
    l = [59.0/255, 34.0/255, 30.0/255]
    
    color = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    
    color[0] = a
    color[1] = b
    color[2] = c
    color[3] = d
    color[4] = e
    color[5] = f
    color[6] = g
    color[7] = h
    color[8] = i
    color[9] = j
    
    color[rand(0..9)]
    
    creeper = Array.new(26)
    
    for i in 0..25 do
      creeper[i] = Array.new(8)
    end
    
    for i in 0..25 do
      for j in 0..7 do
        creeper[i][j] = color[rand(0..9)]
      end
    end
    
    creeper[3][2] = k
    creeper[3][5] = k
    creeper[2][1] = l
    creeper[3][1] = l
    creeper[2][2] = l
    creeper[2][5] = l
    creeper[2][6] = l
    creeper[3][6] = l #eyes
    
    creeper[6][2] = k
    creeper[5][3] = k
    creeper[6][3] = k
    creeper[5][4] = k
    creeper[6][4] = k
    creeper[6][5] = k
    creeper[5][2] = l
    creeper[7][2] = l
    creeper[4][3] = l
    creeper[4][4] = l
    creeper[5][5] = l
    creeper[7][5] = l #mouth
    
    for i in 0..7 do
      if i % 2 == 0
        creeper[24][i] = l
        creeper[25][i] = k
      else
        creeper[24][i] = k
        creeper[25][i] = l
      end
    end #feet
    
    show(creeper)
    
  35. (00-640132J)

    授業の見本に三角関数を加えてみました。sの大きさysとkの大小によっては上手く出ませんが、基本的にsがある程度大きければ出力できます。なるべく綺麗に出力されるように画像はs=1500,k=10です。
    def picture_a(s,k)
      image=make2d(s,s)
      for x in (0..s-1)
        for y in (0..s-1)
          for i in (1..k)
            if y>=s*(sin(20*PI*x/s))/20+i*(s-1)/(k+1)-s/75 && y<=s*(sin(20*PI*x/s))/20+i*(s-1)/(k+1)+s/75
              image[y][x]=[1,1,1]
            elsif y>=s*(sin(20*PI*x/s))/20+i*(s-1)/(k+1)+s/75 && y<=s*(sin(20*PI*x/s))/20+(i+1)*(s-1)/(k+1)-s/75
              u=1.000000*i/(k+1)
              v=1.000000*x*i/((k+1)*s)
              image[y][x]=[v,1-u,0.5]
            end
          end
          if y>=s*(sin(20*PI*x/s))/20+k*(s-1)/(k+1)+s/75
            image[y][x]=[1.000000*x/s,0,0.5]
          end
          if y<=s*(sin(20*PI*x/s))/20+(s-1)/(k+1)-s/75
            image[y][x]=[1.000000*x/((k+1)*s),1,0.5]
          end
        end
      end
      image
    end
    
  36. (00-640092C)

    虹に見えるように色を調整しました。真ん中のほうの色がもっと綺麗になると良かったのですが・・・。
    def rainbow()
      a=make2d(50,50)
      for y in 0..49
        for x in 0..49
          a[y][x]=[y/49.0,x/49.0,1-x/49.0]
        end 
      end
      show(a)
    end
    
  37. (00-640119I)

    適当に三角関数を使ってそこそこ対称性のありそうなものを作成しました。
    def color_picture()
      a=make2d(512,512)
      for y in 0 .. 511
        for x in 0 .. 511
          a[y][x]=[sin(x*PI/256)*sin(y*PI/256),cos(x*PI/256)*sin(y*PI/256),tan((x*PI/256)*(y*PI/256))]
        end
      end
      show(a)
    end
    
  38. (00-440473D)

    螺旋形の作図
    def r(x,y)
      sqrt((x-250)**2+(y-250)**2)
    end
    def radify(x,y)
      if (y-250)>0
        acos((x-250)/r(x,y))
      else
        if y==250
          if x>=250
            0
          else
            PI
          end
        else
          2*PI-acos((x-250)/r(x,y))
        end
      end
    end
    def logarithmic(x,y)
      a=0
      while r(x,y)>exp((radify(x,y)+2*a*PI)*0.3063)
        a=a+1
      end
      if r(x,y)<(exp((radify(x,y)+2*a*PI)*0.3063)+exp((radify(x,y)+2*(a-1)*PI)*0.3063))/2
        b=0
        while 2*r(x,y)*cos(PI/3)/(exp(((2*(b+1)*PI)/(3+2*a)+2*a*PI)*0.3063)+exp(((2*(b+1)*PI)/(3+2*a)+2*(a-1)*PI)*0.3063))>1
         b=b+1
        end
        while radify(x,y)>(2*(b+1)*PI)/(3+2*a)+PI/3-acos(2*r(x,y)*cos(PI/3)/(exp(((2*(b+1)*PI)/(3+2*a)+2*a*PI)*0.3063)+exp(((2*(b+1)*PI)/(3+2*a)+2*(a-1)*PI)*0.3063)))
          b=b+1
        end
        c=((4*b*PI/(3.0+2*a)+2*a*PI)-radify(x,y))**3/25000.0
        [1,c,c]
      else
        d=(radify(x,y)+2*a*PI)/50.0
        [1,0.3+2*d,d]
      end
    end
    def scp()
      image=make2d(500,500)
      for x in 0..499
        for y in 0..499
          image[y][x]=logarithmic((607-y),(590-x))
        end
      end
      image
    end
    
  39. (00-640104C)

    三角関数を使って適当に書いてみました。
    def show_color_picture(n)
      a=make2d(n,n)
      for y in 0..n-1
        for x in 0..n-1
          a[y][x]=[sin(x+tan(x**2)).abs,sin(log(cos(y**2).abs+1)),2*asin((x-y)/(n-1)).abs/PI]
        end
      end
      show(a)
    end
    
  40. (00-640190J)

    マンデルブロ集合を描画してみました 境界線を少しぼやけさせてみました
    require "complex"
    
    def mandelbrot(cr, ci)
      limit = 255
      i = 0.0
      c = Complex(cr, ci)
      z = Complex(0, 0)
      while i < limit and z.abs < 2
        z = z * z + c
        i += 1.0
      end
      i / limit
    end
    
    def mandel_calc(min_r, min_i, max_r, max_i, delta)
      y = (max_i - min_i).abs / delta
      x = (max_r - min_r).abs / delta
      arr = Array.new(y){ Array.new(x) }
      i = 0
      p_i = min_i
      while p_i < max_i
        j = 0
        p_r = min_r
        while p_r < max_r
          arr[i][j] = Array.new(3, mandelbrot(p_r, p_i))
          p_r += delta
          j += 1
        end
        p_i += delta
        i += 1
      end
      arr
    end
    
    arr = mandel_calc(-2, -1, 1, 1, 0.004)
    show(arr)
    
    RGBではなくHSVとして解釈・変換して着色したバージョンも作ってみました
    def hsv_to_rgb h, s, v
        s /= 100.0
        v /= 100.0
        c = v * s
        x = c * (1 - ((h / 60.0) % 2 - 1).abs)
        m = v - c
        r, g, b = case
                  when h < 60  then [c, x, 0]
                  when h < 120 then [x, c, 0]
                  when h < 180 then [0, c, x]
                  when h < 240 then [0, x, c]
                  when h < 300 then [x, 0, c]
                  else              [c, 0, x]
                  end
        [r, g, b].map{|c| ((c + m) * 255).ceil }
    end
    
    ついでに、c=0.36+0.33iに固定したジュリア集合も描画してみました
    require "complex"
    
    def julia(cr, ci)
      limit = 255
      i = 0.0
      c = Complex(0.36, 0.33)
      z = Complex(cr, ci)
      while i < limit and z.abs < 2
        z = z * z + c
        i += 1.0
      end
      i / limit
    end
    
    def julia_calc(min_r, min_i, max_r, max_i, delta)
      y = (max_i - min_i).abs / delta
      x = (max_r - min_r).abs / delta
      arr = Array.new(y){ Array.new(x) }
      i = 0
      p_i = min_i
      while p_i < max_i
        j = 0
        p_r = min_r
        while p_r < max_r
          arr[i][j] = Array.new(3, julia(p_r, p_i))
          p_r += delta
          j += 1
        end
        p_i += delta
        i += 1
      end
      arr
    end
    
    arr = julia_calc(-2, -3, 2, 3, 0.004)
    show(arr)
  41. (00-640098A)

    いろいろな色を指定することができるようなrandom()を定義したあと、それをただ1000000個並べるという力こそパワーなプログラムを作りました。はじめは10000個にしていましたが、しょぼかったのでとりあえず1000000個にしたら砂嵐っぽくなりました。弊害として、学校のパソコンでは一回showするごとにターミナルの終了が必要になるようです。連続でshowすると、メモリ不足のエラーが出るか、画像もエラーすらも出ないという体たらく(私の)でした。
    def random()
      random = [1.0/rand(10),1.0/rand(10),1.0/rand(10)]
    end 
    
    def rad()
      k=0
      image = make2d(1000, 1000)
      for x in (0..999)
        image[0][x]=random
        image[1][x]=random
        image[2][x]=random
        ︙
        image[998][x]=random
        image[999][x]=random
      end
      image
    end 
    
  42. (00-640113A)

    三角関数を使って、模様を作った k=250の時の模様である。
    def show_color_picture(k)
      a=make2d(k,k)
      for y in 0..k-1
        for x in 0..k-1
          a[y][x]=[cos((x-y)/(k-1)),cos(x*y/(k-1)),sin((x+y)/(k-1))]
        end 
      end
      show(a)
    end