らんだむな記憶

blogというものを体験してみようか!的なー

ブロックで描く円

Minecraftで円形を作りたいとかいうことで、rubyで書いてみたが。

#! /usr/bin/ruby -Ku

class CircleRenderer
    def initialize(radius)
        @radius = radius
    end

    def render
        center_x = @radius
        center_y = @radius

        adjust = 0.3    # 厳密な計算だとちょっと不恰好なので補正。

        0.upto(@radius * 2 + 1 - 1).each { |x|
            0.upto(@radius * 2 + 1 - 1).each { |y|
                if (x - center_x) ** 2 + (y - center_y) ** 2 <= (@radius + adjust) ** 2 
                    print ""
                else
                    print ""
                end
            }
            puts
        }
    end
end

################################################################################

def show_help
    print "ruby %s RADIUS\n" % __FILE__
end

if ARGV.size < 1
    show_help
    exit 1
end

CircleRenderer.new(ARGV[0].to_i).render

実にいけてないな...。

しかも案の定、Plotz Model Selectionのように、Minecraft用の立体物生成器があるんだなぁー。ブラウザ内で動くし...。canvas要素で3Dオブジェクトを描いているみたい。実装とか今の知識じゃ全然想像できないな。