设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 1800|回复: 3
打印 上一主题 下一主题

[已经过期] 绘制斜线的脚本?

 关闭 [复制链接]

Lv2.观梦者

花开堪折直须折

梦石
0
星屑
636
在线时间
943 小时
注册时间
2010-7-17
帖子
4963

贵宾

跳转到指定楼层
1
发表于 2011-6-29 11:30:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
绘制直线的是这样的
self.contents.fill_rect(x坐标, y坐标, 宽度, 高度, 颜色)
那怎么绘制斜线啊?必须得一个点一个点的描绘吗。。没速度。
先谢过各位大大了!
大家好,我叫节操,有一天,我被吃了。
http://forever-dream.5d6d.com
永恒の梦制作组论坛

129993099
永恒の梦制作组QQ群

Lv1.梦旅人

梦石
0
星屑
50
在线时间
1281 小时
注册时间
2006-8-27
帖子
590
2
发表于 2011-6-29 19:03:04 | 只看该作者
http://rpg.blue/htm/Topic_30254.htm

13楼 平面図形描画
回复

使用道具 举报

Lv2.观梦者

花开堪折直须折

梦石
0
星屑
636
在线时间
943 小时
注册时间
2010-7-17
帖子
4963

贵宾

3
 楼主| 发表于 2011-6-29 21:05:30 | 只看该作者
wbsy8241 发表于 2011-6-29 19:03
http://rpg.blue/htm/Topic_30254.htm

13楼 平面図形描画

此网站打开不能- -
大家好,我叫节操,有一天,我被吃了。
http://forever-dream.5d6d.com
永恒の梦制作组论坛

129993099
永恒の梦制作组QQ群
回复

使用道具 举报

Lv1.梦旅人

梦石
0
星屑
50
在线时间
1281 小时
注册时间
2006-8-27
帖子
590
4
发表于 2011-6-29 21:09:08 | 只看该作者
冰舞蝶恋 发表于 2011-6-29 21:05
此网站打开不能- -

我怎么能打开啊= =
  1. #_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
  2. #_/  ◆平面図形描画 - KGC_Drawing 2D◆
  3. #_/----------------------------------------------------------------------------
  4. #_/ 各種平面図形の描画機能を追加します。
  5. #_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/


  6. #★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★

  7. #==============================================================================
  8. # ■ Bitmap
  9. #==============================================================================

  10. class Bitmap
  11. #--------------------------------------------------------------------------
  12. # ● 直線描画
  13. #--------------------------------------------------------------------------
  14. def draw_line(*args)
  15.    # 引数処理
  16.    x, y, dx, dy, color, size = _line_args(args, 0)
  17.    return unless x
  18.    # 描画方向判定
  19.    mx = x < dx ? 1 : -1
  20.    my = y < dy ? 1 : -1
  21.    # 座標差分計算
  22.    dfx, dfy = (dx - x).abs.to_i, (dy - y).abs.to_i
  23.    # 誤差計算
  24.    diff = [dfx, dfy].max / 2
  25.    # 直線描画
  26.    if dfx >= dfy
  27.      (dfx + 1).times { |i|
  28.        if size == 1
  29.          self.set_pixel(x, y, color)
  30.        else
  31.          self.fill_rect(x, y, size, size, color)
  32.        end
  33.        x += mx
  34.        diff += dfy
  35.        if diff >= dfx
  36.          diff -= dfx
  37.          y += my
  38.        end
  39.      }
  40.    else
  41.      (dfy + 1).times { |i|
  42.        if size == 1
  43.          self.set_pixel(x, y, color)
  44.        else
  45.          self.fill_rect(x, y, size, size, color)
  46.        end
  47.        y += my
  48.        diff += dfx
  49.        if diff >= dfy
  50.          diff -= dfy
  51.          x += mx
  52.        end
  53.      }
  54.    end
  55. end
  56. #--------------------------------------------------------------------------
  57. # ● グラデーション直線描画
  58. #--------------------------------------------------------------------------
  59. def draw_grad_line(*args)
  60.    # 引数処理
  61.    x, y, dx, dy, color, size, end_color, end_size = _line_args(args, 1)
  62.    return unless x
  63.    # 描画方向判定
  64.    mx = x < dx ? 1 : -1
  65.    my = y < dy ? 1 : -1
  66.    # 座標差分計算
  67.    dfx, dfy = (dx - x).abs.to_i, (dy - y).abs.to_i
  68.    # 誤差計算
  69.    diff = [dfx, dfy].max
  70.    # 色?サイズ差分計算
  71.    dcr = (end_color.red - color.red) / (diff - 1)
  72.    dcg = (end_color.green - color.green) / (diff - 1)
  73.    dcb = (end_color.blue - color.blue) / (diff - 1)
  74.    dca = (end_color.alpha - color.alpha) / (diff - 1)
  75.    ds = (end_size.to_f - size.to_f) / (diff - 1)
  76.    # 誤差修正
  77.    diff /= 2
  78.    # 直線描画
  79.    if dfx >= dfy
  80.      (dfx + 1).times { |i|
  81.        if size == 1
  82.          self.set_pixel(x, y, color)
  83.        else
  84.          self.fill_rect(x, y, size, size, color)
  85.        end
  86.        x += mx
  87.        diff += dfy
  88.        if diff >= dfx
  89.          diff -= dfx
  90.          y += my
  91.        end
  92.        color.red += dcr
  93.        color.green += dcg
  94.        color.blue += dcb
  95.        color.alpha += dca
  96.        size += ds
  97.      }
  98.    else
  99.      (dfy + 1).times { |i|
  100.        if size == 1
  101.          self.set_pixel(x, y, color)
  102.        else
  103.          self.fill_rect(x, y, size, size, color)
  104.        end
  105.        y += my
  106.        diff += dfx
  107.        if diff >= dfy
  108.          diff -= dfy
  109.          x += mx
  110.        end
  111.        color.red += dcr
  112.        color.green += dcg
  113.        color.blue += dcb
  114.        color.alpha += dca
  115.        size += ds
  116.      }
  117.    end
  118. end
  119. #--------------------------------------------------------------------------
  120. # ◆ 直線描画用引数処理
  121. #--------------------------------------------------------------------------
  122. def _line_args(args, type)
  123.    if args[0].is_a?(Point)
  124.      argc = (type == 0 ? 3 : 4)
  125.      if args.size == argc
  126.        x, y = args[0].x, args[0].y
  127.        dx, dy = args[1].x, args[1].y
  128.        if args[2].is_a?(Pen)
  129.          color,  size = args[2].color.dup, args[2].size
  130.          end_color, end_size = args[3].color.dup, args[3].size if type == 1
  131.        else
  132.          color, size = args[2], 1
  133.          end_color, end_size = args[3], 1 if type == 1
  134.        end
  135.      else
  136.        raise(ArgumentError, "wrong number of arguments(#{args.size} of #{argc})")
  137.        return [nil]
  138.      end
  139.    else
  140.      argc = (type == 0 ? 5 : 6)
  141.      if args.size == argc
  142.        x, y, dx, dy = args
  143.        if args[4].is_a?(Pen)
  144.          color, size = args[4].color, args[4].size
  145.          end_color, end_size = args[5].color, args[5].size if type == 1
  146.        else
  147.          color, size = args[4], 1
  148.          end_color, end_size = args[5], 1 if type == 1
  149.        end
  150.      else
  151.        raise(ArgumentError, "wrong number of arguments(#{args.size} of #{argc}")
  152.        return [nil]
  153.      end
  154.    end
  155.    return [x, y, dx, dy, color, size, end_color, end_size]
  156. end
  157. protected :_line_args
  158. #--------------------------------------------------------------------------
  159. # ● 矩形描画
  160. #--------------------------------------------------------------------------
  161. def draw_rect(*args)
  162.    # 引数処理
  163.    x, y, w, h, color, size = _rect_args(args)
  164.    return unless x
  165.    pen = Pen.new(color, size)
  166.    draw_line(x, y, x + w, y, pen)
  167.    draw_line(x + w, y, x + w, y + h, pen)
  168.    draw_line(x + w, y + h, x, y + h, pen)
  169.    draw_line(x, y + h, x, y, pen)
  170. end
  171. #--------------------------------------------------------------------------
  172. # ◆ 矩形描画用引数処理
  173. #--------------------------------------------------------------------------
  174. def _rect_args(args)
  175.    if args[0].is_a?(Rect)
  176.      if args.size == 2
  177.        x, y, w, h = args[0].x, args[0].y, args[0].width, args[0].height
  178.        if args[1].is_a?(Pen)
  179.          color,  size = args[1].color.dup, args[1].size
  180.        else
  181.          color, size = args[1], 1
  182.        end
  183.      else
  184.        raise(ArgumentError, "wrong number of arguments(#{args.size} of 2)")
  185.        return [nil]
  186.      end
  187.    else
  188.      if args.size == 5
  189.        x, y, w, h = args
  190.        if args[4].is_a?(Pen)
  191.          color, size = args[4].color, args[4].size
  192.        else
  193.          color, size = args[4], 1
  194.        end
  195.      else
  196.        raise(ArgumentError, "wrong number of arguments(#{args.size} of 5)")
  197.        return [nil]
  198.      end
  199.    end
  200.    return [x, y, w, h, color, size]
  201. end
  202. protected :_rect_args
  203. #--------------------------------------------------------------------------
  204. # ● 折れ線描画
  205. #--------------------------------------------------------------------------
  206. def draw_connect_line(*args)
  207.    # 引数処理
  208.    points = _connect_line_args(args)
  209.    pen = points.shift
  210.    (0...(points.size - 1)).each { |i|
  211.      draw_line(points[i], points[i + 1], pen)
  212.    }
  213. end
  214. #--------------------------------------------------------------------------
  215. # ◆ 折れ線描画用引数処理
  216. #--------------------------------------------------------------------------
  217. def _connect_line_args(args)
  218.    if args[0].is_a?(Color)
  219.      args.insert(0, Pen.new(args[0], 1))
  220.      args.delete_at(1)
  221.    end
  222.    return args
  223. end
  224. protected :_connect_line_args
  225. #--------------------------------------------------------------------------
  226. # ● 多角形描画
  227. #--------------------------------------------------------------------------
  228. def draw_polygon(*args)
  229.    # 引数処理
  230.    cx, cy, r, vertex, color, size = _polygon_args(args)
  231.    return unless cx
  232.    # 漸化式を初期化
  233.    x, y = 0, -r
  234.    px, py = x + cx, y + cy
  235.    dfx = dfy = 0
  236.    pr = Integer([100, r].max ** 1.6)
  237.    a = 6.3 / pr
  238.    cnt = pr / vertex
  239.    # 多角形を描画
  240.    pen = Pen.new(color, size)
  241.    vertex.times { |i|
  242.      cnt.times { |j|
  243.        dfx, dfy = a * y, a * x
  244.        x, y = x + dfx, y - dfy
  245.      }
  246.      dx, dy = x + cx, y + cy
  247.      self.draw_line(px, py, dx, dy, pen)
  248.      px, py = dx, dy
  249.    }
  250. end
  251. #--------------------------------------------------------------------------
  252. # ◆ 多角形描画用引数処理
  253. #--------------------------------------------------------------------------
  254. def _polygon_args(args)
  255.    if args[0].is_a?(Point)
  256.      if args.size == 4
  257.        cx, cy = args[0].x, args[0].y
  258.        r, vertex = args[1, 2]
  259.        if args[3].is_a?(Pen)
  260.          color, size = args[3].color, args[3].size
  261.        else
  262.          color, size = args[3], 1
  263.        end
  264.      else
  265.        raise(ArgumentError, "wrong number of arguments(#{args.size} of 4)")
  266.        return [nil]
  267.      end
  268.    else
  269.      if args.size == 5
  270.        cx, cy, r, vertex = args
  271.        if args[4].is_a?(Pen)
  272.          color, size = args[4].color, args[4].size
  273.        else
  274.          color, size = args[4], 1
  275.        end
  276.      else
  277.        raise(ArgumentError, "wrong number of arguments(#{args.size} of 5)")
  278.        return [nil]
  279.      end
  280.    end
  281.    return [cx, cy, r, vertex, color, size]
  282. end
  283. protected :_polygon_args
  284. #--------------------------------------------------------------------------
  285. # ● 円弧描画
  286. #--------------------------------------------------------------------------
  287. def draw_arc(*args)
  288.    # 引数処理
  289.    cx, cy, r, st_ang, end_ang, color, size = _arc_args(args)
  290.    return unless cx
  291.    # 漸化式を初期化
  292.    x = r * Math.cos(Math::PI * st_ang / 180.0)
  293.    y = r * -Math.sin(Math::PI * st_ang / 180.0)
  294.    dfx = dfy = 0
  295.    pr = Integer([100, r].max ** 1.6)
  296.    a = 6.3 / pr
  297.    pr = pr * (end_ang - st_ang) / 360
  298.    # 円弧を描画
  299.    pr.times { |i|
  300.      x, y = x + dfx, y - dfy
  301.      dx, dy = x + cx, y + cy
  302.      if size == 1
  303.        self.set_pixel(dx, dy, color)
  304.      else
  305.        self.fill_rect(dx, dy, size, size, color)
  306.      end
  307.      dfx, dfy = a * y, a * x
  308.    }
  309. end
  310. #--------------------------------------------------------------------------
  311. # ◆ 円弧描画用引数処理
  312. #--------------------------------------------------------------------------
  313. def _arc_args(args)
  314.    if args[0].is_a?(Point)
  315.      if args.size == 5
  316.        cx, cy = args[0].x, args[0].y
  317.        r, st_ang, end_ang = args[1, 3]
  318.        if args[4].is_a?(Pen)
  319.          color, size = args[4].color, args[4].size
  320.        else
  321.          color, size = args[4], 1
  322.        end
  323.      else
  324.        raise(ArgumentError, "wrong number of arguments(#{args.size} of 5)")
  325.        return [nil]
  326.      end
  327.    else
  328.      if args.size == 6
  329.        cx, cy, r, st_ang, end_ang = args
  330.        if args[5].is_a?(Pen)
  331.          color, size = args[5].color, args[5].size
  332.        else
  333.          color, size = args[5], 1
  334.        end
  335.      else
  336.        raise(ArgumentError, "wrong number of arguments(#{args.size} of 6)")
  337.        return [nil]
  338.      end
  339.    end
  340.    return [cx, cy, r, st_ang, end_ang, color, size]
  341. end
  342. protected :_arc_args
  343. #--------------------------------------------------------------------------
  344. # ● 円描画
  345. #--------------------------------------------------------------------------
  346. def draw_circle(*args)
  347.    # 引数処理
  348.    cx, cy, r, st_ang, end_ang, color, size = _circle_args(args)
  349.    return unless cx
  350.    draw_arc(cx, cy, r, 0, 360, Pen.new(color, size))
  351. end
  352. #--------------------------------------------------------------------------
  353. # ◆ 円描画用引数処理
  354. #--------------------------------------------------------------------------
  355. def _circle_args(args)
  356.    if args[0].is_a?(Point)
  357.      args.insert(2, [0, 360])
  358.    else
  359.      args.insert(3, [0, 360])
  360.    end
  361.    return _arc_args(args.flatten)
  362. end
  363. protected :_circle_args
  364. #--------------------------------------------------------------------------
  365. # ● 扇形描画
  366. #--------------------------------------------------------------------------
  367. def draw_pie(*args)
  368.    # 引数処理
  369.    cx, cy, r, st_ang, end_ang, color, size = _arc_args(args)
  370.    return unless cx
  371.    pen = Pen.new(color, size)
  372.    pt1 = Point.new(cx, cy)
  373.    pt2 = Point.new(cx + r * Math.cos(Math::PI * st_ang / 180.0),
  374.      cy - r * Math.sin(Math::PI * st_ang / 180.0))
  375.    draw_line(pt1, pt2, pen)
  376.    pt2.x = cx + r * Math.cos(Math::PI * end_ang / 180.0)
  377.    pt2.y = cy - r * Math.sin(Math::PI * end_ang / 180.0)
  378.    draw_line(pt1, pt2, pen)
  379.    draw_arc(cx, cy, r, st_ang, end_ang, pen)
  380. end
  381. #--------------------------------------------------------------------------
  382. # ● 楕円描画
  383. #--------------------------------------------------------------------------
  384. def draw_ellipse(*args)
  385.    # 引数処理
  386.    cx, cy, w, h, color, size = _ellipse_args(args)
  387.    return unless cx
  388.    # 漸化式を初期化
  389.    x, y = w, 0
  390.    dfx = dfy = 0
  391.    pr = Integer([100, [w, h].max].max ** 1.6)
  392.    a = 6.3 / pr
  393.    # 円弧を描画
  394.    pr.times { |i|
  395.      x, y = x + dfx, y - dfy
  396.      dx, dy = x + cx, y * h / w + cy
  397.      if size == 1
  398.        self.set_pixel(dx, dy, color)
  399.      else
  400.        self.fill_rect(dx, dy, size, size, color)
  401.      end
  402.      dfx, dfy = a * y, a * x
  403.    }
  404. end
  405. #--------------------------------------------------------------------------
  406. # ◆ 楕円描画用引数処理
  407. #--------------------------------------------------------------------------
  408. def draw_ellipse(*args)
  409.    # 引数処理
  410.    cx, cy, w, h, color, size = _ellipse_args(args)
  411.    return unless cx
  412.    # 漸化式を初期化
  413.    x, y = w, 0
  414.    dfx = dfy = 0
  415.    pr = Integer([100, [w, h].max].max ** 1.6)
  416.    a = 6.3 / pr
  417.    # 円弧を描画
  418.    pr.times { |i|
  419.      x, y = x + dfx, y - dfy
  420.      dx, dy = x + cx, y * h / w + cy
  421.      if size == 1
  422.        self.set_pixel(dx, dy, color)
  423.      else
  424.        self.fill_rect(dx, dy, size, size, color)
  425.      end
  426.      dfx, dfy = a * y, a * x
  427.    }
  428. end
  429. #--------------------------------------------------------------------------
  430. # ◆ 楕円描画用引数処理
  431. #--------------------------------------------------------------------------
  432. def _ellipse_args(args)
  433.    if args[0].is_a?(Point)
  434.      if args.size == 4
  435.        cx, cy = args[0].x, args[0].y
  436.        w, h = args[1, 2]
  437.        if args[3].is_a?(Pen)
  438.          color, size = args[3].color, args[3].size
  439.        else
  440.          color, size = args[3], 1
  441.        end
  442.      else
  443.        raise(ArgumentError, "wrong number of arguments(#{args.size} of 4)")
  444.        return [nil]
  445.      end
  446.    else
  447.      if args.size == 5
  448.        cx, cy, w, h = args
  449.        if args[4].is_a?(Pen)
  450.          color, size = args[4].color, args[4].size
  451.        else
  452.          color, size = args[4], 1
  453.        end
  454.      else
  455.        raise(ArgumentError, "wrong number of arguments(#{args.size} of 5)")
  456.        return [nil]
  457.      end
  458.    end
  459.    return [cx, cy, w, h, color, size]
  460. end
  461. protected :_ellipse_args
  462. end

  463. #★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★

  464. #==============================================================================
  465. # ■ Struct
  466. #==============================================================================

  467. # ■ Point 構造体
  468. Point = Struct.new("Point", :x, :y)

  469. #★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★

  470. #==============================================================================
  471. # ■ Pen
  472. #==============================================================================

  473. class Pen
  474. attr_accessor :color, :size
  475. def initialize(color, size = 1)
  476.    @color = color.dup
  477.    @size = [size, 1].max
  478. end
  479. end
复制代码

点评

就算是画水平线也是1格1格画的  发表于 2011-6-29 23:08
哎……说白了还是要一个像素一个像素的画……  发表于 2011-6-29 22:59
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

//
拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-5-16 11:20

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表