|
|
|
|
几何图形绘制脚本 v0.8 ( 66RPG, RPG MAKER XP教程 )
本站首页→制作教程→RMVX 高级教程 |
|
几何图形绘制脚本 v0.8
教程作者:snstar2006
首发网址:点此进入本教学的原始帖
适宜用户:RGSS研究者 技术通用度:★★★ 技术应用复杂度:80 (满分150分) 学习的理解难度:80 (满分150分)
|
作者的话: 最近在研究精灵,刚好八云紫妹妹又在问怎麽画三角形 於是就写出来了,顺便写了其他几个形状。 原本要做出绘制正多边行的,但是公式方面还有问题 等解决了再发出。 使用了Deathless 的线条绘制脚本
教学正文:
==================================== 使用说明 ==================================== 三角形绘制: XXX = Triangle.new(第一点, 第二点, 第三点,[线粗, 颜色]) 正三角形绘制: XXX = Triangle_E.new(原点, 边长, [线粗, 颜色]) 等腰三角形绘制:XXX = Triangle_I.new(原点, 底长, [边长, 线粗, 颜色]) 四边形绘制: XXX = Quadri.new(第一点, 第二点, 第三点, 第四点, [线粗, 颜色]) 长方形绘制: XXX = Rectangle.new(原点, 宽度, 长度,[线粗, 颜色]) 菱形绘制: XXX = Rhombus.new(原点, 宽度, 长度,[线粗, 颜色]) 正方形绘制: XXX = Square.new(原点, 长度,[线粗, 颜色]) 上面在[]中的参数为选填的,默认 线粗为1, 颜色白 等腰三角形的边长默认为底长的2/3倍
任意多边行: XXX = Polygon.new([第一点, 第二点, 第三点, ...], {线粗, 颜色}) 上面在{}中的参数为选填的,默认 线粗为1, 颜色白
点的格式为: [X座标, Y座标] 绘制任意多边型时,必须提供两点以上的参数,否则直接无视
其他函数: .name 返回名称 .area 返回面积(目前不支援四边形和多边型) .print_name(x, y) 显示名称座标 .length(p1, p2) 计算p1到p2两点间距离
顺便附赠一个网格脚本 使用 XXX = Grid.new([横距, 纵距]) 横距和纵距不提供的话默认为32
class Polygon < Sprite_Base attr_accessor :p1 attr_accessor :p2 attr_accessor :p3 attr_accessor :name attr_accessor :po #attr_accessor :print
def initialize(points, thick=1, color=normal_color) super(Viewport.new(0, 0, 544, 416)) self.bitmap = Bitmap.new(544, 416) @p1=[] @p2=[] @p3=[] @thick = thick @color = color @points = points @name = "Polygon" draw end def set_viewport(x, y, width, height) self.viewport = Viewport.new(x, y, width, height) end def update end def draw self.bitmap.clear for i in 0...@points.size if @points.size > 2 if i < (@points.size) - 1 p1 = @points[i] p2 = @points[i+1] else p1 = @points[i] p2 = @points[0] end self.bitmap.drawline(p1[0], p1[1], p2[0], p2[1], @thick, @color) end end end def area return 0 end def print_name(x, y) #self.bitmap.font.color.alpha = print ? 255 : 0 self.bitmap.draw_text(x, y, bitmap.text_size(@name).width, 24, name) end def length(p1, p2) a = (p2[0]-p1[0]).abs b = (p2[1]-p1[1]).abs return Math.hypot(a, b) end end class Triangle < Polygon def initialize(p1, p2, p3, thick=1, color=normal_color) @p1 = p1 @p2 = p2 @p3 = p3 super([p1, p2, p3],thick, color) @name = "Triangle" end def area a = length(@p1, @p2) b = length(@p2, @p3) c = length(@p3, @p1) d = (a+b+c)/2 area = Math.sqrt( d*(d-a)*(d-b)*(d-c) ) return area end end class Triangle_E < Triangle def initialize(p1, side, thick=1, color=normal_color) height = (side/2) * Math.sqrt(3) @p1 = [] @p1[0] = p1[0]+side/2 @p1[1] = p1[1] @p2=[] @p2[0]=p1[0] @p2[1]=p1[1]+height @p3=[] @p3[0]=p1[0]+side @p3[1]=p1[1]+height super(@p1, @p2, @p3, thick, color) end end class Triangle_I < Triangle def initialize(p1, base, side=base*2/3, thick=1, color=normal_color) base_h = base/2 height = Math.sqrt( side**2 - base_h**2 ) @p1 = [] @p1[0] = p1[0]+base/2 @p1[1] = p1[1] @p2=[] @p2[0]=p1[0] @p2[1]=p1[1]+height @p3=[] @p3[0]=p1[0]+base @p3[1]=p1[1]+height super(@p1, @p2, @p3, thick, color) end end class Quadri < Polygon attr_accessor :p4 def initialize(p1, p2, p3, p4, thick=1, color=normal_color) super([p1, p2, p3, p4], thick, color) @p1 = p1 @p2 = p2 @p3 = p3 @p4 = p4 @name = "Quadrilateral" draw end end class Rectangle < Quadri def initialize(p1, width, length, thick=1, color=normal_color) @p1 = p1
@p2=[] @p2[0] = p1[0] @p2[1] = p1[1] + width @p4 = [] @p4[0] = p1[0] + length @p4[1] = p1[1] @p3=[] @p3[0] = @p4[0] @p3[1] = @p2[1] @width = width @length = length super(@p1, @p2, @p3, @p4, thick, color) @name = "Rectangle" end def area return @length * @width end end class Rhombus < Quadri def initialize(p1, width, height, thick=1, color=normal_color) @p1 = [] @p1[0] = p1[0] + width/2 @p1[1] = p1[1]
@p2 = [] @p2[0] = p1[0] + width @p2[1] = p1[1] + height/2 @p3 = [] @p3[0] = p1[0] + width/2 @p3[1] = p1[1] + height @p4 = [] @p4[0] = p1[0] @p4[1] = p1[1] + height/2
super(@p1, @p2, @p3, @p4, thick, color) @name = "Rhombus" end def area return @width * @height / 2 end end class Square < Rectangle def initialize(p1, side, thick=1, color=normal_color) super(p1, side, side, thick, color) @name = "Square" end end
class Grid < Sprite_Base def initialize(x=32, y=32) super(Viewport.new(0, 0, 544, 416)) self.bitmap = Bitmap.new(544, 416) color = Color.new(255, 255, 255, 255) for i in 1...(544/x) dx = x*i self.bitmap.drawline(dx, 0, dx, 416, 1, color) end for i in 1...(416/y) dy = y*i self.bitmap.drawline(0, dy, 544, dy, 1, color) end end end
class Polygon < Sprite_Base #-------------------------------------------------------------------------- # ● 獲取文字顏色 # n : 文字顏色色號(0-31) #-------------------------------------------------------------------------- def text_color(n) windowskin = Cache.system("Window") x = 64 + (n % 8) * 8 y = 96 + (n / 8) * 8 return windowskin.get_pixel(x, y) end #-------------------------------------------------------------------------- # ● 獲取一般文字顏色 #-------------------------------------------------------------------------- def normal_color return text_color(0) end #-------------------------------------------------------------------------- # ● 獲取系統文字顏色 #-------------------------------------------------------------------------- def system_color return text_color(16) end #-------------------------------------------------------------------------- # ● 獲取危機文字顏色 #-------------------------------------------------------------------------- def crisis_color return text_color(17) end #-------------------------------------------------------------------------- # ● 獲取戰鬥不能文字顏色 #-------------------------------------------------------------------------- def knockout_color return text_color(18) end end
# 线条绘制脚本 作者 Deathless class Bitmap #-------------------------------------------------------------------------- # ● 描绘直线 # x1,y1,x2,y2: 直线两端的坐标 # width: 宽度 # color: 颜色 #-------------------------------------------------------------------------- def drawline(x1, y1, x2, y2, width, color) x1 = x1.to_f y1 = y1.to_f x2 = x2.to_f y2 = y2.to_f width = width.to_f k = (y2 - y1) / (x2 - x1) if k.abs > 1 drawline_x(x1, y1, x2, y2, width, color) else drawline_y(x1, y1, x2, y2, width, color) end end def drawline_x(x1, y1, x2, y2, width, color) l = ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5 * width / (y1 - y2) length = l.abs * 2 k = (x2 - x1) / (y2 - y1) #x=ky+b b = x1 - k * y1 if l > 0 for ty in y2.to_i..y1.to_i tx = ty * k + b fill_rect(tx - l, ty, length, 1, color) end else for ty in y1.to_i..y2.to_i tx = ty * k + b fill_rect(tx + l, ty, length, 1, color) end end end def drawline_y(x1, y1, x2, y2, width, color) l = ((x1 - x2) ** 2 + (y1 - y2) ** 2) ** 0.5 * width / (x1 - x2) height = l.abs * 2 k = (y2 - y1) / (x2 - x1) #y=kx+b b = y1 - k * x1 if l > 0 for tx in x2.to_i..x1.to_i ty = tx * k + b fill_rect(tx, ty - l, 1, height, color) end else for tx in x1.to_i..x2.to_i ty = tx * k + b fill_rect(tx, ty + l, 1, height, color) end end end end 效果图:  使用范例: 在事件中调用脚本:$scene = Scene_Try.new即可
class Scene_Try < Scene_Base def start @triangle = Triangle.new([10,40],[40, 40],[10, 100]) @i_triangle = Triangle_I.new([60, 10], 60) @e_triangle = Triangle_E.new([110, 20], 60) @triangle.print_name(100, 100) @quadri = Quadri.new([10, 230], [20, 230], [70, 250], [10, 250]) @rectangle = Rectangle.new([80, 200], 100, 50) @rhombus = Rhombus.new([10, 300], 100, 50) @square = Square.new([150, 300], 30) @quadri.print_name(175, 250) end end
关键字:RMVX RGSS RUBY
发布日期:2008-8-8 0:34:57 点击量:1
上一篇:显示头像的战斗信息框 下一篇:没有下一条记录
|
|