赞 | 0 |
VIP | 13 |
好人卡 | 65 |
积分 | 1 |
经验 | 58644 |
最后登录 | 2017-10-23 |
在线时间 | 1281 小时 |
Lv1.梦旅人
- 梦石
- 0
- 星屑
- 50
- 在线时间
- 1281 小时
- 注册时间
- 2006-8-27
- 帖子
- 590
|
冰舞蝶恋 发表于 2011-6-29 21:05
此网站打开不能- -
我怎么能打开啊= =- #_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
- #_/ ◆平面図形描画 - KGC_Drawing 2D◆
- #_/----------------------------------------------------------------------------
- #_/ 各種平面図形の描画機能を追加します。
- #_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
- #★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★
- #==============================================================================
- # ■ Bitmap
- #==============================================================================
- class Bitmap
- #--------------------------------------------------------------------------
- # ● 直線描画
- #--------------------------------------------------------------------------
- def draw_line(*args)
- # 引数処理
- x, y, dx, dy, color, size = _line_args(args, 0)
- return unless x
- # 描画方向判定
- mx = x < dx ? 1 : -1
- my = y < dy ? 1 : -1
- # 座標差分計算
- dfx, dfy = (dx - x).abs.to_i, (dy - y).abs.to_i
- # 誤差計算
- diff = [dfx, dfy].max / 2
- # 直線描画
- if dfx >= dfy
- (dfx + 1).times { |i|
- if size == 1
- self.set_pixel(x, y, color)
- else
- self.fill_rect(x, y, size, size, color)
- end
- x += mx
- diff += dfy
- if diff >= dfx
- diff -= dfx
- y += my
- end
- }
- else
- (dfy + 1).times { |i|
- if size == 1
- self.set_pixel(x, y, color)
- else
- self.fill_rect(x, y, size, size, color)
- end
- y += my
- diff += dfx
- if diff >= dfy
- diff -= dfy
- x += mx
- end
- }
- end
- end
- #--------------------------------------------------------------------------
- # ● グラデーション直線描画
- #--------------------------------------------------------------------------
- def draw_grad_line(*args)
- # 引数処理
- x, y, dx, dy, color, size, end_color, end_size = _line_args(args, 1)
- return unless x
- # 描画方向判定
- mx = x < dx ? 1 : -1
- my = y < dy ? 1 : -1
- # 座標差分計算
- dfx, dfy = (dx - x).abs.to_i, (dy - y).abs.to_i
- # 誤差計算
- diff = [dfx, dfy].max
- # 色?サイズ差分計算
- dcr = (end_color.red - color.red) / (diff - 1)
- dcg = (end_color.green - color.green) / (diff - 1)
- dcb = (end_color.blue - color.blue) / (diff - 1)
- dca = (end_color.alpha - color.alpha) / (diff - 1)
- ds = (end_size.to_f - size.to_f) / (diff - 1)
- # 誤差修正
- diff /= 2
- # 直線描画
- if dfx >= dfy
- (dfx + 1).times { |i|
- if size == 1
- self.set_pixel(x, y, color)
- else
- self.fill_rect(x, y, size, size, color)
- end
- x += mx
- diff += dfy
- if diff >= dfx
- diff -= dfx
- y += my
- end
- color.red += dcr
- color.green += dcg
- color.blue += dcb
- color.alpha += dca
- size += ds
- }
- else
- (dfy + 1).times { |i|
- if size == 1
- self.set_pixel(x, y, color)
- else
- self.fill_rect(x, y, size, size, color)
- end
- y += my
- diff += dfx
- if diff >= dfy
- diff -= dfy
- x += mx
- end
- color.red += dcr
- color.green += dcg
- color.blue += dcb
- color.alpha += dca
- size += ds
- }
- end
- end
- #--------------------------------------------------------------------------
- # ◆ 直線描画用引数処理
- #--------------------------------------------------------------------------
- def _line_args(args, type)
- if args[0].is_a?(Point)
- argc = (type == 0 ? 3 : 4)
- if args.size == argc
- x, y = args[0].x, args[0].y
- dx, dy = args[1].x, args[1].y
- if args[2].is_a?(Pen)
- color, size = args[2].color.dup, args[2].size
- end_color, end_size = args[3].color.dup, args[3].size if type == 1
- else
- color, size = args[2], 1
- end_color, end_size = args[3], 1 if type == 1
- end
- else
- raise(ArgumentError, "wrong number of arguments(#{args.size} of #{argc})")
- return [nil]
- end
- else
- argc = (type == 0 ? 5 : 6)
- if args.size == argc
- x, y, dx, dy = args
- if args[4].is_a?(Pen)
- color, size = args[4].color, args[4].size
- end_color, end_size = args[5].color, args[5].size if type == 1
- else
- color, size = args[4], 1
- end_color, end_size = args[5], 1 if type == 1
- end
- else
- raise(ArgumentError, "wrong number of arguments(#{args.size} of #{argc}")
- return [nil]
- end
- end
- return [x, y, dx, dy, color, size, end_color, end_size]
- end
- protected :_line_args
- #--------------------------------------------------------------------------
- # ● 矩形描画
- #--------------------------------------------------------------------------
- def draw_rect(*args)
- # 引数処理
- x, y, w, h, color, size = _rect_args(args)
- return unless x
- pen = Pen.new(color, size)
- draw_line(x, y, x + w, y, pen)
- draw_line(x + w, y, x + w, y + h, pen)
- draw_line(x + w, y + h, x, y + h, pen)
- draw_line(x, y + h, x, y, pen)
- end
- #--------------------------------------------------------------------------
- # ◆ 矩形描画用引数処理
- #--------------------------------------------------------------------------
- def _rect_args(args)
- if args[0].is_a?(Rect)
- if args.size == 2
- x, y, w, h = args[0].x, args[0].y, args[0].width, args[0].height
- if args[1].is_a?(Pen)
- color, size = args[1].color.dup, args[1].size
- else
- color, size = args[1], 1
- end
- else
- raise(ArgumentError, "wrong number of arguments(#{args.size} of 2)")
- return [nil]
- end
- else
- if args.size == 5
- x, y, w, h = args
- if args[4].is_a?(Pen)
- color, size = args[4].color, args[4].size
- else
- color, size = args[4], 1
- end
- else
- raise(ArgumentError, "wrong number of arguments(#{args.size} of 5)")
- return [nil]
- end
- end
- return [x, y, w, h, color, size]
- end
- protected :_rect_args
- #--------------------------------------------------------------------------
- # ● 折れ線描画
- #--------------------------------------------------------------------------
- def draw_connect_line(*args)
- # 引数処理
- points = _connect_line_args(args)
- pen = points.shift
- (0...(points.size - 1)).each { |i|
- draw_line(points[i], points[i + 1], pen)
- }
- end
- #--------------------------------------------------------------------------
- # ◆ 折れ線描画用引数処理
- #--------------------------------------------------------------------------
- def _connect_line_args(args)
- if args[0].is_a?(Color)
- args.insert(0, Pen.new(args[0], 1))
- args.delete_at(1)
- end
- return args
- end
- protected :_connect_line_args
- #--------------------------------------------------------------------------
- # ● 多角形描画
- #--------------------------------------------------------------------------
- def draw_polygon(*args)
- # 引数処理
- cx, cy, r, vertex, color, size = _polygon_args(args)
- return unless cx
- # 漸化式を初期化
- x, y = 0, -r
- px, py = x + cx, y + cy
- dfx = dfy = 0
- pr = Integer([100, r].max ** 1.6)
- a = 6.3 / pr
- cnt = pr / vertex
- # 多角形を描画
- pen = Pen.new(color, size)
- vertex.times { |i|
- cnt.times { |j|
- dfx, dfy = a * y, a * x
- x, y = x + dfx, y - dfy
- }
- dx, dy = x + cx, y + cy
- self.draw_line(px, py, dx, dy, pen)
- px, py = dx, dy
- }
- end
- #--------------------------------------------------------------------------
- # ◆ 多角形描画用引数処理
- #--------------------------------------------------------------------------
- def _polygon_args(args)
- if args[0].is_a?(Point)
- if args.size == 4
- cx, cy = args[0].x, args[0].y
- r, vertex = args[1, 2]
- if args[3].is_a?(Pen)
- color, size = args[3].color, args[3].size
- else
- color, size = args[3], 1
- end
- else
- raise(ArgumentError, "wrong number of arguments(#{args.size} of 4)")
- return [nil]
- end
- else
- if args.size == 5
- cx, cy, r, vertex = args
- if args[4].is_a?(Pen)
- color, size = args[4].color, args[4].size
- else
- color, size = args[4], 1
- end
- else
- raise(ArgumentError, "wrong number of arguments(#{args.size} of 5)")
- return [nil]
- end
- end
- return [cx, cy, r, vertex, color, size]
- end
- protected :_polygon_args
- #--------------------------------------------------------------------------
- # ● 円弧描画
- #--------------------------------------------------------------------------
- def draw_arc(*args)
- # 引数処理
- cx, cy, r, st_ang, end_ang, color, size = _arc_args(args)
- return unless cx
- # 漸化式を初期化
- x = r * Math.cos(Math::PI * st_ang / 180.0)
- y = r * -Math.sin(Math::PI * st_ang / 180.0)
- dfx = dfy = 0
- pr = Integer([100, r].max ** 1.6)
- a = 6.3 / pr
- pr = pr * (end_ang - st_ang) / 360
- # 円弧を描画
- pr.times { |i|
- x, y = x + dfx, y - dfy
- dx, dy = x + cx, y + cy
- if size == 1
- self.set_pixel(dx, dy, color)
- else
- self.fill_rect(dx, dy, size, size, color)
- end
- dfx, dfy = a * y, a * x
- }
- end
- #--------------------------------------------------------------------------
- # ◆ 円弧描画用引数処理
- #--------------------------------------------------------------------------
- def _arc_args(args)
- if args[0].is_a?(Point)
- if args.size == 5
- cx, cy = args[0].x, args[0].y
- r, st_ang, end_ang = args[1, 3]
- if args[4].is_a?(Pen)
- color, size = args[4].color, args[4].size
- else
- color, size = args[4], 1
- end
- else
- raise(ArgumentError, "wrong number of arguments(#{args.size} of 5)")
- return [nil]
- end
- else
- if args.size == 6
- cx, cy, r, st_ang, end_ang = args
- if args[5].is_a?(Pen)
- color, size = args[5].color, args[5].size
- else
- color, size = args[5], 1
- end
- else
- raise(ArgumentError, "wrong number of arguments(#{args.size} of 6)")
- return [nil]
- end
- end
- return [cx, cy, r, st_ang, end_ang, color, size]
- end
- protected :_arc_args
- #--------------------------------------------------------------------------
- # ● 円描画
- #--------------------------------------------------------------------------
- def draw_circle(*args)
- # 引数処理
- cx, cy, r, st_ang, end_ang, color, size = _circle_args(args)
- return unless cx
- draw_arc(cx, cy, r, 0, 360, Pen.new(color, size))
- end
- #--------------------------------------------------------------------------
- # ◆ 円描画用引数処理
- #--------------------------------------------------------------------------
- def _circle_args(args)
- if args[0].is_a?(Point)
- args.insert(2, [0, 360])
- else
- args.insert(3, [0, 360])
- end
- return _arc_args(args.flatten)
- end
- protected :_circle_args
- #--------------------------------------------------------------------------
- # ● 扇形描画
- #--------------------------------------------------------------------------
- def draw_pie(*args)
- # 引数処理
- cx, cy, r, st_ang, end_ang, color, size = _arc_args(args)
- return unless cx
- pen = Pen.new(color, size)
- pt1 = Point.new(cx, cy)
- pt2 = Point.new(cx + r * Math.cos(Math::PI * st_ang / 180.0),
- cy - r * Math.sin(Math::PI * st_ang / 180.0))
- draw_line(pt1, pt2, pen)
- pt2.x = cx + r * Math.cos(Math::PI * end_ang / 180.0)
- pt2.y = cy - r * Math.sin(Math::PI * end_ang / 180.0)
- draw_line(pt1, pt2, pen)
- draw_arc(cx, cy, r, st_ang, end_ang, pen)
- end
- #--------------------------------------------------------------------------
- # ● 楕円描画
- #--------------------------------------------------------------------------
- def draw_ellipse(*args)
- # 引数処理
- cx, cy, w, h, color, size = _ellipse_args(args)
- return unless cx
- # 漸化式を初期化
- x, y = w, 0
- dfx = dfy = 0
- pr = Integer([100, [w, h].max].max ** 1.6)
- a = 6.3 / pr
- # 円弧を描画
- pr.times { |i|
- x, y = x + dfx, y - dfy
- dx, dy = x + cx, y * h / w + cy
- if size == 1
- self.set_pixel(dx, dy, color)
- else
- self.fill_rect(dx, dy, size, size, color)
- end
- dfx, dfy = a * y, a * x
- }
- end
- #--------------------------------------------------------------------------
- # ◆ 楕円描画用引数処理
- #--------------------------------------------------------------------------
- def draw_ellipse(*args)
- # 引数処理
- cx, cy, w, h, color, size = _ellipse_args(args)
- return unless cx
- # 漸化式を初期化
- x, y = w, 0
- dfx = dfy = 0
- pr = Integer([100, [w, h].max].max ** 1.6)
- a = 6.3 / pr
- # 円弧を描画
- pr.times { |i|
- x, y = x + dfx, y - dfy
- dx, dy = x + cx, y * h / w + cy
- if size == 1
- self.set_pixel(dx, dy, color)
- else
- self.fill_rect(dx, dy, size, size, color)
- end
- dfx, dfy = a * y, a * x
- }
- end
- #--------------------------------------------------------------------------
- # ◆ 楕円描画用引数処理
- #--------------------------------------------------------------------------
- def _ellipse_args(args)
- if args[0].is_a?(Point)
- if args.size == 4
- cx, cy = args[0].x, args[0].y
- w, h = args[1, 2]
- if args[3].is_a?(Pen)
- color, size = args[3].color, args[3].size
- else
- color, size = args[3], 1
- end
- else
- raise(ArgumentError, "wrong number of arguments(#{args.size} of 4)")
- return [nil]
- end
- else
- if args.size == 5
- cx, cy, w, h = args
- if args[4].is_a?(Pen)
- color, size = args[4].color, args[4].size
- else
- color, size = args[4], 1
- end
- else
- raise(ArgumentError, "wrong number of arguments(#{args.size} of 5)")
- return [nil]
- end
- end
- return [cx, cy, w, h, color, size]
- end
- protected :_ellipse_args
- end
- #★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★
- #==============================================================================
- # ■ Struct
- #==============================================================================
- # ■ Point 構造体
- Point = Struct.new("Point", :x, :y)
- #★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★
- #==============================================================================
- # ■ Pen
- #==============================================================================
- class Pen
- attr_accessor :color, :size
- def initialize(color, size = 1)
- @color = color.dup
- @size = [size, 1].max
- end
- end
复制代码 |
|