查看: 2522|回复: 2

[原创发布] 用RGD绘制Bézier曲线

[复制链接]
梦石
0
星屑
1944
在线时间
404 小时
注册时间
2015-8-30
回帖
322
发表于 2018-11-18 21:30:07 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有账号?注册会员

×
本帖最后由 有丘直方 于 2018-12-2 20:29 编辑

  1. class Vector
  2.   
  3.   include(Enumerable)
  4.   
  5.   attr_accessor :x
  6.   attr_accessor :y
  7.   
  8.   def initialize(*args)
  9.     case args.size
  10.     when 0
  11.       @x = 0
  12.       @y = 0
  13.     when 1
  14.       @x = args[0]
  15.       @y = args[0]
  16.     when 2
  17.       @x = args[0]
  18.       @y = args[1]
  19.     end
  20.   end
  21.   
  22.   def +@
  23.     self
  24.   end
  25.   
  26.   def -@
  27.     Vector.new(-@x, -@y)
  28.   end
  29.   
  30.   def +(other)
  31.     Vector.new(@x + other.x, @y + other.y)
  32.   end
  33.   
  34.   def -(other)
  35.     self + -other
  36.   end
  37.   
  38.   def *(other)
  39.     case other
  40.     when Numeric
  41.       Vector.new(@x * other, @y * other)
  42.     when Vector
  43.       @x * other.x + @y * other.y
  44.     end
  45.   end
  46.   
  47.   def dot(other)
  48.     self * other
  49.   end
  50.   
  51.   def cross(other)
  52.     self.x * other.y - other.x * self.y
  53.   end
  54.   
  55.   def slope
  56.     y / x
  57.   end
  58.   
  59.   def square_length
  60.     self * self
  61.   end
  62.   
  63.   def length
  64.     Math.sqrt(square_length)
  65.   end
  66.   
  67.   def is_out_of?(other)
  68.     @x.abs > other.x.abs || @y.abs > other.y.abs
  69.   end
  70.   
  71.   def abs
  72.     Vector.new(@x.abs, @y.abs)
  73.   end
  74.   
  75.   def each
  76.     yield @x
  77.     yield @y
  78.   end
  79. end

  80. module Math
  81.   
  82.   def self.definite_proportion(point1, point2, proportion)
  83.     point1 * (1 - proportion) + point2 * proportion
  84.   end
  85. end

  86. class Rect
  87.   
  88.   alias ulysses201811181949_initialize initialize
  89.   def initialize(*args)
  90.     args = case args.size
  91.     when 0, 4
  92.       args
  93.     when 2
  94.       [args[0].x, args[0].y, args[1].x, args[1].y]
  95.     end
  96.     ulysses201811181949_initialize(*args)
  97.   end
  98.   
  99.   def position
  100.     Vector.new(x, y)
  101.   end
  102.   
  103.   def position=(position)
  104.     self.x = position.x
  105.     self.y = position.y
  106.   end
  107.   
  108.   def size
  109.     Vector.new(width, height)
  110.   end
  111.   
  112.   def size=(size)
  113.     self.width = size.x
  114.     self.height = size.y
  115.   end
  116. end

  117. module Kernel
  118.   
  119.   def do_by_step(from, to, step)
  120.     if block_given?
  121.       i = from
  122.       until i >= to
  123.         yield i
  124.         i += step
  125.       end
  126.     else
  127.       to_enum(__method__, from, to, step)
  128.     end
  129.   end
  130. end

  131. class Bitmap
  132.   
  133.   alias ulysses201811181708_initialize initialize
  134.   def initialize(*args)
  135.     args = if args.size == 2 || args[0].is_a?(String)
  136.       args
  137.     elsif args.size == 1
  138.       [args[0].x, args[0].y]
  139.     else
  140.       [0, 0]
  141.     end
  142.     ulysses201811181708_initialize(*args)
  143.   end
  144.   
  145.   def fill_circle(center, radius, color)
  146.     i_max = (radius * 707) / 1000 + 1
  147.     square_distance_max = radius * radius + radius / 2
  148.     x = radius
  149.     fill_rect(center.x - radius, center.y, 2 * radius, 1, color)
  150.     for i in 1..i_max
  151.       if Vector.new(center.x, i).square_length > square_distance_max
  152.         if x > i_max
  153.           fill_rect(center.x - i + 1, center.y + x, 2 * (i - 1), 1, color)
  154.           fill_rect(center.x - i + 1, center.y - x, 2 * (i - 1), 1, color)
  155.         end
  156.         x -= 1
  157.       end
  158.       fill_rect(center.x - x, center.y + i, 2 * x, 1, color)
  159.       fill_rect(center.x - x, center.y - i, 2 * x, 1, color)
  160.     end
  161.   end
  162.   
  163.   def draw_line(point1, point2, color)
  164.     difference = point2 - point1
  165.     abs_difference = difference.abs
  166.     total_step = abs_difference.max
  167.     delta = difference * (1.0 / total_step)
  168.     vector = point1
  169.     (total_step - 1).to_i.times do
  170.       set_pixel(vector, color)
  171.       vector += delta
  172.     end
  173.   end
  174.   
  175.   alias ulysses201811182036_set_pixel set_pixel
  176.   def set_pixel(*args)
  177.     args = case args.size
  178.     when 3
  179.       args
  180.     when 2
  181.       [args[0].x, args[0].y, args[1]]
  182.     end
  183.     ulysses201811182036_set_pixel(*args)
  184.   end
  185.   
  186.   def text_vector(text)
  187.     text_size(text).size
  188.   end
  189.   
  190.   def size
  191.     Vector.new(width, height)
  192.   end
  193.   
  194.   def size=(size)
  195.     self.rect.size = size
  196.   end
  197.   
  198.   alias ulysses201811181952_draw_text draw_text
  199.   def draw_text(*args)
  200.     args = case args[0]
  201.     when Numeric, Rect
  202.       args
  203.     when Vector
  204.       a = [args[0].x, args[0].y, args[1].x, args[1].y, args[2]]
  205.       a.push(args[3]) if args.size >= 4
  206.       a
  207.     end
  208.     ulysses201811181952_draw_text(*args)
  209.   end
  210. end

  211. class Sprite
  212.   
  213.   alias ulysses201811181903_initialize initialize
  214.   def initialize(viewport = nil)
  215.     ulysses201811181903_initialize(viewport)
  216.     self.bitmap = Bitmap.new
  217.   end
  218.   
  219.   def position=(position)
  220.     self.x = position.x
  221.     self.y = position.y
  222.   end
  223. end

  224. module Graphics
  225.   
  226.   def self.size
  227.     Vector.new(width, height)
  228.   end
  229. end

  230. module Mouse
  231.   
  232.   def self.position
  233.     Vector.new(x, y)
  234.   end
  235. end

  236. module Scene
  237.   
  238.   POINT_RADIUS = 3
  239.   POINT_COLOR = Color.new(255, 255, 255, 255)
  240.   POINT_Z = 10
  241.   CURVE_RADIUS = 2
  242.   CURVE_COLOR = Color.new(255, 255, 255, 255)
  243.   LINE_COLOR = Color.new(255, 255, 255, 255)
  244.   T_BEGINNING = 0.0
  245.   T_END = 1.0
  246.   T_STEP = 0.001
  247.   UPDATE_STEPS = 20
  248.   
  249.   def self.init
  250.     Graphics.resize_screen(1024, 768)
  251.     @graph_sprite = Sprite.new
  252.     @graph_sprite.bitmap = Bitmap.new(Graphics.size)
  253.     @line_sprite = Sprite.new
  254.     @line_sprite.bitmap = Bitmap.new(Graphics.size)
  255.     @points = []
  256.     @point_sprites = []
  257.     @temp_bitmap = Bitmap.new(32, 32)
  258.     @line_visible = true
  259.     @point_visible = true
  260.     @fast_mode = false
  261.     @covering_drawing = 0
  262.   end
  263.   
  264.   def self.create_point(point)
  265.     sprite = Sprite.new
  266.     sprite.visible = @point_visible
  267.     radius_vector = Vector.new(POINT_RADIUS)
  268.     text_vector = @temp_bitmap.text_vector(@points.size)
  269.     radius_text_vector = radius_vector + text_vector
  270.     sprite.bitmap = Bitmap.new(radius_text_vector + radius_vector)
  271.     flip = (point + radius_text_vector).is_out_of?(Graphics.size)
  272.     excursion = flip ? radius_text_vector : radius_vector
  273.     sprite.position = point - excursion
  274.     sprite.bitmap.fill_circle(excursion, POINT_RADIUS, POINT_COLOR)
  275.     text_position = flip ? Vector.new : radius_vector * 2
  276.     sprite.bitmap.draw_text(text_position, text_vector, @points.size)
  277.     sprite.z = POINT_Z
  278.     @points.push(point)
  279.     @point_sprites.push(sprite)
  280.     refresh_graph
  281.   end
  282.   
  283.   def self.delete_point
  284.     @points.pop
  285.     @point_sprites.pop.dispose
  286.     refresh_graph
  287.   end
  288.   
  289.   def self.refresh_graph
  290.     @covering_drawing += 1
  291.     @graph_sprite.bitmap.clear
  292.     return if @points.empty?
  293.     step_count = 0
  294.     will_update = false
  295.     do_by_step(T_BEGINNING, T_END, T_STEP) do |t|
  296.       will_update = true if !@fast_mode && (step_count += 1) >= UPDATE_STEPS
  297.       @line_sprite.bitmap.clear
  298.       sequences = [@points]
  299.       (@points.size - 1).times do |i|
  300.         last_sequence = sequences.last
  301.         sequences.push([])
  302.         (last_sequence.size - 1).times do |j|
  303.           if @line_visible && will_update
  304.             @line_sprite.bitmap.draw_line(*last_sequence[j, 2], LINE_COLOR)
  305.           end
  306.           sequences.last.push(Math.definite_proportion(*last_sequence[j, 2], t))
  307.         end
  308.       end
  309.       @graph_sprite.bitmap.fill_circle(*sequences.last, CURVE_RADIUS, CURVE_COLOR)
  310.       if will_update
  311.         step_count = 0
  312.         will_update = false
  313.         last_covering_drawing = @covering_drawing
  314.         update
  315.         return if @covering_drawing > last_covering_drawing
  316.       end
  317.     end
  318.     (@points.size - 1).times do |i|
  319.       @line_sprite.bitmap.draw_line(*@points[i, 2], LINE_COLOR)
  320.     end
  321.   end
  322.   
  323.   def self.toggle_line_visible
  324.     @line_visible = !@line_visible
  325.     @line_sprite.visible = @line_visible
  326.   end
  327.   
  328.   def self.toggle_point_visible
  329.     @point_visible = !@point_visible
  330.     @point_sprites.each {|sprite| sprite.visible = @point_visible }
  331.   end
  332.   
  333.   def self.toggle_fast_mode
  334.     @fast_mode = !@fast_mode
  335.   end
  336.   
  337.   def self.update_basic
  338.     Graphics.update
  339.     Input.update
  340.     Mouse.update
  341.   end
  342.   
  343.   def self.update
  344.     update_basic
  345.     if Mouse.click?(:L)
  346.       create_point(Mouse.position)
  347.     elsif Mouse.click?(:R)
  348.       delete_point
  349.     elsif Input.trigger?(:A)
  350.       toggle_fast_mode
  351.     elsif Input.trigger?(:B)
  352.       toggle_line_visible
  353.     elsif Input.trigger?(:C)
  354.       toggle_point_visible
  355.     end
  356.   end
  357.   def self.main
  358.     init
  359.     loop { update }
  360.   end
  361. end

  362. Scene.main
复制代码
马上要睡了,不多说了,看图
Animation.gif
本来想用shader的,后来还是屈服于Bitmap了……
按左键添加节点,按右键去除节点,按A开启快速模式(不显示绘制过程),按B切换线段的显示,按C切换节点的显示(此处ABC不是键盘上的ABC)

  1. # 二维向量
  2. class Vector
  3.   
  4.   # 可以进行枚举,参见#each
  5.   include(Enumerable)
  6.   
  7.   attr_accessor :x # x方向上的分量
  8.   attr_accessor :y # y方向上的分量
  9.   
  10.   # 初始化
  11.   # 无参数时:(0, 0)
  12.   # 有一个参数r时:(r, r)
  13.   # 有两个参数x, y时:(x, y)
  14.   def initialize(*args)
  15.     case args.size
  16.     when 0
  17.       @x = 0
  18.       @y = 0
  19.     when 1
  20.       @x = args[0]
  21.       @y = args[0]
  22.     when 2
  23.       @x = args[0]
  24.       @y = args[1]
  25.     end
  26.   end
  27.   
  28.   def +@
  29.     self
  30.   end
  31.   
  32.   # 反向量
  33.   def -@
  34.     Vector.new(-@x, -@y)
  35.   end
  36.   
  37.   # 加法
  38.   def +(other)
  39.     Vector.new(@x + other.x, @y + other.y)
  40.   end
  41.   
  42.   # 减法
  43.   def -(other)
  44.     self + -other
  45.   end
  46.   
  47.   # 点乘或数乘运算
  48.   def *(other)
  49.     case other
  50.     when Numeric
  51.       Vector.new(@x * other, @y * other)
  52.     when Vector
  53.       @x * other.x + @y * other.y
  54.     end
  55.   end
  56.   
  57.   # 点积
  58.   def dot(other)
  59.     @x * other.x + @y * other.y
  60.   end
  61.   
  62.   # 叉积
  63.   def cross(other)
  64.     self.x * other.y - other.x * self.y
  65.   end
  66.   
  67.   # 斜率
  68.   def slope
  69.     x =! 0 ? y / x : Float::Infinity
  70.   end
  71.   
  72.   # 模的平方
  73.   def square_length
  74.     self * self
  75.   end
  76.   
  77.   # 模
  78.   def length
  79.     Math.sqrt(square_length)
  80.   end
  81.   
  82.   # 超出范围
  83.   def is_out_of?(other)
  84.     @x.abs > other.x.abs || @y.abs > other.y.abs
  85.   end
  86.   
  87.   # 对x和y方向的分量分别取绝对值
  88.   def abs
  89.     Vector.new(@x.abs, @y.abs)
  90.   end
  91.   
  92.   # 切向
  93.   def normal
  94.     Vector.new(-@y, @x)
  95.   end
  96.   
  97.   # 枚举,先对x计算,后对y计算
  98.   def each
  99.     yield @x
  100.     yield @y
  101.   end
  102.   
  103.   def inspect
  104.     "(#{@x}, #{@y})"
  105.   end
  106. end

  107. module Math
  108.   
  109.   # 获得两点之间由某个比例决定的分点
  110.   def self.definite_proportion(point1, point2, proportion)
  111.     point1 * (1 - proportion) + point2 * proportion
  112.   end
  113. end

  114. class Rect
  115.   
  116.   # 初始化
  117.   # 除了原先的参数列表,支持由两个向量来决定其位置大小
  118.   # 如果用向量表示,第一个向量是position,第二个向量是size
  119.   alias ulysses201811181949_initialize initialize
  120.   def initialize(*args)
  121.     args = case args.size
  122.     when 0, 4
  123.       args
  124.     when 2
  125.       [args[0].x, args[0].y, args[1].x, args[1].y]
  126.     end
  127.     ulysses201811181949_initialize(*args)
  128.   end
  129.   
  130.   def position
  131.     Vector.new(x, y)
  132.   end
  133.   
  134.   def position=(position)
  135.     self.x = position.x
  136.     self.y = position.y
  137.   end
  138.   
  139.   def size
  140.     Vector.new(width, height)
  141.   end
  142.   
  143.   def size=(size)
  144.     self.width = size.x
  145.     self.height = size.y
  146.   end
  147. end

  148. module Kernel
  149.   
  150.   # 令变量从from到to按照step进行线性变化并传递到块中
  151.   def do_by_step(from, to, step)
  152.     if block_given?
  153.       i = from
  154.       until i >= to
  155.         yield i
  156.         i += step
  157.       end
  158.     else
  159.       to_enum(__method__, from, to, step)
  160.     end
  161.   end
  162.   
  163.   def gets(*args)
  164.     $stdin.gets(*args)
  165.   end
  166.   
  167.   def Vector(*args)
  168.     Vector.new(*args)
  169.   end
  170. end

  171. class Bitmap
  172.   
  173.   # 初始化
  174.   # 支持向量表示
  175.   alias ulysses201811181708_initialize initialize
  176.   def initialize(*args)
  177.     args = if args.size == 2 || args[0].is_a?(String)
  178.       args
  179.     elsif args.size == 1
  180.       [args[0].x, args[0].y]
  181.     else
  182.       [0, 0]
  183.     end
  184.     ulysses201811181708_initialize(*args)
  185.   end
  186.   
  187.   # 填充一个圆
  188.   def fill_circle(center, radius, color)
  189.     i_max = (radius * 707) / 1000 + 1
  190.     square_distance_max = radius * radius + radius / 2
  191.     x = radius
  192.     fill_rect(center.x - radius, center.y, 2 * radius, 1, color)
  193.     for i in 1..i_max
  194.       if Vector.new(center.x, i).square_length > square_distance_max
  195.         if x > i_max
  196.           fill_rect(center.x - i + 1, center.y + x, 2 * (i - 1), 1, color)
  197.           fill_rect(center.x - i + 1, center.y - x, 2 * (i - 1), 1, color)
  198.         end
  199.         x -= 1
  200.       end
  201.       fill_rect(center.x - x, center.y + i, 2 * x, 1, color)
  202.       fill_rect(center.x - x, center.y - i, 2 * x, 1, color)
  203.     end
  204.   end
  205.   
  206.   # 用一条1像素宽的线段联结两点
  207.   def draw_line(point1, point2, color)
  208.     difference = point2 - point1
  209.     abs_difference = difference.abs
  210.     total_step = abs_difference.max
  211.     delta = difference * (1.0 / total_step)
  212.     vector = point1
  213.     (total_step.to_i + 1).times do
  214.       set_pixel(vector, color)
  215.       vector += delta
  216.     end
  217.   end
  218.   
  219.   alias ulysses201811182036_set_pixel set_pixel
  220.   def set_pixel(*args)
  221.     args = case args.size
  222.     when 3
  223.       args
  224.     when 2
  225.       [args[0].x, args[0].y, args[1]]
  226.     end
  227.     ulysses201811182036_set_pixel(*args)
  228.   end
  229.   
  230.   def text_vector(text)
  231.     text_size(text).size
  232.   end
  233.   
  234.   def size
  235.     Vector.new(width, height)
  236.   end
  237.   
  238.   def size=(size)
  239.     self.rect.size = size
  240.   end
  241.   
  242.   alias ulysses201811181952_draw_text draw_text
  243.   def draw_text(*args)
  244.     args = case args[0]
  245.     when Numeric, Rect
  246.       args
  247.     when Vector
  248.       a = [args[0].x, args[0].y, args[1].x, args[1].y, args[2]]
  249.       a.push(args[3]) if args.size >= 4
  250.       a
  251.     end
  252.     ulysses201811181952_draw_text(*args)
  253.   end
  254. end

  255. class Sprite
  256.   
  257.   def position=(position)
  258.     self.x = position.x
  259.     self.y = position.y
  260.   end
  261. end

  262. class Viewport
  263.   
  264.   # 让add_fast_layer接受默认参数blend_type = 0
  265.   alias ulysses201812021851_add_fast_layer add_fast_layer
  266.   def add_fast_layer(z, blend_type = 0)
  267.     ulysses201812021851_add_fast_layer(z, blend_type)
  268.   end
  269. end

  270. # 重写了Input模块
  271. # 支持全键盘和鼠标按键
  272. # 如果不需要判断鼠标位置的话,可以不update
  273. module Input
  274.   
  275.   REPEAT_TIME = 4 # 当key被按超过此数时,repeat?(key) == true
  276.   GetKeyState = Win32API.new("user32", "GetAsyncKeyState", ['I'], 'I')
  277.   
  278.   @key_states = {}
  279.   @key_repeat_times = {}
  280.   
  281.   def self.press?(key)
  282.     key < 0x08 ? Mouse.press?(key) : GetKeyState.call(key) != 0
  283.   end
  284.   
  285.   def self.repeat?(key)
  286.     if press?(key)
  287.       unless @key_repeat_times[key]
  288.         @key_repeat_times[key] = 0
  289.         return true
  290.       end
  291.       @key_repeat_times[key] += 1
  292.     else
  293.       @key_repeat_times[key] = nil
  294.       @key_states[key] = 0
  295.     end
  296.     if !@key_repeat_times[key].nil? && @key_repeat_times[key] > REPEAT_TIME
  297.       @key_repeat_times[key] = 0
  298.       return true
  299.     end
  300.     false
  301.   end
  302.   
  303.   def self.trigger?(key)
  304.     if press?(key)
  305.       return false if @key_states[key] == 1
  306.       @key_states[key] = 1
  307.       true
  308.     else
  309.       @key_states[key] = 0
  310.       false
  311.     end
  312.   end
  313.   
  314.   def self.update
  315.     Mouse.update
  316.   end
  317. end

  318. class << Graphics
  319.   
  320.   def size
  321.     Vector.new(width, height)
  322.   end
  323.   
  324.   alias ulysses201811251830_resize_screen resize_screen
  325.   def resize_screen(*args)
  326.     case args.size
  327.     when 2
  328.       ulysses201811251830_resize_screen(*args)
  329.     when 1
  330.       ulysses201811251830_resize_screen(args[0].x, args[0].y)
  331.     end
  332.   end
  333. end

  334. Graphics.resize_screen(1024, 768) # 更改分辨率

  335. module Mouse
  336.   
  337.   def self.position
  338.     Vector.new(x, y)
  339.   end
  340. end

  341. # 封装为模块的场景
  342. module Scene
  343.   
  344.   # 绘制控制点的参数
  345.   POINTS_RADIUS = 3
  346.   POINTS_COLOR = Color.new(255, 255, 255, 255)
  347.   POINTS_Z = 10
  348.   # 绘制曲线的参数
  349.   CURVE_COLOR = Color.new(255, 255, 255, 255)
  350.   CURVE_Z = 0
  351.   # 绘制线段的参数
  352.   LINES_COLOR = Color.new(255, 255, 255, 127)
  353.   LINES_Z = -10
  354.   # 用于调整绘制的范围和精度
  355.   T_BEGINNING = 0.0
  356.   T_END = 1.0
  357.   T_STEP = 0.01
  358.   UPDATE_STEPS = 5
  359.   TEMP_BITMAP = Bitmap.new(32, 32) # 临时位图,用于获取文字大小
  360.   # 按键
  361.   CREATE_CONTROL_POINT_KEY = 0x01 # left
  362.   DELETE_CONTROL_POINT_KEY = 0x02 # right
  363.   FAST_MODE_KEY = 0x41 # A
  364.   LINES_VISIBLE_KEY = 0x42 # B
  365.   CONTROL_POINTS_VISIBLE_KEY = 0x43 # C
  366.   
  367.   # 显示端口
  368.   @viewport = Viewport.new
  369.   @viewport.add_fast_layer(POINTS_Z)
  370.   # 曲线
  371.   @curve_sprite = Sprite.new(@viewport)
  372.   @curve_sprite.bitmap = Bitmap.new(Graphics.size)
  373.   @curve_sprite.z = CURVE_Z
  374.   # 线段
  375.   @lines_sprite = Sprite.new(@viewport)
  376.   @lines_sprite.bitmap = Bitmap.new(Graphics.size)
  377.   @lines_sprite.z = LINES_Z
  378.   # 控制点
  379.   @control_points = []
  380.   @control_point_sprites = []
  381.   # 模式控制
  382.   @lines_visible = true
  383.   @control_points_visible = true
  384.   @fast_mode = false
  385.   # 阻断刷新控制
  386.   @covering_drawing = 0
  387.   
  388.   # 创建控制点
  389.   def self.create_control_point(point)
  390.     sprite = Sprite.new(@viewport)
  391.     sprite.visible = @control_points_visible
  392.     radius_vector = Vector.new(POINTS_RADIUS)
  393.     text_vector = TEMP_BITMAP.text_vector(@control_points.size)
  394.     radius_text_vector = radius_vector + text_vector
  395.     sprite.bitmap = Bitmap.new(radius_text_vector + radius_vector)
  396.     sprite.bitmap.font.color = POINTS_COLOR
  397.     x_flip = (point + radius_text_vector).x > Graphics.width
  398.     y_flip = (point + radius_text_vector).y > Graphics.height
  399.     excursion = Vector.new
  400.     excursion.x = x_flip ? radius_text_vector.x : radius_vector.x
  401.     excursion.y = y_flip ? radius_text_vector.y : radius_vector.y
  402.     sprite.position = point - excursion
  403.     sprite.bitmap.fill_circle(excursion, POINTS_RADIUS, POINTS_COLOR)
  404.     text_position = Vector.new
  405.     text_position.x = x_flip ? 0 : radius_vector.x * 2
  406.     text_position.y = y_flip ? 0 : radius_vector.y * 2
  407.     sprite.bitmap.draw_text(text_position, text_vector, @control_points.size)
  408.     sprite.z = POINTS_Z
  409.     @control_point_sprites.push(sprite)
  410.     @control_points.push(point)
  411.     refresh_graph
  412.     point
  413.   end
  414.   
  415.   # 删除上一个控制点
  416.   def self.delete_control_point
  417.     return if @control_points.empty?
  418.     point = @control_points.pop
  419.     sprite = @control_point_sprites.pop
  420.     sprite.bitmap.dispose
  421.     sprite.dispose
  422.     refresh_graph
  423.     point
  424.   end
  425.   
  426.   # 刷新图形
  427.   # 该方法在非快速模式下会阻塞,但update不会停且可以被打断
  428.   def self.refresh_graph
  429.     @covering_drawing += 1
  430.     @curve_sprite.bitmap.clear
  431.     return if @control_points.empty?
  432.     graph_points = []
  433.     steps_count = 0
  434.     do_by_step(T_BEGINNING, T_END, T_STEP) do |t|
  435.       will_update = !@fast_mode && (steps_count += 1) >= UPDATE_STEPS
  436.       graph_points.push(next_graph_point(t, will_update))
  437.       if graph_points.size > 1
  438.         @curve_sprite.bitmap.draw_line(*graph_points.last(2), CURVE_COLOR)
  439.       end
  440.       if will_update
  441.         steps_count = 0
  442.         last_covering_drawing = @covering_drawing
  443.         update
  444.         return if @covering_drawing > last_covering_drawing
  445.       end
  446.     end
  447.     connect_control_points_with_lines
  448.   end
  449.   
  450.   # 根据t返回曲线上的点坐标
  451.   # 若@lines_visible && will_update则会绘制线段
  452.   def self.next_graph_point(t, will_update = false)
  453.     is_drawing = @lines_visible && will_update
  454.     @lines_sprite.bitmap.clear if is_drawing
  455.     sequences = [@control_points]
  456.     (@control_points.size - 1).times do |i|
  457.       last_sequence = sequences.last
  458.       sequences.push([])
  459.       (last_sequence.size - 1).times do |j|
  460.         if is_drawing
  461.           @lines_sprite.bitmap.draw_line(*last_sequence[j, 2], LINES_COLOR)
  462.         end
  463.         sequences.last.push(Math.definite_proportion(*last_sequence[j, 2], t))
  464.       end
  465.     end
  466.     sequences.last.first
  467.   end
  468.   
  469.   # 用线段将控制点联结
  470.   def self.connect_control_points_with_lines
  471.     @lines_sprite.bitmap.clear
  472.     (@control_points.size - 1).times do |i|
  473.       @lines_sprite.bitmap.draw_line(*@control_points[i, 2], LINES_COLOR)
  474.     end
  475.     nil
  476.   end
  477.   
  478.   # 切换线段可见
  479.   def self.toggle_lines_visible
  480.     @lines_visible = !@lines_visible
  481.     @lines_sprite.visible = @lines_visible
  482.   end
  483.   
  484.   # 切换控制点可见
  485.   def self.toggle_points_visible
  486.     @control_points_visible = !@control_points_visible
  487.     @control_point_sprites.each do |sprite|
  488.       sprite.visible = @control_points_visible
  489.     end
  490.   end
  491.   
  492.   # 切换快速模式
  493.   def self.toggle_fast_mode
  494.     @fast_mode = !@fast_mode
  495.   end
  496.   
  497.   # 基础更新,必须每帧调用
  498.   def self.update_basic
  499.     Graphics.update
  500.     Input.update
  501.   end
  502.   
  503.   # 更新,需每帧调用
  504.   def self.update
  505.     update_basic
  506.     if Input.trigger?(CREATE_CONTROL_POINT_KEY)
  507.       create_control_point(Mouse.position)
  508.     elsif Input.trigger?(DELETE_CONTROL_POINT_KEY)
  509.       delete_control_point
  510.     elsif Input.trigger?(FAST_MODE_KEY)
  511.       toggle_fast_mode
  512.     elsif Input.trigger?(LINES_VISIBLE_KEY)
  513.       toggle_lines_visible
  514.     elsif Input.trigger?(CONTROL_POINTS_VISIBLE_KEY)
  515.       toggle_points_visible
  516.     end
  517.   end
  518.   
  519.   # 简单的irb
  520.   def self.simple_irb_start
  521.     Thread.start do
  522.       ans = nil
  523.       loop { puts("=> #{(ans = eval(gets)).inspect}") rescue puts($!.message) }
  524.     end
  525.   end
  526.   
  527.   # 主进程
  528.   def self.main
  529.     simple_irb_start
  530.     loop { update }
  531.   end
  532. end

  533. # 入口
  534. Scene.main
复制代码
更新了很多地方,懒得说了,依赖这玩意https://rpg.blue/thread-400462-1-1.html

评分

参与人数 5赞 +5 收起 理由
kirh_036 + 1 芳芳醋虾
Kim_Shyuen + 1 雖然不是很明白 但是好像很厲害.
百里_飞柳 + 1 有丶东西
KB.Driver + 1 精品文章
张咚咚 + 1 精品文章

查看全部评分

小仙女一枚~
梦石
0
星屑
733
在线时间
886 小时
注册时间
2012-5-24
回帖
893
发表于 2018-11-18 22:18:55 | 显示全部楼层
厉害啊太强了。。。。。。
回复

使用道具 举报

梦石
10
星屑
39550
在线时间
1919 小时
注册时间
2010-11-14
回帖
3214

R考场第七期纪念奖

发表于 2018-11-18 23:19:03 | 显示全部楼层
太强了……
回复

使用道具 举报

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

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部