タートルグラフィックス
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Turtle | |
def initialize(width, height, ob) | |
@width, @height = width, height | |
@pen_po = Vector[0, 0] | |
@pen = ob | |
@dir = Vector[1, 0] | |
@color = [65535, 65535, 65535] | |
end | |
attr_accessor :pen_po, :dir | |
def left(deg) | |
θ = PI * deg / 180 | |
@dir = Matrix[[cos(θ), -sin(θ)], [sin(θ), cos(θ)]] * @dir | |
end | |
def right(deg) | |
left(-deg) | |
end | |
def forward(length, draw = true) | |
next_po = @pen_po + @dir * length | |
if draw | |
@pen.color(*@color) | |
@pen.line(@width / 2 + next_po[0], @height / 2 - next_po[1], | |
@width / 2 + @pen_po[0], @height / 2 - @pen_po[1]) | |
end | |
@pen_po = next_po | |
end | |
def back(length) | |
forward(-length, false) | |
end | |
def color(r, g, b) | |
@color = [r, g, b] | |
@pen.color(*@color) | |
end | |
def circle(radius, fill = false) | |
@pen.color(*@color) | |
@pen.circle(fill, @width / 2 + @pen_po[0], @height / 2 - @pen_po[1], radius) | |
end | |
def move(x, y) | |
@pen_po = Vector[x, y] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment