rubysketchでふよふよ

December 16, 2020

Advent Calendarで良さげなものを知りました。

https://zenn.dev/tokujiros/articles/96f63899760cb0

というわけで早速ふよふよを書いてみました。

https://ruby.tnantoka.com/entry/2019/12/17/231125

「ふよふよ」とは僕が新たな環境でお絵描き系プログラミングを始めるときに、いつも試しに描いてみるものです。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
require 'rubygems'
require 'bundler/setup'
Bundler.require

R = 100 # RADIUS is used by rubysketch
FREQUENCY = 10
SIZE = 300

Point = Struct.new(:x, :y)

size SIZE, SIZE
center = Point.new(width / 2, height / 2)

amplitude = 0
amplitude_dir = 1

draw do
  background('#ffffff')

  points = 0.step(to: 361, by: 0.05).map do |degrees|
    radians = degrees * Math::PI / 180
    noise = Math.sin(radians * FREQUENCY) * amplitude
    Point.new(
      center.x + Math.cos(radians) * (R + noise),
      center.y + Math.sin(radians) * (R + noise)
    )
  end

  points.each_slice(2) do |p1, p2|
    point p1.x, p1.y
  end

  amplitude += amplitude_dir
  amplitude_dir *= -1 unless (-14..14).include?(amplitude)
end

コードはこれだけです。

ちゃんとふよふよしています。

なんとiOSでも動くそうでこれは良い環境ですね。

ソースコードはこちら(この記事に載せせたのが全てですが)

https://github.com/tnantoka/rubysketch-fuyofuyo