Author Topic: [RMVX/XP Script]Draw_Poligon and Draw_Triangle RGSS1/2 Command Scripts  (Read 551 times)

Magusmage

  • Porrean (+50)
  • *
  • Posts: 59
  • "I didn't create Lavos, just called him...I think"
    • View Profile
Here is the original CT's draw_poligon, draw_triangle and draw_line commands, but for Ruby Game Scripting System 1 and 2.
CREDITS FOR "Gab!" !!!
Code: [Select]
class Bitmap

  def draw_triangle(p1,p2,p3,w=1)
    draw_poligon(p1,p2,p3,w)
    nil
  end
 
  def draw_poligon(*p)
    p[-1].is_a?(Integer) ? (w = p[-1]; p.delete_at(-1)) : w = 1
    return if p.size < 2
    p << (last = [p[0][0], p[0][1]])
    p.each{|x,y| draw_line(last[0], last[1], x, y, w); last = [x,y]}
  end
  
  private
  def draw_line(x1, y1, x2, y2, w = 1)
    distance = (x1 - x2).abs + (y1 - y2).abs
    cor      = font.color
    (1..distance).each{|i|
      x = (x1 + 1.0 * (x2 - x1) * i / distance).to_i
      y = (y1 + 1.0 * (y2 - y1) * i / distance).to_i
      width == 1 ? self.set_pixel(x, y, cor) : self.fill_rect(x, y, w, w, cor)
    }
    nil
  end
end

How to Use

Well, first you'll need to have a little known of RGSS script programming. To draw a line, call the script:
Code: [Select]
@line = Sprite.new
@line.bitmap = Bitmap.new(W, H)
@line.bitmap.draw_line(x1, y1, x2, y2, LW)
Where:
W = bitmap's width
H = bitmap's Height
x1 = Line's initial x coord.
x2 = Line's end x coord.
y1 = Line's initial y coord.
y2 = Line's end y coord.
LW = Line "Weight" (optional, if not defined, it will return as "1")

To draw a triangle, call the script:
Code: [Select]
@triangle = Sprite.new
@triangle.bitmap = Bitmap.new(W, H)
@triangle.bitmap.draw_triangle(p1,p2,p3,TW)
Where:

W = bitmap's width
H = bitmap's Height
p1 = triangle's vertex 1 (use [x, y] to define it)
p2 = triangle's vertex 2 (use [x, y] to define it)
p3 = triangle's vertex 3 (use [x, y] to define it)
TW = Triangle Weight

To draw a poligon, just call the script:
Code: [Select]
@poligon = Sprite.new
@poligon.bitmap = Bitmap.new(W, H)
@poligon.bitmap.draw_poligon(p1, p2, p3, etc...)
Where:
W = bitmap's width
H = bitmap's Height
p(number) = poligon's vertex (number). You can add how many you want. (use [x, y] to define it)

Screenshot of the Script in Use

I used the "draw_poligon" to do it.

Well, here is the code that I used at the Scene_Title:
Code: [Select]
    # Draw_Background_triangle
    @triangle = Sprite.new
    @triangle.x = 80 ; @triangle.y = 80
    @triangle.bitmap = Bitmap.new(481, 321)
    @triangle.bitmap.font.color = Color.new(255, 0, 0)
    for e in 0..240
      i = e/2
      p1 = [240, 0+i]
      p2 = [0+e, 80+i]
      p3 = [96+i, 320-i]
      p4 = [384-i, 320-i]
      p5 = [480-e, 80+i]
      @triangle.bitmap.draw_poligon(p1,p2,p3,p4,p5)
    end
« Last Edit: March 05, 2011, 12:14:10 am by Magusmage »

utunnels

  • Guru of Reason Emeritus
  • Zurvan Surfer (+2500)
  • *
  • Posts: 2797
    • View Profile
Perfectly done. Except the gap in the pentagon. :wink:

Magusmage

  • Porrean (+50)
  • *
  • Posts: 59
  • "I didn't create Lavos, just called him...I think"
    • View Profile
@utunnels
Yeah. It was because I missed one or two coords.