Skip to content

Instantly share code, notes, and snippets.

@obelisk68
Last active October 19, 2018 01:14
Show Gist options
  • Save obelisk68/379735f999afa96fad14a3c0cf0309bd to your computer and use it in GitHub Desktop.
Save obelisk68/379735f999afa96fad14a3c0cf0309bd to your computer and use it in GitHub Desktop.
「コマ大数学科」GIFアニメ作成
require 'cairo'
require_relative 'gifanime'
require 'matrix'
include Math
Dir.chdir("picture") #作業用ディレクトリ
Width = 300
Surface = Cairo::ImageSurface.new(Cairo::FORMAT_ARGB32, Width, Width)
C = Cairo::Context.new(Surface)
class Vector
def scrn() [Width / 2 + self[0], Width / 2 - self[1]] end
end
R = Width / 2 - 20
Steps = 200.0
agl_step = 2 * PI / Steps
rad_step = R * 2 / Steps
pa = ->(n) {Vector[cos(agl_step * n), sin(agl_step * n)] * R}
pb = ->(n) {Vector[cos(PI + agl_step * n), sin(PI + agl_step * n)] * R}
pp = ->(n) {
a, b = pa.(n), pb.(n)
a + (b - a) / (2 * R) * rad_step * n
}
(Steps.to_i + 1).times do |i|
#背景色
C.set_source_color(Cairo::Color.parse("#ffe7d6"))
C.rectangle(0, 0, Width, Width)
C.fill
#円
C.set_source_rgb(1, 1, 1)
C.arc(Width / 2, Width / 2, R, 0, 2 * PI)
C.stroke
#線分AB
a, b = pa.(i).scrn, pb.(i).scrn
C.move_to(*a)
C.line_to(*b)
C.stroke
#点A
C.set_source_rgb(1, 0, 0)
C.arc(*a, 3, 0, 2 * PI)
C.fill
#点B
C.set_source_rgb(1, 0, 0)
C.arc(*b, 3, 0, 2 * PI)
C.fill
#点P
C.set_source_rgb(0, 0.5, 0)
(0..i).each do |j|
C.arc(*pp.(j).scrn, 3, 0, 2 * PI)
C.fill
end
Surface.write_to_png("%04d.png" % i)
end
40.times {|j| Surface.write_to_png("%04d.png" % (Steps.to_i + j + 1))} #2秒のwait
gifanime(5) #50ミリ秒間隔でGIF画像生成
# "gifanime.rb" -- http://obelisk.hatenablog.com/entry/2016/05/26/170134
# ブログ記事 -- https://marginalia.hatenablog.com/entry/2018/10/17/000843#20171003_3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment