Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Random MACL trouble; draw_polygon

[rgss]  #-------------------------------------------------------------------------
  # * Name      : Draw Polygon
  #   Info      : Draws a Polygon
  #   Author    : Caesar (Rewrote By Trickster)
  #   Call Info : Array vertices - Points of the polygon
  #               Integer width - Thickness of the lines
  #               Color color - color to draw it in
  #               Boolean filled - false outline true filled
  #               Integer step - fill steps
  #   Comments  : Example of vertices setup [[30, 80], [80, 80], [30, 60]]
  #-------------------------------------------------------------------------
  def draw_polygon(vertices, stroke = 1, color = Color.new(255, 255, 255),
    filled = false, step = 1)
    # Return if no width or not enough points
    return if stroke <= 0 or vertices.size <= 2
    # Get Count
    count = vertices.size
    # Get Points
    x1, y1, x2, y2 = vertices[-1] + vertices[0]
    # Draw Line
    draw_line(x1, y1, x2, y2, stroke, color)
    # Shade if filled
    shade_section(cx, cy, x1, y1, x2, y2, stroke, step, color) if filled
    # Run Through with next
    vertices.each_with_next do |start, point|
      # Get Points
      x1, y1, x2, y2 = start + point
      # Draw Line
      draw_line(x1, y1, x2, y2, stroke, color)
      # Shade if filled
      shade_section(cx, cy, x1, y1, x2, y2, stroke, step, color) if filled
    end
  end
  #-------------------------------------------------------------------------
  # * Name      : Shade Section
  #   Info      : Shades a section from (cx,cy), (x1,y1), (x2,y2)
  #   Author    : Trickster
  #   Call Info : Six to Nine Arguments
  #               Integer cx, cy, x1, y1, x2, y2 - Points
  #               Integer Thick - Line Thickness
  #               Integer Step - how many lines to draw (lower = higher accuracy)
  #               Color color - color to shade in
  #-------------------------------------------------------------------------
  def shade_section(cx, cy, x1, y1, x2, y2, thick = 1, step = 1,
      color = Color.new(255, 255, 255))
    # Reverse all parameters sent if 2 x is less than the first x
    x1, x2, y1, y2 = x2, x1, y2, y1 if x2 < x1    
    # Get Slope
    slope = (y2 - y1).to_f / (x2 - x1)
    # If Slope is infinite
    if slope.infinite?
      y1.step(y2, step) {|y| draw_line(cx, cy, x1, y, thick, color)}
    # If Slope is zero
    elsif slope.zero?
      x1.step(x2, step) {|x| draw_line(cx, cy, x, y1, thick, color)}
    elsif not slope.nan?
      # Get Y intercept
      yint = y1 - slope * x1
      x1.step(x2, step) {|x| draw_line(cx, cy, x, slope * x + yint, thick, color)}
    end
  end
[/rgss]

cx and cy are undefined, so it doesn't work. Does anyone know how it should figure them out? (Or what they're even for?)
 
It looks like the "shade_section" method fills in a triangle (3 points), so if cx & cy are set to any point on the polygon, it should work. Try setting them to the first point. Right above the first "shade_section" insert

cx, cy = x1, y1
 
Ah, that helps some. It's kinda working now. But that made some weird banding in half of it. So I think what it wants to do is make triangles from each set of three points in a row (0,1,2; 1,2,3; 2,3,4; etc), I edited it to do that and it seems to work.
[rgss]  def draw_polygon(vertices, stroke = 1, color = Color.new(255, 255, 255),
    filled = false, step = 1)
    # Return if no width or not enough points
    return if stroke <= 0 or vertices.size <= 2
    # Get Count
    count = vertices.size
    # Run Through
    for i in 0...count
      # Get Points
      x1, y1, x2, y2 = vertices[i - 0] + vertices[i - 1]
      # Draw Line
      draw_line(x1, y1, x2, y2, stroke, color)
      # Shade if filled
      if filled
        cx, cy = vertices[i - 2]
        shade_section(cx, cy, x1, y1, x2, y2, stroke, step, color)
      end
    end
  end
[/rgss]
Changed the "do |things|" iteration to a for loop because I have no idea how those work :/
 
fgsfds, you're right. I guess your way has to do.

EDIT
LinesD.png

So to deal with the lines like that I increase the step?
 
Er, yeah decrease the number, increase the amount of lines it draws.
Even with a step of .5 it didn't fill a larger octagon in fully, so I'll probably just use images instead of drawing it in realtime, since it's a bit slow.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top