/******************************************************************** * Functions for 2D plot * * Example: * import plot2d; plot2d(600, 600, 0, 1, .2, 0, 1, .2); line2d([0, 1], [0, 1]); show2d(); * ********************************************************************/ load("zegraph.dll"); __render = null; __scene = null; __plot = null; __font = null; __width = 512; __height = 512; __scale = 1.0; __x0 = null; __x1 = null; __y0 = null; __y1 = null; DEG_TO_RAD = 0.017453292519943; // degree to radius function plot2d(w=512, h=512, x0=-1, x1=1, dx=.5, y0=-1, y1=1, dy=.5) { assert(w > 32 && h >= 32); global:__x0 = x0; global:__x1 = x1; global:__y0 = y0; global:__y1 = y1; if (w > 800) __scale = w / 800.0; G = zegraph("render", "scene", "node", "plot", "font"); node = G.node; global:__render = G.render; global:__scene = G.scene; global:__plot = G.plot; global:__font = G.font; global:__width = w; global:__height = h; global:__render:size(w, h); global:__render:add(__scene); global:__scene:viewport(0, 0, w, h); global:__scene:root(node); node:add(__plot); __plot:font(__font, 10*__scale); __plot:xaxis(0, -1, 0); __plot:yaxis(-1, 0, 0); xaxis = __plot:xaxis(); xaxis:range(x0, x1); xaxis:title("X"); xaxis:tickmarks(x0, dx, 0); G.xaxis = xaxis; yaxis = __plot:yaxis(); yaxis:range(y0, y1); yaxis:title("Y"); yaxis:tickmarks(y0, dy, 0); G.yaxis = yaxis; return G; } function line2d(x, y) { line = zegraph("line"); vert = zegraph("vertex"); line:vertex(vert); line:type("strip"); line:solid(1.5*__scale); __plot:add(line); n = size(x); for (i = 0; i < n; i++) vert:add(x[i], y[i], 0); return line; } function frame2d(x, y, fill=false) { poly = zegraph("polygon"); vert = zegraph("vertex"); poly:vertex(vert); poly:type("quads"); __plot:add(poly); n = size(x); assert(n >= 4); vert:add(x[0], y[0], 0, x[1], y[1], 0, x[2], y[2], 0, x[3], y[3], 0); poly:fill(-1); if (fill) poly:fill(1); return poly; } function point2d(x, y) { point = zegraph("point"); vert = zegraph("vertex"); point:vertex(vert); point:size(5*__scale); __plot:add(point); if (isarray(x)) { n = size(x); for (i = 0; i < n; i++) vert:add(x[i], y[i], 0); } else { vert:add(x, y, 0); } return point; } function symbol2d(x, y, symbol) { if (isarray(x)) { n = size(x); for (i = 0; i < n; i++) __plot:anchor(symbol, x[i], y[i], 0); } else { __plot:anchor(symbol, x, y, 0); } } function bar2d(x, y, w, h) { poly = zegraph("polygon"); vert = zegraph("vertex"); poly:type("quads"); poly:vertex(vert); p1 = __plot:global(__width, __height, x, y, 0); p2 = __plot:global(__width, __height, x + w, y, 0); p3 = __plot:global(__width, __height, x, y + h, 0); w = (p2[0] - p1[0]) / 2; h = (p3[1] - p1[1]); vert:add(-w, 0, 0, w, 0, 0, w, h, 0, -w, h, 0); __plot:anchor(poly, x, y, 0); return poly; } function text2d(x, y, str, halign=0, valign=-1) { text = zegraph("text"); text:font(__font, 10*__scale); text:string(str); size = text:size(); h = -size[0]/2; v = -size[1]/2; if (halign < 0) h = 0; if (halign > 0) h = -size[0]; if (valign < 0) v = 0; if (valign > 0) v = -size[1]; text:layout(h, v, 0, h + 1, v, 0, h, v + 1, 0); __plot:anchor(text, x, y, 0); } function arrow2d(x, y, u, uscale, v, vscale, size=5) { line = zegraph("line"); vert = zegraph("vertex"); line:type("lines"); line:vertex(vert); line:solid(1.5*__scale); a = __plot:local(__width, __height, 0, 0, 0); __plot:anchor(line, a[0], a[1], 0); a = __plot:global(__width, __height, x, y, 0); x1 = a[0]; y1 = a[1]; a = __plot:global(__width, __height, x + u*uscale, y + v*vscale, 0); x2 = a[0]; y2 = a[1]; vert:add(x1, y1, 0, x2, y2, 0); ax = size; ay = size / 3.0; a = direction(x1, y1, 0, x2, y2, 0); cosa = cos((a[1] + 180) * DEG_TO_RAD); sina = sin((a[1] + 180) * DEG_TO_RAD); x1 = x2 + ax * cosa - ay * sina; y1 = y2 + ax * sina + ay * cosa; vert:add(x1, y1, 0, x2, y2, 0); ay = -ay; x1 = x2 + ax * cosa - ay * sina; y1 = y2 + ax * sina + ay * cosa; vert:add(x1, y1, 0, x2, y2, 0); return line; } function image2d(x1, y1, x2, y2, fname) { node = zegraph("node"); tex = zegraph("texture"); poly = zegraph("polygon"); vert = zegraph("vertex"); st = zegraph("texcoord"); node:closed(true); node:add(tex, poly); tex:image(fname, -1, -1, -1); poly:type("quads"); poly:color(1, 1, 1); poly:vertex(vert); poly:texcoord(st); vert:add(x1, y1, -1, x2, y1, -1, x2, y2, -1, x1, y2, -1); st:add(0, 0, 1, 0, 1, 1, 0, 1); __plot:add(node); } function show2d() { size = __render:size(); window(size[0], size[1], "callback"); function callback(hwnd, msg, wp, hwp, lwp, lp, hlp, llp) { if (msg == "PAINT") __render:towindow(hwnd); } } function scale2d(xscale=1, yscale=1) { __plot:scale(sx, sy, 1); } function move2d(dx, dy) { __plot:translate(dx, dy, 0); } function coast2d(gshhs, z=0) { x1 = __x0; x2 = __x1; if (__x0 < 0) { x1 += 180; x2 += 180; } data = matrix("double"); data:readgshhs(gshhs, "land", x1, x2, __y0, __y1); data:insert(2, z); if (__x0 < 0) { v1 = data[null,0]; idx = v1 >= 180; v2 = v1*0; v2[idx] = v1; v2 -= 360; v1[idx] = v2; data[null,0] = v1; } xyz = zegraph("vertex"); p = data:ptr(); xyz:add(p[0], p[1]); coast = zegraph("line"); coast:vertex(xyz); coast:type("lines"); coast:solid(__scale); __plot:add(coast); return coast; }