kotaweav / dragonruby-resources

kota's dragonruby resources
8 stars 1 forks source link

+TITLE: dragonruby resources

I will try to collect my own notes, tips, and other fun things dragonruby related. This is also where all resources and code from the [[https://www.youtube.com/channel/UCzFlGBRh9YNy1QMEYWafblQ][dev vlog and tutorials]] will go.

+begin_src ruby :session

class Vec3 def initialize x, y, z @arr = [x, y, z] end

def to_a
  @arr
end

end

v = Vec3.new 2, 3, 4

p *v

+end_src

+RESULTS:

| 2 | 3 | 4 |

** Matrix Multiplication Matrix multiplication is pretty trivial in Ruby.

+begin_src ruby :session

def matmul mat1, mat2 mat1.collect do |row1| mat2.transpose.collect do |col1| col1.zip(row1).inject(0) do |sum, pair| sum + pair[0] * pair[1] end end end end

a = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] b = [[7, 8], [9, 10], [11, 12]]

matmul a, b

+end_src

+RESULTS:

| 58 | 64 | | 139 | 154 | | 220 | 244 | | 301 | 334 |

** Drawing a circle using drgtk render targets

We draw images pretty simply by using pythagorean thm:

+DOWNLOADED: screenshot @ 2021-03-14 00:28:52

[[file:Littleruby/_drgtk_things/2021-03-14_00-28-52_screenshot.png]]

+begin_src ruby

def tick args setup args display args end

def setup args if args.state.tick_count == 0 200.times do |i| r = 100 h = i - r l = Math::sqrt(r r - h h) args.render_target(:circle).lines << [i, r - l, i, r + l] end

  200.times do |i|
    r1 = 100
    h1 = i - r1
    l1 = Math::sqrt(r1 * r1 - h1 * h1)
    r2 = 50
    h2 = i - r2 - r1 / 2
    if h2.abs < r2
      l2 = Math::sqrt(r2 * r2 - h2 * h2)
      args.render_target(:open_circle).lines << [i, r1 - l2, i, r1 - l1]
      args.render_target(:open_circle).lines << [i, r1 + l2, i, r1 + l1]
    else
      args.render_target(:open_circle).lines << [i, r1 - l1, i, r1 + l1]
    end
  end

  200.times do |i|
    r1 = 100
    h1 = i - r1
    l1 = Math::sqrt(r1 * r1 - h1 * h1)
    r2 = 50
    h2 = i - r2 - r1 / 3
    if h2.abs < r2
      l2 = Math::sqrt(r2 * r2 - h2 * h2)
      args.render_target(:offset_circle).lines << [i, r1 - l2, i, r1 - l1]
      args.render_target(:offset_circle).lines << [i, r1 + l2, i, r1 + l1]
    else
      args.render_target(:offset_circle).lines << [i, r1 - l1, i, r1 + l1]
    end
  end
end

end

def display args args.outputs.sprites << { x: 500, y: 500, w: 200, h: 200, path: :circle, source_x: 0, source_y: 0, source_w: 200, source_h: 200 } args.outputs.sprites << { x: 300, y: 200, w: 200, h: 200, path: :open_circle, source_x: 0, source_y: 0, source_w: 200, source_h: 200 } args.outputs.sprites << { x: 800, y: 500, w: 200, h: 200, path: :offset_circle, source_x: 0, source_y: 0, source_w: 200, source_h: 200 } end

+end_src

+DOWNLOADED: https://media.discordapp.net/attachments/674410581326823446/815862136562909224/unknown.png?width=960&height=593 @ 2021-03-14 00:18:59

[[file:Littleruby/_drgtk_things/2021-03-14_00-18-59_unknown.png.png]]

** Drawing an isosceles triangle using drgtk render targets drawing isosceles (two sides are the same length) triangles is pretty trivial:

+begin_src ruby

def tick args if args.state.tick_count.zero? setup args end

args.outputs.sprites << { x: 400, y: 200, w: args.state.triangle.w, h: args.state.triangle.h, r: 255, g: 0, b: 0, path: :triangle } end

def setup args args.state.triangle.w = 400 args.state.triangle.h = 100 triangle_rt args, :triangle, args.state.triangle.w, args.state.triangle.h end

def triangle_rt args, label, w, h slope = h / (w / 2) args.outputs[label].w = w args.outputs[label].h = h args.outputs[label].lines << (args.state.triangle.w.times.map do |x| { x: x, y: 0, x2: x, y2: [ slope x, -slope x + h * 2 ].min, r: 255, g: 255, b: 255 } end) end

+end_src

+DOWNLOADED: screenshot @ 2021-06-23 23:02:26

[[file:Littleruby/_drgtk_things/2021-06-23_23-02-26_screenshot.png]]