Advertisement
Guest User

shark

a guest
Dec 13th, 2013
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.58 KB | None | 0 0
  1. local xratio = 10.0
  2. local yratio = 10.0
  3. local number = 10000
  4. local wmin = 1.0
  5. local hmin = 1.0
  6. local detalization = 1000
  7.  
  8. local wmax = wmin * xratio
  9. local hmax = hmin * yratio
  10.  
  11. local points = {}
  12. for i = 0, detalization do
  13.     local xi = i * wmax / detalization
  14.     table.insert(points, {xi, 0.0})
  15. end
  16. for i = 1, number do
  17.     local w = wmin + (wmax - wmin) * math.random()
  18.     local h = hmin + (hmax - hmin) * math.random()
  19.     local l = (wmax - w) * math.random()
  20.     local r = l + w
  21.     local mean = (l + r) / 2.0
  22.     for _, point in ipairs(points) do
  23.         if l <= point[1] and point[1] <= r then
  24.             local y
  25.             if point[1] < mean then
  26.                 local x = (point[1] - l) / (mean - l)
  27.                 y = math.sqrt((2.0 - x) * x)
  28.             else
  29.                 local x = (point[1] - mean) / (r - mean)
  30.                 y = 1.0 - math.sqrt((2.0 - x) * x)
  31.             end
  32.             point[2] = point[2] + y * h
  33.         end
  34.     end
  35. end
  36.  
  37. local gnuplot = io.popen('gnuplot', 'w')
  38. gnuplot:write('reset;\n')
  39. gnuplot:write('set terminal wxt 1;\n')
  40. gnuplot:write(string.format('set xrange [%f:%f];\n', 0.0, wmax))
  41. gnuplot:write(string.format('set yrange [%f:%f];\n', 0.0, hmax * number))
  42. gnuplot:write('set size square;\n')
  43. gnuplot:write(string.format('set xtics %f;\n', wmax / 10.0))
  44. gnuplot:write(string.format('set ytics %f;\n', hmax * number / 10.0))
  45. gnuplot:write('set grid xtics ytics;\n')
  46. gnuplot:write('plot "-" using 1:2 notitle with lines;\n')
  47. for _, point in ipairs(points) do
  48.     gnuplot:write(string.format('%f %f\n', point[1], point[2]))
  49. end
  50. gnuplot:write('e\n')
  51. gnuplot:flush();
  52.  
  53. os.execute('pause');
  54. gnuplot:write('exit\n');
  55. gnuplot:flush();
  56. gnuplot:close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement