11/12の課題

  1. ( g840523 )

    def show_color_picture
     a=make3d(50,50,3)
     for y in 0..49
       for x in 0..49
          a[y][x]=[(y%2)*(y*30/10)/150.0,(x%2)*(1.0-x/50),((x-y)/50)**2]
       end
     end
     show(a)
    end
    

    def show_color_picture
      a=make3d(50,50,3)
      for y in 0..49
        for x in 0..49
          a[y][x]=[((x+y)%4)*(y*30/10)/150.0,(((x-25)/25.0)**2+((y-25)/25.0)**2)/2.0,((x-y)%4)*(x+y)/100.0]
        end
      end
      show(a)
    end
    
  2. ( g840191 )

    include Math
    def d(x,y,r)
      sqrt((x-r)**2+(y-r)**2)
    end
    def show_color_picture(r)
      image=make2d(2*r,2*r)
      for y in 0..2*r-1
        for x in 0..2*r-1
          image[y][x]=art(x,y,r)
        end
      end
      show(image)
    end
    def art(x,y,r)
      c=make1d(3)
      if d(x,y,r)>=r
        c[1]=1.0
      else
        c[1]=(d(x,y,r)-r)/r
      end
      c[2]=cos(d(x,y,r)/5.0)
      c[0]=log(d(x,y,r)/100.0+1)
      c
    end
    

    def art(x,y,r)
      c=make1d(3)
      if d(x,y,r)>=r
        c[1]=1.0
      else
        c[1]=(d(x,y,r)-r)/2/r
      end
      c[2]=cos(d(x,y,r)/3.5)
      c[0]=sin((x**2+y**2)/20)
      c
    end
    
  3. ( g840186 )

    def image(n,p)
      c = make2d(n,n)
      d = make2d(n,n)
      for i in 0..n-1
        c[i][0] = 1
        c[0][i] = 1
      end
      for i in 0..n-1
        for j in 0..n-1
          c[i][j] = c[i-1][j] + c[i][j-1]
          if c[i][j] % p != 0
            d[i][j] = 1
          end
        end
      end
      d
    end
    
  4. ( g840183 )

    def nyo(x,y)
      if (x%y)>=y/2.0
        (x%y)*y
      else
        (y-(x%y))*y
      end
    end
    def show_color_picture
      a=make3d(101,101,3)
      for y in 0..100
        for x in 0..100
          a[y][x]=[(nyo(x,10)/100.0)**2.0,(nyo(y,10)/100.0)**2.0,0.0]
        end 
      end
      show(a)
    end
    
  5. ( g840547 )

    def color1(x,y)
      [sin(x/10.0)**2,cos(y/10.0)**2,(x+y)/200.0]
    end
    
    def show_color_picture1
      a=make3d(100,100,3)
      for y in 0..99
        for x in 0..99
          a[y][x]=color1(x,y)
        end
      end
      show(a)
    end
    

    def color2(x,y)
      if (x+2*y)%5==0
        [0,0,0]
      else
        if (x+2*y)%5==1
          [0,1,0]
        else 
          if (x+2*y)%5==2
            [1,0,0] 
          else
            if (x+2*y)%5==3
              [0,0,1]
            else
              [1,1,1]
            end
          end
        end
      end
    end
    def show_color_picture2
      a=make3d(10,10,3)
      for y in 0..9
        for x in 0..9
          a[y][x]=color2(x,y)
        end
      end
      show(a)
    end
    

    def color3(x,y,r)
      [cos(sqrt(x**2+(y-r)**2)/5.0)**4+0.5*cos(sqrt((x-r)**2+(y-2*r-1)**2)/5.0)**4
       ,cos(sqrt((x-r)**2+y**2)/5.0)**4+0.5*cos(sqrt((x-r)**2+(y-2*r-1)**2)/5.0)**4
       ,cos(sqrt((x-2*r-1)**2+(y-r)**2)/5.0)**4+0.5*cos(sqrt((x-r)**2+(y-2*r-1)**2)/5.0)**4]
    end
    
    def show_color_picture3
      a=make3d(200,200,3)
      for y in 0..199
        for x in 0..199
          a[y][x]=color3(x,y,99)
        end
      end
      show(a)
    end
    

    def stardust
      (0.5005*rand()).round
    end
    def show_picture3(r)
      a=make2d(2*r,2*r)
      for y in 0..2*r-1
        for x in 0..2*r-1
          a[y][x]=stardust
        end
      end
      for y in 0..r-1 #上半分
        for x in 0..2*r-1
          if (0.7*r)**2<(x-r)**2 +((y-r)*5)**2 && (x-r)**2 +((y-r)*5)**2 < r**2
            a[y][x] = ((x-r)**2 + ((y-r)*5)**2)/(1.0*r**2)
          end
        end
      end
      for y in 0..2*r-1
        for x in 0..2*r-1
          if (x-r)**2 +(y-r)**2<(3*r*1.0/5)**2
            a[y][x]=(x**2+y**2)/(6.0*r**2)
          end
        end
      end
      for y in r..2*r-1 #下半分
        for x in 0..2*r-1
          if (0.7*r)**2<(x-r)**2 +((y-r)*5)**2 && (x-r)**2 +((y-r)*5)**2 < r**2
            a[y][x] = ((x-r)**2 + ((y-r)*5)**2)/(1.0*r**2)
          end
        end
      end
      show(a)
    end
    


    def re(k,l,theta,r)
      a=make2d(2*r+1,2*r+1)
      0.step(2*PI,0.005){|t|
        x=r+(r*sin(k*t)).round
        y=r+(r*cos(l*t+theta)).round
        a[y][x]=1
      }
      a
    end
    
  6. ( g840173 )

    def picture
      a=make2d(500,500)
      for i in 0..499
        for j in 0..499
          x=(j-249.50)/249.50
          y=(i-249.50)/249.50
          m=500.00
          if (y-x*(x-1)*(x+1))*(x+y*(y-1)*(y+1)) <= 0 
            a[i][j] = [(1.0-abs(y-x))**2,(1.0-abs(y-x))**2,(1.0-abs(y+x))**2]
            else
            a[i][j] = [1.0-(1.0-abs(y-x))**2,(1.0-abs(y-x))**2,(1.0-abs(y+x))**2] 
          end
        end
      end
      a
    end
    
  7. ( g840427 )

    def dist(x1,y1,x2,y2)
      sqrt((x1-x2)**2+(y1-y2)**2)
    end
    def angl(x,y,x0,y0,e1,e2)
      atan2(x-x0,y-y0)-atan2(e1,e2)
    end
    def max(x,y)
      if x>y
        x
      else
        y
      end
    end
    def color_b(x,y,size)
      r=dist(x,y,size/2,size)
      s=angl(x,y,size/2,size,1,0)
      c=exp(-r*0.01)*(sin(s*16)+1.2)
      [c*x/size+0.1,c*max(x/size,1-x.to_f/size),c*(1-x.to_f/size)+0.1]
    end
    def show_color_picture
      size=512
      a=make3d(size,size,3)
      for y in 0..(size-1)
        for x in 0..(size-1)
          a[y][x]=color_b(x,y,size)
        end 
      end
      show(a)
    end
    
  8. ( g840432 )

    def colorpicture(n,m,l)
      a=make3d(n,m,l)
      for y in 0..n-1 do
        for x in 0..m-1 do
          a[y][x]=[cos(((x+y)*3.1415926)/l),sin(((x+y)*3.1415926)/l),1,0]
        end
      end
      a
    end
    

    def ellipse(x,y,a,b,p,q)
      (((x-p)**2*1.0)/(a**2))+(((y-q)**2*1.0)/(b**2)) > 1 && 
        (((x-p)**2*1.0)/((a+1)**2))+(((y-q)**2*1.0)/((b+1)**2)) <= 1 
    end
    def hypebo1(x,y,a,b,p,q)
      (((x-p)**2*1.0)/(a**2))-(((y-q)**2*1.0)/(b**2)) > 1 && 
        (((x-p)**2*1.0)/((a+1)**2))-(((y-q)**2*1.0)/((b+1)**2)) <= 1 
    end
    def color1(x,y,a,b)
      if b > 1 then
        if ellipse(x,y,150,150,a,b) then
          cos(3.1415926*(((a+b)*1.0)/100))
        else
          if hypebo1(x,y,150,150,a,b) then
            cos(3.1415926*(((a+b)*1.0)/100))
          else
            color1(x,y,a-1,b-1)
          end
        end
      else
        1.0
      end
    end
    def color2(x,y,a,b)
      if b > 1 then
        if ellipse(x,y,150,150,a,b) then
          sin(3.1415926*(((a+b)*1.0)/100))
        else
          if hypebo1(x,y,150,150,a,b) then
            sin(3.1415926*(((a+b)*1.0)/100))
          else
            color2(x,y,a-1,b-1)
          end
        end
      else
        1.0
      end
    end
    def color3(x,y,a,b)
      if b > 1 then
        if ellipse(x,y,150,150,a,b) then
          1.0
        else
          if hypebo1(x,y,150,150,a,b) then
            1.0
          else
            color3(x,y,a-1,b-1)
          end
        end
      else
        ( x*1.0)/99
      end
    end
    def showcolorpicture
      a=make3d(300,300,3)
      for y in 0..299 do
        for x in 0..299 do
          a[x][y]=[color1(x,y,60,30),color2(x,y,60,30),color3(x,y,60,30)]
        end
      end
      a
    end
    
  9. ( g840446 )

    def gra()
      a=Array.new(50)
      for i in 0 ..49
        a[i]=Array.new(50)
        for j in 0.. 49
          a[i][j]=0.01*(i+j)
        end
      end
      a
    end
    

    def show_color_picture(size)
      a=make3d(size,size,3)
      for y in 0..size-1
        for x in 0..size-1
          xx=x-(size/2)
          yy=y-(size/2)
          r=sqrt(xx**2+yy**2)/5
          q=atan2(yy,xx)
          if r <= PI then 
            if (r-q).abs <  0.2 then
              a[x][y][0]=1-q/15
            end
          elsif r <= 2*PI then
            if (r-2*PI-q).abs <  0.2 then
              a[x][y][0]=1-(q+2*PI)/15
            end
          elsif r <= 3*PI then
            if (r-2*PI-q).abs <  0.2 then
              a[x][y][0]=1-(q+2*PI)/15
            end
          else
            if (r-PI*4-q).abs <  0.3 then
              a[x][y][0]=1-(q+4*PI)/15
            end
          end
          a[x][y][2]=(r*5)/(size/2)
          if x >= y then
            a[x][y][1]=0.3
          elsif size-x >= y then
            a[x][y][1]=0.2
          else
            a[x][y][1]=0.1
          end
        end
      end 
      show(a)
    end
    
  10. ( g840053 )

    def d(x,y,z,w)
      sqrt((x-z)**2+(y-z)**2)
    end
    def image()
      x=128
      image=make2d(x,x)
      for i in 0..x-1
        for j in 0..x-1
          image[i][j]=[1.0/(sqrt(2)*x)*d(i,j,x/6,x/6),1.0/(sqrt(2)*x)*d(i,j,x/2,x/2),1.0/(sqrt(2)*x)*d(i,j,(x-1)*5/6,(x-1)*5/6)]
        end
      end
      show(image)
    end
    
  11. ( g840050 )

    def show_color_picture
      a=make3d(200,200,3)
      for y in 0..199
        for x in 0..199
          if((y <= 199.0*sin(0.1*x).abs && y >= -199.0*sin(0.1*x).abs+199)||(y >= 199.0*sin(0.1*x).abs && y <= -199.0*sin(0.1*x).abs+199))
            a[y][x]=[1.0-x/199.0,x/199.0,1.0]
          else
            a[y][x]=[x/199.0,1.0,1.0-x/199.0]
          end
        end 
      end
      show(a)
    end
    
  12. ( g840428 )

    def show_color_picture
      a=make3d(50,50,3)
      for y in 0..49
        for x in 0..49
          a[y][x]=[x/49.0,1.0-rand()*y]
        end
      end
      show(a)
    end
    
  13. ( g840180 )

    def show_color_picture
      a=make3d(128,128,3)
      for y in 0..127
        theta = y/127.0*1.57
        p1 = sin(theta)
        p2 = sin(theta+1.57/3.0)
        p3 = sin(theta+1.57/1.5)
        for x in 0..127
          q = cos(x/127.0*1.57)
          a[y][x][1] = (p1+q)/2.0
          a[y][x][2] = (p2+q)/2.0
          a[y][x][0] = (p3+q)/2.0
        end
      end
      show(a)
    end
    
  14. ( g840437 )

    def show_color_picture
      a=make3d(200,200,3)
      for y in 0..199
        for x in 0..199
          a[y][x]=[x/199.0,rand(),1 -(y/199.0)]
        end
      end
      show(a) 
    end
    
  15. ( g840441 )

    def show_color_picture
      a=make3d(50,50,3)
      for y in 0..49
        for x in 0..49
          a[y][x]=[sqrt((x**2+y**2)/4802.0),sqrt(((49.0-x)**2+(49.0-y)**2)/4802.0),(x-y+49.0)/98.0]
        end 
      end
      show(a)
    end
    
  16. ( g840534 )

    def assignment1112a
      a=make3d(100,100,100)
      for i in 0..99
        for j in 0..99
          a[i][j][0]=(sin(sqrt( (i-49)**2+(j-49)**2)/(49*sqrt(2))*3)+1)/2
        end
      end
      for i in 0..99
        for j in 0..99
          a[i][j][1]=(sin(sqrt( (i-49)**2+(j-49)**2)/(49*sqrt(2))*27)+1)/2
        end
      end
      for i in 0..99
        for j in 0..99
          a[i][j][2]=(sin(sqrt( (i-49)**2+(j-49)**2)/(49*sqrt(2))*81)+1)/2
        end
      end
      show(a)
    end
    
  17. ( g840526 )


    def make1(n)
      a=Array.new(n)
      for i in 0..(n-1)
        a[i]=0.8
      end
      a
    end
    def make2(n)
      a=Array.new(n)
      for i in 0..(n-1)
        a[i]=make1(n)
      end
      a
    end
    def dis (x,y)
      (x-99)**2+(y-99)**2
    end
    def draw2()
      light_x=-100
      light_y=-100
      light_z=-200
      image=make2(199)
      image2=make2(199)
      for i in 0..199-1
        for j in 0..199-1
          if dis(i,j)<3600
            image2[j][i]=0.2
          else
            image2[j][i]=0.0
          end
        end
      end
      for j in 0..199-1
        for i in 0..199-1
          if 3600-(i-99)**2-(j-99)**2>0
            z=sqrt(3600-(i-99)**2-(j-99)**2)
            vec_x=i-light_x
            vec_y=j-light_y
            vec_z=z-light_z
            vec2_x=99-i
            vec2_y=99-j
            vec2_z=z
            c_theta=(vec_x*vec2_x+vec_y*vec2_y+vec_z*vec2_z)/(sqrt(vec_x**2+vec_y**2+
                                                                   vec_z**2)*sqrt(vec2_x**2+vec2_y**2+vec2_z**2))
            if c_theta>0
              image2[j][i]+=0.8*c_theta**2
            end
          end
          #かげ
          vec_x=light_x-99
          vec_y=light_y-99
          vec_z=light_z
          vec2_x=i-light_x
          vec2_y=j-light_y
          vec2_z=100-light_z
          c_theta=(vec_x*vec2_x+vec_y*vec2_y+vec_z*vec2_z)/(sqrt(vec_x**2+vec_y**2+
                                                                 vec_z**2)*sqrt(vec2_x**2+vec2_y**2+vec2_z**2))
          if (1-c_theta**2)>=0	
            if sqrt(1-c_theta**2)*sqrt(vec_x**2+vec_y**2+vec_z**2)<60
              image[j][i]=0.0
            end
          else
            image[j][i]=0.0
          end
        end
      end
      for j in 0..199-1
        for i in 0..199-1
          if image2[j][i]>0
            image[j][i]=image2[j][i]
          end
        end
      end
      show(image)
    end
    
  18. ( g840550 )

    def show_color_picture
      a=make3d(50,50,3)
      for y in 0..49
        for x in 0..49
          a[y][x]=[x/49.0,y/49.0,rand()]
        end 
      end
      show(a)
    end
    

    def show_color_picture
      a=make3d(50,50,3)
      for y in 0..49
        for x in 0..49
          a[y][x]=[sin(PI*x/49.0),cos(PI*y/49.0).abs(),tan(PI*(x+y)/98.0).abs()]
        end
      end
      show(a)
    end
    
  19. ( g840431 )

    def show_wave
      wavesource1x=200
      wavesource1y=50
      wavesource2x=200
      wavesource2y=5
      wavesource3x=200
      wavesource3y=95
      a=make3d(100,100,3)
      for y in 0..99
        for x in 0..99
          r1=sqrt((x-wavesource1x)**2+(y-wavesource1y)**2)
          r2=sqrt((x-wavesource2x)**2+(y-wavesource2y)**2)
          r3=sqrt((x-wavesource3x)**2+(y-wavesource3y)**2)	
          a[x][y]=[0.5 +(sin( r1 / 2.0))/2,0.5 +(sin( r2 / 2.0))/2,0.5 +(sin( r3 / 2.0))/2]
        end
      end
      show(a)
    end
    
  20. ( g840546 )

    def rose(n,r)
      a=make2d(2*r+1,2*r+1)
      0.step(2*PI,0.005){|i|
        x=r*cos(n*i)*cos(i)+r
        y=r*cos(n*i)*sin(i)+r
        a[y][x]=1
      }
      a
    end