TheoAllen

Iseng2 RGSS3 (7) Shape

Jun 1st, 2013
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.37 KB | None | 0 0
  1. def basic_update
  2.   Graphics.update
  3.   Input.update
  4. end
  5.  
  6. class Bitmap
  7.  
  8.   def draw_line(x1,y1,x2,y2,color = Color.new(255,255,255))
  9.     jarak_x = (x2-x1)
  10.     jarak_y = (y2-y1)
  11.     if jarak_y.abs == 0 || jarak_x.abs == 0
  12.       draw_horz(x1,y1,jarak_x,color) if jarak_y.abs == 0
  13.       draw_vert(x1,y1,jarak_y,color) if jarak_x.abs == 0
  14.       return
  15.     end
  16.     maximum = [jarak_x.abs,jarak_y.abs].max
  17.     rasio_x = jarak_x / maximum.to_f
  18.     rasio_y = jarak_y / maximum.to_f
  19.     real_x = x1.to_f
  20.     real_y = y1.to_f
  21.     for i in 0..maximum
  22.       Graphics.update
  23.       set_pixel(x1,y1,color)
  24.       real_x += rasio_x
  25.       real_y += rasio_y
  26.       x1 = real_x.round
  27.       y1 = real_y.round
  28.     end
  29.   end
  30.  
  31.   def draw_horz(x,y,width,color = Color.new(255,255,255))
  32.     if width < 0
  33.       fill_rect(x+width+1,y,width.abs,1,color)
  34.       return
  35.     end
  36.     fill_rect(x,y,width,1,color)
  37.   end
  38.  
  39.   def draw_vert(x,y,height,color = Color.new(255,255,255))
  40.     if height < 0
  41.       fill_rect(x,y+height+1,1,height.abs,color)
  42.       return
  43.     end
  44.     fill_rect(x,y,1,height,color)
  45.   end
  46.  
  47.   def draw_elipse(x,y=0,horz=1,vert=1,color=Color.new(255,255,255),step=0.1)
  48.     if x.is_a?(Ellipse)
  49.       elipse = x
  50.       x = elipse.x
  51.       y = elipse.y
  52.       horz = elipse.horz
  53.       vert = elipse.vert
  54.     end
  55.     ori_x = x
  56.     ori_y = y
  57.     x += horz
  58.     degree = 0.0
  59.     while degree <= 360
  60.       Graphics.update
  61.       set_pixel(x,y,color)
  62.       x = Math.cos(Math.radian(degree)) * horz + ori_x
  63.       y = Math.sin(Math.radian(degree)) * vert + ori_y
  64.       degree = [degree+step,360].min
  65.     end
  66.   end
  67.  
  68.   def draw_shape_params(x,y,params,length,color = Color.new(255,255,255))
  69.     return unless params.is_a?(Array)
  70.     return unless params.size >= 3
  71.     degree_minus = 360 / params.size
  72.     degree = 270
  73.     coordinate = []
  74.     params.each do |i|
  75.       x_des = x + Math.cos(Math.radian(degree)) * (length*(i.to_f/params.max))
  76.       y_des = y + Math.sin(Math.radian(degree)) * (length*(i.to_f/params.max))
  77.       draw_line(x,y,x_des,y_des,color)
  78.       degree += degree_minus
  79.       coordinate.push(Coordinate.new(x_des,y_des))
  80.     end
  81.     for i in -1..coordinate.size-2
  82.       c = coordinate
  83.       draw_line(c[i].x,c[i].y,c[i+1].x,c[i+1].y)
  84.     end
  85.   end
  86.  
  87. end
  88.  
  89. class Coordinate
  90.   attr_accessor :x,:y
  91.  
  92.   def initialize(x,y)
  93.     @x = x
  94.     @y = y
  95.   end
  96.  
  97. end
  98.  
  99. module Math
  100.  
  101.   def self.radian(degree)
  102.     return (degree.to_f/180) * Math::PI
  103.   end
  104.  
  105.   def self.degree(radian)
  106.     return (radian.to_f/Math::PI) * 180
  107.   end
  108.  
  109. end
  110.  
  111. class Ellipse
  112.   attr_accessor :x, :y, :horz, :vert,
  113. end
  114.  
  115. def updates
  116.   basic_update
  117. end
  118.  
  119. $sprite = Sprite.new
  120. $sprite.bitmap = Bitmap.new(Graphics.width,Graphics.height)
  121. #$sprite.bitmap.draw_elipse(200,200,100,100,Color.new(255,255,255),1)
  122. params = []
  123. params += [100,50]*2
  124. $sprite.bitmap.draw_shape_params(100,150,params,50)
  125. params = []
  126. params += [100,50]*4
  127. $sprite.bitmap.draw_shape_params(200,150,params,50)
  128. params = []
  129. params += [100,50]*6
  130. $sprite.bitmap.draw_shape_params(300,150,params,50)
  131. params = []
  132. params += [100]*3
  133. $sprite.bitmap.draw_shape_params(100,300,params,50)
  134. params = []
  135. params += [100]*5
  136. $sprite.bitmap.draw_shape_params(200,300,params,50)
  137. params = []
  138. params += [100]*7
  139. $sprite.bitmap.draw_shape_params(300,300,params,50)
  140. #$sprite.bitmap.draw_line(100,100,20,20)
  141. updates while true
Advertisement
Add Comment
Please, Sign In to add comment