author | Francesco Abbate <francesco.bbt@gmail.com> | 2012年06月08日 15:36:59 +0200 |
---|---|---|
committer | Francesco Abbate <francesco.bbt@gmail.com> | 2012年06月08日 15:36:59 +0200 |
commit | d19b35f2417edb4936b642b1c5530a6dbd9b491c (patch) | |
tree | 8e96889cda21cb1ff1e7fa340a993815f76405de | |
parent | 2d17ffeb35f0cf8fdc38ca8f548983132d64cc0f (diff) | |
parent | c11ef40405dc2f2b9368b3016f482b0a3c567a5f (diff) | |
download | gsl-shell-d19b35f2417edb4936b642b1c5530a6dbd9b491c.tar.gz |
-rw-r--r-- | contour.lua | 6 | ||||
-rw-r--r-- | demos/contour.lua | 13 | ||||
-rw-r--r-- | graph-init.lua | 83 |
diff --git a/contour.lua b/contour.lua index 171a4f8c..1c3fc379 100644 --- a/contour.lua +++ b/contour.lua @@ -23,7 +23,7 @@ use 'math' local insert = table.insert -local default_color_map = graph.color_function('redyellow', 0.9) +local default_color_map = graph.color_function('redyellow', 1) local function reverse(ls) local k, n = 1, #ls @@ -819,6 +819,8 @@ function contour.plot(f, x1, y1, x2, y2, options) g.find_curves() local p = graph.plot() + p:add(graph.rect(x1, y1, x2, y2), 'black') + g.draw_regions(p) if opt 'lines' then g.draw_lines(p, 'black') end @@ -841,6 +843,8 @@ function contour.polar_plot(f, R, options) g.find_curves() local p = graph.plot() + p:add(graph.ellipse(0, 0, R, R), 'black') + g.draw_regions(p) if opt 'legend' then diff --git a/demos/contour.lua b/demos/contour.lua index d511ac73..2c4e6c69 100644 --- a/demos/contour.lua +++ b/demos/contour.lua @@ -3,7 +3,7 @@ use 'math' local function rosenbrock() -- rosenbrock function local f = function(x, y) return 100*(y-x^2)^2 + (1-x)^2 end - local N = 7 + local N = 9 local function frbeval(k) return f(1, 1 - 2 * (k/N)^2) end local ls = iter.ilist(frbeval, N) local p = contour.plot(f, -1.5, -0.5, 1.5, 2, {gridx= 80, gridy= 80, levels= ls}) @@ -18,16 +18,9 @@ local function sincos() end end - local function add_box_title(p, x1, x2, y1, y2, title) - local box = graph.rect(x1, y1, x2, y2) - p:addline(box, 'black') - p.units = false - p.title = title - end - local f = fsincos(0.1, 0.3) - local p1 = contour.plot(f, 0, 0, 4*pi, 4*pi, {gridx=60, gridy=60, show= false}) - add_box_title(p1, 0, 4*pi, 0, 4*pi, 'f(x,y) = cos(x) + cos(y) + 0.1x + 0.3y') + local p1 = contour.plot(f, -2*pi, -2*pi, 6*pi, 6*pi, {gridx=120, gridy=120, levels= 12, show= false}) + p1.title = 'f(x,y) = cos(x) + cos(y) + 0.1x + 0.3y' p1:show() return p1 end diff --git a/graph-init.lua b/graph-init.lua index 48f0f828..289e70ce 100644 --- a/graph-init.lua +++ b/graph-init.lua @@ -156,6 +156,13 @@ function graph.rect(x1, y1, x2, y2) return p end +local function rgba8(r, g, b, a) + local rb = band(lshift(r, 24), 0xff000000) + local gb = band(lshift(g, 16), 0xff0000 ) + local bb = band(lshift(b, 8 ), 0xff00 ) + return bor(rb, gb, bb, a and band(a, 0xff) or 0xff) +end + local function rgba(r, g, b, a) local rb = band(lshift(r*255, 24), 0xff000000) local gb = band(lshift(g*255, 16), 0xff0000 ) @@ -204,6 +211,39 @@ local bcolors = {'red', 'blue', 'green', 'magenta', 'cyan', 'yellow'} -- colors from a popular spreadsheet application local wcolors = {0x4f81bd, 0xc0504d, 0x9bbb59, 0x695185, 0x3c8da3, 0xcc7b38} +local hue_map = { + {231, 0, 0 }, + {231, 113, 0 }, + {231, 211, 0 }, + {156, 231, 0 }, + {0, 231, 33 }, + {0, 231, 156}, + {0, 195, 231}, + {0, 113, 231}, + {0, 0, 231}, + {132, 0, 231} +} + +local function hue_choose(k) + local e = hue_map[k] + local r, g, b = e[1], e[2], e[3] + return rgba8(r, g, b, 255) +end + +local function hue_color(p) + local x = 10 - p * 9 + local i = floor(x) + if i < 1 or i+1 > 10 then + return i < 1 and hue_choose(1) or hue_choose(10) + else + local e1, e2 = hue_map[i], hue_map[i+1] + local r = floor(e1[1] + (e2[1] - e1[1])*(x-i)) + local g = floor(e1[2] + (e2[2] - e1[2])*(x-i)) + local b = floor(e1[3] + (e2[3] - e1[3])*(x-i)) + return rgba8(r, g, b, 255) + end +end + function graph.rainbow(n) local p = #bcolors return graph.color[bcolors[(n-1) % p + 1]] @@ -216,7 +256,7 @@ end local color_schema = { bluish = {0.91, 0.898, 0.85, 0.345, 0.145, 0.6}, - redyellow = {1, 1, 0, 1, 0, 0}, + redyellow = {0.9, 0.9, 0, 0.9, 0, 0}, darkgreen = {0.9, 0.9, 0, 0, 0.4, 0} } @@ -229,46 +269,7 @@ function graph.color_function(schema, alpha) end end -local function HueToRgb(m1, m2, hue) - local v - hue = hue % 1 - - if 6 * hue < 1 then - v = m1 + (m2 - m1) * hue * 6 - elseif 2 * hue < 1 then - v = m2 - elseif 3 * hue < 2 then - v = m1 + (m2 - m1) * (2/3 - hue) * 6 - else - v = m1 - end - - return v -end - -function graph.hsl2rgb(h, s, l) - local m1, m2, hue - local r, g, b - - if s == 0 then - r, g, b = l, l, l - else - if l <= 0.5 then - m2 = l * (s + 1); - else - m2 = l + s - l * s - end - m1 = l * 2 - m2 - r = HueToRgb(m1, m2, h + 1/3) - g = HueToRgb(m1, m2, h) - b = HueToRgb(m1, m2, h - 1/3) - end - return graph.rgb(r, g, b) -end - -function graph.hue(a) - return graph.hsl2rgb(a*0.7, 1, 0.6) -end +graph.hue_color = hue_color function graph.plot_lines(ln, title) local p = graph.plot(title) |