概要
windows11に、sketchup6を入れてみた。
rubyで、3Dを書く。
練習問題やってみた。
練習問題
迷路を描け。
写真
サンプルコード
起動 Maze.new
require 'sketchup.rb'
class Maze
def build_maze(break_walls, maze)
model = Sketchup.active_model
entities = model.active_entities
path = Sketchup.find_support_file "Fence Picket Straight Wide.skp" ,"Components/Landscape/"
definitions = model.definitions
compo = definitions.load path
rows = maze.size
cols = maze[0].size
for i in 0..cols - 1
putc '_'
putc ' '
end
puts
for i in 0..rows - 1
putc '|'
for j in 0..cols - 1
if break_walls.any? { |a, b|
a == [i, j] && b == [i + 1, j]
}
putc ' '
else
putc '_'
point = Geom::Point3d.new 70 * i, 70 * j, 0
trans = Geom::Transformation.new point
entities.add_instance compo, trans
end
if break_walls.any? { |a, b|
a == [i, j] && b == [i, j + 1]
}
putc ' '
else
putc '|'
point = Geom::Point3d.new 70 * i, 70 * j, 0
trans = Geom::Transformation.new point
ins = entities.add_instance compo, trans
rot = Geom::Transformation.new point, Z_AXIS, 90.degrees
ins.transform! rot
end
end
puts
end
end
def fill(fm, to, maze)
rows = maze.size
cols = maze[0].size
for i in 0..rows - 1
for j in 0..cols - 1
if maze[i][j] == fm
maze[i][j] = to
end
end
end
end
def get_random_array(initAry)
randAry = Array.new
while(initAry.length > 0)
randAry.push(initAry.slice!(rand(initAry.length)))
end
return randAry
end
def make(rows, cols)
maze = []
for i in 0..rows - 1
maze << (0..cols - 1).map { |j|
cols * i + j
}
end
pairs = []
for i in 0..rows - 1
for j in 0..cols - 1
pairs << [[i, j], [i, j + 1]] if j < cols - 1
pairs << [[i, j], [i + 1, j]] if i < rows - 1
end
end
break_walls = []
pairsshuffle = get_random_array(pairs)
pairsshuffle.each { |a, b|
x = maze[a[0]][a[1]]
y = maze[b[0]][b[1]]
if x != y
break_walls << [a, b]
fill(x, y, maze)
end
}
build_maze(break_walls, maze)
end
def initialize
make 10, 10
end
end
以上。