Skip to content

Instantly share code, notes, and snippets.

@kaorukobo
Last active April 3, 2021 06:16
Show Gist options
  • Save kaorukobo/bed5078b7ba2dbcabf08891f05124a25 to your computer and use it in GitHub Desktop.
Save kaorukobo/bed5078b7ba2dbcabf08891f05124a25 to your computer and use it in GitHub Desktop.
「R2Dを使って、リサージュ図形をアニメーションしてみました https://maehrm.hatenablog.com/entry/20140208/p1 」のR2Dがdeprecatedなので、 Ruby2D gem 向けに書き換えたもの。
# -*- coding: utf-8 -*-
# Originally written by maehrm (id:rahaema) - https://maehrm.hatenablog.com/entry/20140208/p1
require "ruby2d"
include Math
w = Window
class Numeric
def to_rad
self*PI/180
end
end
alpha = 0
exp = ->(deg, alpha) {
# (x, y) = (cosθ, sin(2θ+α))
[cos(deg.to_rad)*200+w.width/2, -sin(2*deg.to_rad+alpha.to_rad)*200+w.height/2]
}
update do
Rectangle.new(x: 0, y: 0, width: w.width, height: w.height, color: 'black')
Text.new("α = #{alpha}", x: 10, y: 10, size: 30, color: 'green')
x1, y1 = exp.call(0, alpha)
5.step(360, 5) {|deg|
x2, y2 = exp.call(deg, alpha)
Line.new(x1: x1, y1: y1, x2: x2, y2: y2, width: 2, color: "red")
x1, y1 = x2, y2
}
alpha >= 330 ? alpha = 0 : alpha += 30
end
w.show
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment