查看: 14812|回复: 2

[胡扯] 论用RGSS画复变函数图像

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

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

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

×
本帖最后由 有丘直方 于 2018-9-9 20:40 编辑

这个东西貌似已经有人搞过了??
  1. # Color
  2. class Color
  3.   
  4.   # 根据HSL数据来生成颜色对象,第四个参数是不透明度
  5.   def self.hsl(hue, saturation, lightness, alpha = 1.0)
  6.     saturation = saturation.clamp(0.0, 1.0)
  7.     lightness = lightness.clamp(0.0, 1.0)
  8.     alpha = alpha.clamp(0.0, 1.0)
  9.    
  10.     times = (hue * 3 / Math::PI).modulo(6)
  11.     amount1 = (1 - (2 * lightness - 1).abs) * saturation
  12.     amount2 = amount1 * (1 - (times % 2 - 1).abs)
  13.     correction = lightness - amount1 / 2
  14.     translation = (times.to_i + 1) % 6
  15.    
  16.     args = [0, 0, 0]
  17.     args[translation / 2] = amount1
  18.     args[2 - translation % 3] = amount2
  19.    
  20.     new(*args.map {|origin| (origin + correction) * 255 }, alpha * 255)
  21.   end
  22.   
  23.   # 在此颜色之上按正常方式叠加另一个颜色时呈现的颜色
  24.   def blend(other)
  25.     alpha1 = alpha / 255.0
  26.     alpha2 = other.alpha / 255.0
  27.     calc_blend = proc do |c1, c2|
  28.       c1 /= 255.0
  29.       c2 /= 255.0
  30.       blend = c1 * alpha1 * (1 - alpha2) + c2 * alpha2
  31.       blend /= alpha1 + alpha2 - alpha1 * alpha2
  32.       blend *= 255
  33.     end
  34.     result = Color.new
  35.     result.alpha = (alpha1 + alpha2 - alpha1 * alpha2) * 255
  36.     result.red = calc_blend.call(red, other.red)
  37.     result.green = calc_blend.call(green, other.green)
  38.     result.blue = calc_blend.call(blue, other.blue)
  39.     result
  40.   end
  41.   
  42.   # blend的破坏型方法
  43.   def blend!(other)
  44.     set(blend(other))
  45.     self
  46.   end
  47.   
  48. end

  49. # Comparable
  50. module Comparable
  51.   
  52.   # 返回在min和max的范围内最接近self的对象
  53.   def clamp(min, max)
  54.     return min if self <=> min < 0
  55.     return max if self <=> max > 0
  56.     self
  57.   end
  58.   
  59. end

  60. # 关于x的以mean为期望,variance为方差的正态分布函数
  61. # 不给出mean和variance参数时是标准正态分布函数
  62. def Math.normal_distribution(x, mean = 0.0, variance = 1.0)
  63.   exp(-(x - mean) ** 2 / (2 * variance)) / sqrt(2 * self::PI * variance)
  64. end

  65. # Complex
  66. class Complex
  67.   
  68.   # 获取此复数对应的定义域着色
  69.   # lightness_base: 用于计算亮度的函数中的底数,越小越亮,必须在0和1之间
  70.   # saturation: 饱和度
  71.   def to_color(lightness_base = 0.5, saturation = 1.0)
  72.     raise unless lightness_base.between?(0, 1)
  73.     exponent = abs
  74.     hue = exponent == 0 ? 0.0 : arg
  75.     lightness = 1 - lightness_base ** exponent
  76.     Color.hsl(hue, saturation, lightness)
  77.   end
  78.   
  79.   # 获取此复数对应的结构化的定义域着色
  80.   # 这种着色方案有特殊效果,具体看参数说明
  81.   # discontinuity_base: 当此复数的模是达到参数的整数次幂时,亮度骤然变为最低
  82.   # saturation: 饱和度
  83.   # min_lightness: 不考虑网格和射线时能达到的最低亮度
  84.   # lightness_range: 不考虑网格和射线时能达到的最高和最低亮度之差
  85.   # grid_hue: 网格的色相
  86.   # rays_hue: 射线的色相
  87.   # grid_lightness: 网格的亮度
  88.   # rays_lightness: 射线的亮度
  89.   # grid_density: 网格线的密度,复数的实部或虚部是参数的倒数的整数倍时出现网格
  90.   # rays_density: 射线的密度,复数的俯角是参数的倒数的整数倍时出现射线
  91.   # grid_radius: 网格线的宽度的一半,不包括扩散出去的部分
  92.   # rays_radius: 射线的宽度的一半,不包括扩散出去的部分
  93.   # grid_restraint: 网格线的扩散度,越大越模糊
  94.   # rays_restraint: 射线的扩散度,越大越模糊
  95.   # grid_opacity: 网格的不透明度,如果不想显示网格请设为0.0
  96.   # rays_opacity: 射线的不透明度,如果不想显示射线请设为0.0
  97.   def to_color_structured(discontinuity_base = 2.0, saturation = 1.0,
  98.       min_lightness = 0.3, lightness_range = 0.4, grid_hue = 0.0,
  99.       rays_hue = 0.0, grid_lightness = 0.0, rays_lightness = 1.0,
  100.       grid_density = 1.0, rays_density = 6.0, grid_radius = 0.005,
  101.       rays_radius = 0.01, grid_restraint = 60.0, rays_restraint = 50.0,
  102.       grid_opacity = 0.2, rays_opacity = 0.8)
  103.     antilog = abs
  104.     hue = antilog == 0 ? 0.0 : arg
  105.    
  106.     lightness_phase = Math.log(antilog, discontinuity_base).modulo(1)
  107.     lightness_phase = 0.0 if lightness_phase.nan?
  108.     lightness = lightness_phase * lightness_range + min_lightness
  109.    
  110.     color = Color.hsl(hue, saturation, lightness)
  111.     f = Math.sqrt(2 * Math::PI)
  112.    
  113.     dense_real = real * grid_density
  114.     real_distance = (dense_real - dense_real.round).abs
  115.     dense_imaginary = imaginary * grid_density
  116.     imaginary_distance = (dense_imaginary - dense_imaginary.round).abs
  117.     grid_distance = [[real_distance, imaginary_distance].min, grid_radius].max
  118.     grid_excursion = (grid_distance - grid_radius) * grid_restraint
  119.     grid_alpha = Math.normal_distribution(grid_excursion) * f * grid_opacity
  120.     grid_color = Color.hsl(grid_hue, saturation, grid_lightness, grid_alpha)
  121.     color.blend!(grid_color)
  122.    
  123.     rays_distances = Array.new(rays_density) do |i|
  124.       slope = Math.tan(i / rays_density * Math::PI)
  125.       (slope * real - imaginary).abs / Math.hypot(slope, -1)
  126.     end
  127.     rays_distance = [rays_distances.min, rays_radius].max
  128.     rays_excursion = (rays_distance - rays_radius) * rays_restraint
  129.     rays_alpha = Math.normal_distribution(rays_excursion) * f * rays_opacity
  130.     rays_color = Color.hsl(rays_hue, saturation, rays_lightness, rays_alpha)
  131.     color.blend!(rays_color)
  132.   end
  133. end

  134. # Bitmap
  135. class Bitmap
  136.   # 绘制复变函数的函数图像
  137.   # function: 需要绘制的函数,提供接受一个复数参数并返回复数的call方法
  138.   # delta_x: 图中1像素的距离代表的大小
  139.   # structured: 是否采用结构化的图像,见Complex#to_color_structured
  140.   # ox: 自变量取0+0i时所在的位置的相对于位图的x坐标
  141.   # oy: 自变量取0+0i时所在的位置的相对于位图的y坐标
  142.   def draw_function(function, delta_x, structured = false,
  143.     ox = Graphics.width / 2, oy = Graphics.height / 2)
  144.     width.times do |x|
  145.       height.times do |y|
  146.         real = (x - ox) * delta_x
  147.         imaginary = (oy - y) * delta_x
  148.         complex = function.call(Complex(real, imaginary))
  149.         color = structured ? complex.to_color_structured : complex.to_color
  150.         set_pixel(x, y, color)
  151.       end
  152.     end
  153.   end
  154. end
复制代码
一个简单的用于玩耍的代码(依赖这玩意)(由于我用了RGD所以无视分辨率限制了)(虽然不用多线程能画得更快,但是那样就看不到图像慢慢绘制出来的过程了):
  1. # 玩耍方法:在控制台输入你想要画的函数,然后等待即可
  2. Graphics.resize_screen(1024, 768)
  3. sprite = Sprite.new
  4. sprite.bitmap = Bitmap.new(Graphics.width, Graphics.height)
  5. Thread.start do
  6.   loop do
  7.     print("f(z) = ")
  8.     string = $stdin.gets
  9.     function = eval("->(z) { #{string} }")
  10.     puts("Please wait...")
  11.     time = Time.now
  12.     sprite.bitmap.draw_function(function, 0.008, true) rescue puts($!.message)
  13.     printf("Completed after %d seconds.\n", Time.now - time)
  14.   end
  15. end

  16. rgss_stop
复制代码
配上CMath后玩耍体验更佳:
  1. #==============================================================================
  2. # * CMath
  3. #------------------------------------------------------------------------------
  4. # Trigonometric and transcendental functions for complex numbers.
  5. # CMath is a library that provides trigonometric and transcendental functions
  6. # for complex numbers. The functions in this module accept integers, floating-
  7. # point numbers or complex numbers as arguments.
  8. # Note that the selection of functions is similar, but not identical, to that
  9. # in module math. The reason for having two modules is that some users aren't
  10. # interested in complex numbers, and perhaps don't even know what they are. They
  11. # would rather have Math.sqrt(-1) raise an exception than return a complex
  12. # number.
  13. # For more information you can see Complex class.
  14. #==============================================================================

  15. module CMath

  16.   include Math

  17.   alias exp! exp
  18.   alias log! log
  19.   alias log2! log2
  20.   alias log10! log10
  21.   alias sqrt! sqrt
  22.   alias cbrt! cbrt

  23.   alias sin! sin
  24.   alias cos! cos
  25.   alias tan! tan

  26.   alias sinh! sinh
  27.   alias cosh! cosh
  28.   alias tanh! tanh

  29.   alias asin! asin
  30.   alias acos! acos
  31.   alias atan! atan
  32.   alias atan2! atan2

  33.   alias asinh! asinh
  34.   alias acosh! acosh
  35.   alias atanh! atanh

  36.   #--------------------------------------------------------------------------
  37.   # * exp(z)
  38.   #--------------------------------------------------------------------------
  39.   # Math::E raised to the z power
  40.   #--------------------------------------------------------------------------
  41.   def exp(z)
  42.     if z.real?
  43.       exp!(z)
  44.     else
  45.       ere = exp!(z.real)
  46.       Complex(ere * cos!(z.imag),
  47.               ere * sin!(z.imag))
  48.     end
  49.   end

  50.   #--------------------------------------------------------------------------
  51.   # * log(z, b=::Math::E)
  52.   #--------------------------------------------------------------------------
  53.   # Returns the natural logarithm of Complex. If a second argument is given,
  54.   # it will be the base of logarithm.
  55.   #--------------------------------------------------------------------------
  56.   def log(*args)
  57.     z, b = args
  58.     if z.real? and z >= 0 and (b.nil? or b >= 0)
  59.       log!(*args)
  60.     else
  61.       a = Complex(log!(z.abs), z.arg)
  62.       if b
  63.         a /= log(b)
  64.       end
  65.       a
  66.     end
  67.   end

  68.   #--------------------------------------------------------------------------
  69.   # * log2(z)
  70.   #--------------------------------------------------------------------------
  71.   # Returns the base 2 logarithm of z
  72.   #--------------------------------------------------------------------------
  73.   def log2(z)
  74.     if z.real? and z >= 0
  75.       log2!(z)
  76.     else
  77.       log(z) / log!(2)
  78.     end
  79.   end

  80.   #--------------------------------------------------------------------------
  81.   # * log10(z)
  82.   #--------------------------------------------------------------------------
  83.   # Returns the base 10 logarithm of z
  84.   #--------------------------------------------------------------------------
  85.   def log10(z)
  86.     if z.real? and z >= 0
  87.       log10!(z)
  88.     else
  89.       log(z) / log!(10)
  90.     end
  91.   end

  92.   #--------------------------------------------------------------------------
  93.   # * sqrt(z)
  94.   #--------------------------------------------------------------------------
  95.   # Returns the non-negative square root of Complex.
  96.   #--------------------------------------------------------------------------
  97.   def sqrt(z)
  98.     if z.real?
  99.       if z < 0
  100.         Complex(0, sqrt!(-z))
  101.       else
  102.         sqrt!(z)
  103.       end
  104.     else
  105.       if z.imag < 0 ||
  106.         (z.imag == 0 && z.imag.to_s[0] == '-')
  107.         sqrt(z.conjugate).conjugate
  108.       else
  109.         r = z.abs
  110.         x = z.real
  111.         Complex(sqrt!((r + x) / 2), sqrt!((r - x) / 2))
  112.       end
  113.     end
  114.   end

  115.   #--------------------------------------------------------------------------
  116.   # * cbrt(z)
  117.   #--------------------------------------------------------------------------
  118.   # Returns the principal value of the cube root of z
  119.   #--------------------------------------------------------------------------
  120.   def cbrt(z)
  121.     if z.real?
  122.       cbrt!(z)
  123.     else
  124.       Complex(z) ** (1.0/3)
  125.     end
  126.   end

  127.   #--------------------------------------------------------------------------
  128.   # * sin(z)
  129.   #--------------------------------------------------------------------------
  130.   # Returns the sine of z, where z is given in radians
  131.   #--------------------------------------------------------------------------
  132.   def sin(z)
  133.     if z.real?
  134.       sin!(z)
  135.     else
  136.       Complex(sin!(z.real) * cosh!(z.imag),
  137.               cos!(z.real) * sinh!(z.imag))
  138.     end
  139.   end

  140.   #--------------------------------------------------------------------------
  141.   # * cos(z)
  142.   #--------------------------------------------------------------------------
  143.   # Returns the cosine of z, where z is given in radians
  144.   #--------------------------------------------------------------------------
  145.   def cos(z)
  146.     if z.real?
  147.       cos!(z)
  148.     else
  149.       Complex(cos!(z.real) * cosh!(z.imag),
  150.               -sin!(z.real) * sinh!(z.imag))
  151.     end
  152.   end

  153.   #--------------------------------------------------------------------------
  154.   # * tan(z)
  155.   #--------------------------------------------------------------------------
  156.   # Returns the tangent of z, where z is given in radians
  157.   #--------------------------------------------------------------------------
  158.   def tan(z)
  159.     if z.real?
  160.       tan!(z)
  161.     else
  162.       sin(z) / cos(z)
  163.     end
  164.   end

  165.   #--------------------------------------------------------------------------
  166.   # * sinh(z)
  167.   #--------------------------------------------------------------------------
  168.   # Returns the hyperbolic sine of z, where z is given in radians
  169.   #--------------------------------------------------------------------------
  170.   def sinh(z)
  171.     if z.real?
  172.       sinh!(z)
  173.     else
  174.       Complex(sinh!(z.real) * cos!(z.imag),
  175.               cosh!(z.real) * sin!(z.imag))
  176.     end
  177.   end

  178.   #--------------------------------------------------------------------------
  179.   # * cos(z)
  180.   #--------------------------------------------------------------------------
  181.   # Returns the hyperbolic cosine of z, where z is given in radians
  182.   #--------------------------------------------------------------------------
  183.   def cosh(z)
  184.     if z.real?
  185.       cosh!(z)
  186.     else
  187.       Complex(cosh!(z.real) * cos!(z.imag),
  188.               sinh!(z.real) * sin!(z.imag))
  189.     end
  190.   end

  191.   #--------------------------------------------------------------------------
  192.   # * tanh(z)
  193.   #--------------------------------------------------------------------------
  194.   # Returns the hyperbolic tangent of z, where z is given in radians
  195.   #--------------------------------------------------------------------------
  196.   def tanh(z)
  197.     if z.real?
  198.       tanh!(z)
  199.     else
  200.       sinh(z) / cosh(z)
  201.     end
  202.   end

  203.   #--------------------------------------------------------------------------
  204.   # * asin(z)
  205.   #--------------------------------------------------------------------------
  206.   # Returns the arc sine of z
  207.   #--------------------------------------------------------------------------
  208.   def asin(z)
  209.     if z.real? and z >= -1 and z <= 1
  210.       asin!(z)
  211.     else
  212.       (-1.0).i * log(1.0.i * z + sqrt(1.0 - z * z))
  213.     end
  214.   end

  215.   #--------------------------------------------------------------------------
  216.   # * acos(z)
  217.   #--------------------------------------------------------------------------
  218.   # Returns the arc cosine of z
  219.   #--------------------------------------------------------------------------
  220.   def acos(z)
  221.     if z.real? and z >= -1 and z <= 1
  222.       acos!(z)
  223.     else
  224.       (-1.0).i * log(z + 1.0.i * sqrt(1.0 - z * z))
  225.     end
  226.   end

  227.   #--------------------------------------------------------------------------
  228.   # * atan(z)
  229.   #--------------------------------------------------------------------------
  230.   # Returns the arc tangent of z
  231.   #--------------------------------------------------------------------------
  232.   def atan(z)
  233.     if z.real?
  234.       atan!(z)
  235.     else
  236.       1.0.i * log((1.0.i + z) / (1.0.i - z)) / 2.0
  237.     end
  238.   end

  239.   #--------------------------------------------------------------------------
  240.   # * atan2(z)
  241.   #--------------------------------------------------------------------------
  242.   # returns the arc tangent of y divided by x using the signs of y and x to
  243.   # determine the quadrant
  244.   #--------------------------------------------------------------------------
  245.   def atan2(y,x)
  246.     if y.real? and x.real?
  247.       atan2!(y,x)
  248.     else
  249.       (-1.0).i * log((x + 1.0.i * y) / sqrt(x * x + y * y))
  250.     end
  251.   end

  252.   #--------------------------------------------------------------------------
  253.   # * asinh(z)
  254.   #--------------------------------------------------------------------------
  255.   # returns the inverse hyperbolic sine of z
  256.   #--------------------------------------------------------------------------
  257.   def asinh(z)
  258.     if z.real?
  259.       asinh!(z)
  260.     else
  261.       log(z + sqrt(1.0 + z * z))
  262.     end
  263.   end

  264.   #--------------------------------------------------------------------------
  265.   # * acosh(z)
  266.   #--------------------------------------------------------------------------
  267.   # returns the inverse hyperbolic cosine of z
  268.   #--------------------------------------------------------------------------
  269.   def acosh(z)
  270.     if z.real? and z >= 1
  271.       acosh!(z)
  272.     else
  273.       log(z + sqrt(z * z - 1.0))
  274.     end
  275.   end

  276.   #--------------------------------------------------------------------------
  277.   # * atanh(z)
  278.   #--------------------------------------------------------------------------
  279.   # returns the inverse hyperbolic tangent of z
  280.   #--------------------------------------------------------------------------
  281.   def atanh(z)
  282.     if z.real? and z >= -1 and z <= 1
  283.       atanh!(z)
  284.     else
  285.       log((1.0 + z) / (1.0 - z)) / 2.0
  286.     end
  287.   end

  288.   module_function :exp!
  289.   module_function :exp
  290.   module_function :log!
  291.   module_function :log
  292.   module_function :log2!
  293.   module_function :log2
  294.   module_function :log10!
  295.   module_function :log10
  296.   module_function :sqrt!
  297.   module_function :sqrt
  298.   module_function :cbrt!
  299.   module_function :cbrt

  300.   module_function :sin!
  301.   module_function :sin
  302.   module_function :cos!
  303.   module_function :cos
  304.   module_function :tan!
  305.   module_function :tan

  306.   module_function :sinh!
  307.   module_function :sinh
  308.   module_function :cosh!
  309.   module_function :cosh
  310.   module_function :tanh!
  311.   module_function :tanh

  312.   module_function :asin!
  313.   module_function :asin
  314.   module_function :acos!
  315.   module_function :acos
  316.   module_function :atan!
  317.   module_function :atan
  318.   module_function :atan2!
  319.   module_function :atan2

  320.   module_function :asinh!
  321.   module_function :asinh
  322.   module_function :acosh!
  323.   module_function :acosh
  324.   module_function :atanh!
  325.   module_function :atanh

  326.   module_function :frexp
  327.   module_function :ldexp
  328.   module_function :hypot
  329.   module_function :erf
  330.   module_function :erfc
  331.   module_function :gamma
  332.   module_function :lgamma

  333. end
复制代码
QQ截图20180909200109.png
QQ截图20180909200045.png
顺便一提,虽然绘制速度慢到让人吐血,但是由于不支持后台运行,请将焦点保持在窗口上,慢慢看着它画完;我也不是很清楚为什么RGD不支持这个
再顺便一提,要是RGD有更高的Ruby版本就好了,我就不用写Complex(1, 2),直接写1+2i了

评分

参与人数 1赞 +1 收起 理由
David_Exmachina + 1

查看全部评分

小仙女一枚~

可以吃的头衔

梦石
0
星屑
1015
在线时间
424 小时
注册时间
2010-10-17
回帖
586
发表于 2018-9-10 03:43:57 | 显示全部楼层
尝试用Shader完成这个效果把(效率会让你惊呆

点评

主要耗时不在绘制,而是计算。1024*768次循环的复杂的没有经过优化的计算,能不慢吗  发表于 2018-9-10 19:13
……
回复

使用道具 举报

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

本版积分规则

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

在本版发帖返回顶部