6
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Elixirでraylibを使ってグラフィック描画してみる その2 〜 円を描く 〜

Posted at

今回の目標

Rayexに円を描く関数がなかった
raw_pixelを使って点で円を描く

前提知識

仕様

  • 1秒に5個円を描く
  • 半径は100px
  • x,yはランダム

ソース

defmodule RayexPixel do
  use Rayex

  def main() do
    init_window(800, 800, "window name")
    main_loop(true)
  end

  defp main_loop(true) do
    begin_drawing()

    1..5
    |> Enum.each(fn _ ->
      draw()
    end)

    end_drawing()

    Process.sleep(1000)
    main_loop(!window_should_close())
  end

  defp main_loop(_), do: nil

  defp draw() do
    x1 = Enum.random(1..800)
    y1 = Enum.random(1..800)

    1..360
    |> Enum.each(fn r ->
      point(r, x1, y1)
      |> then(fn [x, y] ->
        draw_pixel(x, y, %{r: 255, g: 161, b: 0, a: 255})
      end)
    end)
  end

  def point(r, x, y) do
    c = :math.pi() / 180
    px = :math.cos(c * r) * 100 + x
    py = :math.sin(c * r) * 100 + y
    [trunc(px), trunc(py)]
  end
end

実行結果

image.png

image.png

6
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?