Skip to content

Instantly share code, notes, and snippets.

@KonnorRogers
Created January 12, 2025 00:55
Show Gist options
  • Save KonnorRogers/c4f473df7d47640b6da77457acaaa1fd to your computer and use it in GitHub Desktop.
Save KonnorRogers/c4f473df7d47640b6da77457acaaa1fd to your computer and use it in GitHub Desktop.
drawing an "empty" circle in dragonruby
def draw_circle(args, diameter: 200)
diameter.times do |i|
r1 = diameter / 2
h1 = i - r1
l1 = Math::sqrt(r1 * r1 - h1 * h1)
r2 = r1 - 4 # Modify this to adjust the size of the inner circle. the bigger this is, the more empty space, the smaller, the less empty space.
h2 = (i - r2) - (r1 - r2)
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
circle = {
source_x: 0, source_y: 0,
source_w: diameter, source_h: diameter
}
args.outputs.sprites << circle.merge({
x: 500,
y: 500,
w: 200, h: 200, path: :open_circle,
})
end
@KonnorRogers
Copy link
Author

Screenshot 2025-01-11 at 6 14 06 PM

@KonnorRogers
Copy link
Author

A different, equally cursed way to draw a circle.

  # https://stackoverflow.com/questions/9007977/draw-circle-using-pixels-applied-in-an-image-with-for-loop
  def draw_circle(args, diameter: 240, stroke_width: 2)
    target = args.render_target(:circle)

    sprites = []
    degrees = 360
    radius = (diameter / 2) - stroke_width

    start_x = radius
    start_y = radius

    degrees.times do |i|
      angle = i
      x = radius * Math.cos(angle * Math::PI / 180)
      y = radius * Math.sin(angle * Math::PI / 180)
      sprites << {
        x: start_x + x,
        y: start_y + y,
        w: stroke_width,
        h: stroke_width,
        path: :pixel,
        r: 0, b: 0, g: 0, a: 255
      }
    end

    args.outputs[:circle].sprites.concat(sprites)

    circle = {
      source_x: 0, source_y: 0,
      source_w: diameter, source_h: diameter
    }

    args.outputs.sprites << circle.merge({
      x: 500,
      y: 300,
      w: diameter, h: diameter, path: :circle,
    })
  end
Screenshot 2025-01-11 at 8 20 33 PM

@KonnorRogers
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment