Annotation of OpenXM_contrib2/asir2000/lib/glib, Revision 1.5
1.5 ! takayama 1: /* $OpenXM: OpenXM_contrib2/asir2000/lib/glib,v 1.4 2002/07/10 08:40:58 takayama Exp $ */
1.1 takayama 2: /* $Id: drill,v 1.9 2000/11/20 01:44:49 taka Exp $ */
3:
4:
5: /* #define CLIP 1 */
6: /* #define DEBUG 1 */
7: #define LIST 4
8:
1.5 ! takayama 9: def glib_floor(N) {
1.1 takayama 10: return(pari(floor,N));
11: }
12:
1.5 ! takayama 13: /*
1.1 takayama 14: def join(A,B) {
15: if (type(B) == 0) return(A);
16: return(append(A,B));
17: }
18: def eigenvalues(M) {
19: return(pari(eigen,M));
20: }
21: def roots(F) {
22: return(pari(roots,F));
23: }
1.5 ! takayama 24: */
1.1 takayama 25:
26: /* ---------------------------------------------- */
27: def ox_get_errors(P) {
28: ox_push_cmd(P,276);
29: return(ox_pop_cmo(P));
30: }
31:
32: def reset_plot() {
33: extern Glib_process;
34: print(" Sending signal to ox_plot",0);
35: ox_reset(Glib_process);
36: print(" Done.");
37: }
1.5 ! takayama 38: Glib_ps = 0$
! 39: Glib_h = []$
1.1 takayama 40: Glib_canvas_x = 400$
41: Glib_canvas_y = 400$
42: Glib_xmin=0$ Glib_xmax=Glib_canvas_x$
43: Glib_ymin=0$ Glib_ymax=Glib_canvas_y$
44: #define START_SERVER \
45: extern Glib_server_started, Glib_process$ \
46: if (type(Glib_server_started) == 0) { \
47: Glib_process = ox_launch_nox(0,"ox_plot")$ \
48: register_handler(reset_plot); \
49: Glib_server_started = 1$ \
50: }$
51:
52: Glib_canvas = -1$
53: Glib_xmag = 1$ Glib_ymag=1$ Glib_xoffset=0$ Glib_yoffset=0$
54: Glib_safe_mode = 2 $
55: Glib_counter = 100$
56:
57: def open_Canvas(P,S) {
58: open_canvas(P,S);
59: R = ox_pop_cmo(P);
60: /*
61: if (ox_get_errors(P) == []) {
62: R = ox_pop_cmo(P);
63: }else{
64: debug;
65: }
66: */
67: return(R);
68: }
69:
70: def glib_open() {
71: extern Glib_canvas_x, Glib_canvas_y,
72: Glib_process, Glib_canvas;
73: START_SERVER
74: Glib_canvas = open_Canvas(Glib_process,[Glib_canvas_x,Glib_canvas_y])$
75: glib_check_strict();
76: return(Glib_canvas);
77: }
78:
79: def glib_check() {
80: extern Glib_process, Glib_canvas, Glib_safe_mode,
81: Glib_canvas_x, Glib_canvas_y, Glib_counter;
82: if (Glib_safe_mode == 0) {
83: return(0);
84: }
85: if (Glib_safe_mode == 2) {
86: if (Glib_counter > 0) {
87: Glib_counter--;
88: return(0);
89: }else{
90: Glib_counter=100;
91: }
92: }
93: glib_check_strict();
94: }
95: def glib_check_strict() {
96: extern Glib_process, Glib_canvas, Glib_safe_mode,
97: Glib_canvas_x, Glib_canvas_y, Glib_counter;
98: if (Glib_canvas < 0) {
99: glib_open();
100: }
101: E = ox_get_errors(Glib_process);
102: if (E != []) {
103: ox_pops(Glib_process,200);
104: print(E);
105: print("Warning: ",0);
106: print("Drawing canvas seems to be closed.");
107: print("Opening a new canvas.");
108: Glib_canvas = open_Canvas(Glib_process,[Glib_canvas_x,Glib_canvas_y])$
109: error("Drawing aborted");
110: }
111: }
112:
113: def glib_clear() {
114: extern Glib_process, Glib_canvas;
115: if (Glib_canvas < 0) glib_open();
116: clear_canvas(Glib_process,Glib_canvas);
117: }
118:
1.5 ! takayama 119: /*&usage begin: glib_window(Xmin,Ymin,Xmax,Ymax)
! 120: It generates a window with the left top corner [{Xmin},{Ymin}] and
! 121: the right bottom corner [{Xmax},{Ymax}].
! 122: example: glib_window(-1,-1,10,10);
! 123: end: */
1.1 takayama 124: def glib_window(Xmin,Ymin,Xmax,Ymax) {
125: extern Glib_xmin, Glib_xmax, Glib_ymin, Glib_ymax,
126: Glib_canvas_x, Glib_canvas_y, Glib_process, Glib_canvas,
127: Glib_xoffset, Glib_yoffset, Glib_xmag, Glib_ymag;
128: if (Xmax <= Xmin) error("glib window: Invalid size");
129: if (Ymax <= Ymin) error("glib window: Invalid size");
1.2 takayama 130: glib_check_arg(Xmin,Ymin); glib_check_arg(Xmax,Ymax);
1.1 takayama 131: Glib_xmin = Xmin; Glib_xmax = Xmax;
132: Glib_ymin = Ymin; Glib_ymax = Ymax;
133: Glib_xoffset = -Xmin;
134: Glib_yoffset = -Ymin;
135: Glib_xmag = Glib_canvas_x/(Xmax-Xmin);
136: Glib_ymag = Glib_canvas_y/(Ymax-Ymin);
137: if (Glib_canvas < 0) glib_open();
138: }
139:
1.2 takayama 140: def glib_check_arg(X,Y) {
141: if (type(X) <= 1 && type(Y) <= 1) return 1;
142: else {
143: print("Error in glib: arguments ",0);
144: print([X,Y],0);
145: print(" are not numbers.");
146: error("Invalid argument for glib_window, glib_putpixel, glib_line.");
147: }
148: }
149:
1.5 ! takayama 150: /*&usage begin: glib_putpixel(X,Y | color)
! 151: It puts a pixel at [{X},{Y}] with {color}
! 152: example: glib_putpixel(1,2 | color=0xffff00);
! 153: end: */
1.1 takayama 154: def glib_putpixel(X,Y) {
155: extern Glib_process, Glib_canvas,
1.5 ! takayama 156: Glib_xoffset, Glib_yoffset, Glib_xmag, Glib_ymag, Glib_ps;
1.1 takayama 157: if (Glib_canvas < 0) glib_open();
158: glib_check();
1.2 takayama 159: glib_check_arg(X,Y);
1.3 noro 160: C = getopt(color);
1.5 ! takayama 161: Pos = [glib_floor(Glib_xmag*(X+Glib_xoffset)),
! 162: glib_floor(Glib_ymag*(Y+Glib_yoffset))];
! 163: if ( type(C) != -1 ) {
! 164: draw_obj(Glib_process,Glib_canvas,Pos,C);
! 165: if (Glib_ps) glib_history(["putpixel",Pos,C]);
! 166: }else{
! 167: draw_obj(Glib_process,Glib_canvas,Pos);
! 168: if (Glib_ps) glib_history(["putpixel",Pos,0]);
! 169: }
1.1 takayama 170: }
171:
1.5 ! takayama 172: /*&usage begin: glib_line(X0,Y0,X1,Y1 | color)
! 173: It draw the line [{X0},{Y0}]-- [{X1},{Y1}]with {color}
! 174: example: glib_line(0,0,5,3/2 | color=0xff00ff);
! 175: end: */
1.1 takayama 176: def glib_line(X0,Y0,X1,Y1) {
177: extern Glib_xmag, Glib_ymag, Glib_xoffset, Glib_yoffset;
1.3 noro 178: C = getopt(color);
1.2 takayama 179: glib_check_arg(X0,Y0);
180: glib_check_arg(X1,Y1);
1.5 ! takayama 181: glib_clip_line(glib_floor(Glib_xmag*(X0+Glib_xoffset)),
! 182: glib_floor(Glib_ymag*(Y0+Glib_yoffset)),
! 183: glib_floor(Glib_xmag*(X1+Glib_xoffset)),
! 184: glib_floor(Glib_ymag*(Y1+Glib_yoffset)),C);
1.1 takayama 185: }
186:
1.3 noro 187: def glib_clip_line(X0,Y0,X1,Y1,Color) {
1.1 takayama 188: /* X0, Y0, X1, Y1 should be integers.
189: Coordinates are already translated. */
1.5 ! takayama 190: extern Glib_process, Glib_canvas, Glib_canvas_x, Glib_canvas_y,
! 191: Glib_ps;
1.1 takayama 192: if (Glib_canvas < 0) glib_open();
193:
1.5 ! takayama 194: if (Glib_ps) {
1.1 takayama 195: #ifdef DEBUG
196: print(["clip_line",[X0,Y0,X1,Y1]]);
197: #endif
198: /* clip by x = 0 */
199: S = glib_clip0_x(X0,Y0,X1,Y1,0);
200: #ifdef DEBUG
201: print(["clip0_x",S]);
202: #endif
203: if (type(S) == 0) return;
204: X0 = S[0]; Y0 = S[1]; X1 = S[2]; Y1 = S[3];
205:
206: S = glib_clip1_x(X0,Y0,X1,Y1,Glib_canvas_x-1);
207: #ifdef DEBUG
208: print(["clip1_x",S]);
209: #endif
210: if (type(S) == 0) return;
211: X0 = S[0]; Y0 = S[1]; X1 = S[2]; Y1 = S[3];
212:
213: S = glib_clip0_y(X0,Y0,X1,Y1,0);
214: #ifdef DEBUG
215: print(["clip0_y",S]);
216: #endif
217: if (type(S) == 0) return;
218: X0 = S[0]; Y0 = S[1]; X1 = S[2]; Y1 = S[3];
219:
220: S = glib_clip1_y(X0,Y0,X1,Y1,Glib_canvas_y-1);
221: #ifdef DEBUG
222: print(["clip1_y",S]);
223: #endif
224: if (type(S) == 0) return;
225: X0 = S[0]; Y0 = S[1]; X1 = S[2]; Y1 = S[3];
226:
227: #ifdef DEBUG
228: print([X0,Y0,X1,Y1]);
229: #endif
1.5 ! takayama 230: }
! 231:
1.1 takayama 232: glib_check();
1.5 ! takayama 233: Pos = [glib_floor(X0),glib_floor(Y0),glib_floor(X1),glib_floor(Y1)];
! 234: if ( type(Color) != -1 ) {
! 235: draw_obj(Glib_process,Glib_canvas,Pos,Color);
! 236: if (Glib_ps) glib_history(["line",Pos,Color]);
! 237: }else{
! 238: draw_obj(Glib_process,Glib_canvas,Pos);
! 239: if (Glib_ps) glib_history(["line",Pos,0]);
! 240: }
1.1 takayama 241: }
242:
243: def glib_clip0_x(X0,Y0,X1,Y1,Clip) {
244: if (X0 < Clip && X1 < Clip) return(0);
245: if (X0 < Clip && X1 > Clip) {
246: return([Clip, Y0+(Clip-X0)*(Y1-Y0)/(X1-X0),X1,Y1]);
247: }
248: if (X1 > Clip && X1 < Clip) {
249: return([X0,Y0,Clip, Y1+(Clip-X1)*(Y0-Y1)/(X0-X1)]);
250: }
251: return([X0,Y0,X1,Y1]);
252: }
253: def glib_clip0_y(X0,Y0,X1,Y1,Clip) {
254: if (Y0 < Clip && Y1 < Clip) return(0);
255: if (Y0 < Clip && Y1 > Clip) {
256: return([X0+(Clip-Y0)*(X1-X0)/(Y1-Y0),Clip,X1,Y1]);
257: }
258: if (Y1 > Clip && Y1 < Clip) {
259: return([X0,Y0,X1+(Clip-Y1)*(X0-X1)/(Y0-Y1),Clip]);
260: }
261: return([X0,Y0,X1,Y1]);
262: }
263: def glib_clip1_x(X0,Y0,X1,Y1,Clip) {
264: if (X0 > Clip && X1 > Clip) return(0);
265: if (X0 > Clip && X1 < Clip) {
266: return([Clip, Y0+(Clip-X0)*(Y1-Y0)/(X1-X0),X1,Y1]);
267: }
268: if (X1 < Clip && X1 > Clip) {
269: return([X0,Y0,Clip, Y1+(Clip-X1)*(Y0-Y1)/(X0-X1)]);
270: }
271: return([X0,Y0,X1,Y1]);
272: }
273: def glib_clip1_y(X0,Y0,X1,Y1,Clip) {
274: if (Y0 > Clip && Y1 > Clip) return(0);
275: if (Y0 > Clip && Y1 < Clip) {
276: return([X0+(Clip-Y0)*(X1-X0)/(Y1-Y0),Clip,X1,Y1]);
277: }
278: if (Y0 < Clip && Y1 > Clip) {
279: return([X0,Y0,X1+(Clip-Y1)*(X0-X1)/(Y0-Y1),Clip]);
280: }
281: return([X0,Y0,X1,Y1]);
1.4 takayama 282: }
283:
284: /*&usage begin:glib_print(X,Y,Text|color)
285: It put a string {Text} at [{X},{Y}] on the glib canvas.
286: example: glib_print(100,100,"Hello Worlds" | color=0xff0000);
287: end: */
288: def glib_print(X,Y,Text) {
289: extern Glib_process, Glib_canvas,
1.5 ! takayama 290: Glib_xoffset, Glib_yoffset, Glib_xmag, Glib_ymag, Glib_ps;
1.4 takayama 291: if (Glib_canvas < 0) glib_open();
292: glib_check();
293: glib_check_arg(X,Y);
294: if (type(Text) != 7) error("glib_print(X,Y,Text): Text must be a string.");
295: C = getopt(color);
1.5 ! takayama 296: Pos = [glib_floor(Glib_xmag*(X+Glib_xoffset)),
! 297: glib_floor(Glib_ymag*(Y+Glib_yoffset))];
! 298: if ( type(C) != -1 ) {
! 299: draw_string(Glib_process,Glib_canvas,Pos,Text,C);
! 300: if (Glib_ps) glib_history(["print",Pos,Text,C]);
! 301: }else{
! 302: draw_string(Glib_process,Glib_canvas,Pos,Text);
! 303: if (Glib_ps) glib_history(["print",Pos,Text,0]);
! 304: }
! 305: }
! 306:
! 307: def glib_history(L) {
! 308: extern Glib_h, Glib_canvas_x, Glib_canvas_y;
! 309: if (L[0] == "putpixel" || L[0] == "print") {
! 310: if (L[1][0] <= Glib_canvas_x && L[1][0] >= 0) {
! 311: if (L[1][1] <= Glib_canvas_x && L[1][1] >= 0)
! 312: Glib_h = cons(L,Glib_h);
! 313: }
! 314: }else {
! 315: Glib_h = cons(L,Glib_h);
! 316: }
! 317: return 0;
! 318: }
! 319:
! 320: /*&usage begin:glib_tops()
! 321: If Glib_ps is set to 1,
! 322: it returns a postscript program to draw the picture on the canvas.
! 323: end: */
! 324: def glib_tops() {
! 325: extern Glib_h;
! 326: return glib_ps(Glib_h);
! 327: }
! 328: def glib_ps(L) {
! 329: PS = "";
! 330: Prev_color = 0;
! 331: /* Prolog */
! 332: PS += "%%!PS-Adobe-1.0\n";
! 333: PS += "%%BoundingBox: 0 0 " +
! 334: rtostr(Glib_canvas_x) + " " + rtostr(Glib_canvas_y) + "\n";
! 335: PS += "%%Creator: This is generated by ifplot\n";
! 336: PS += "%%Title: ifplot\n";
! 337: PS += "%%EndComments: \n";
! 338: PS += "0.1 setlinewidth \n";
! 339: PS += "2 setlinecap \n";
! 340: PS += "2 setlinejoin \n";
! 341: PS += "/ifplot_putpixel { \n";
! 342: PS += " /yyy 2 1 roll def /xxx 2 1 roll def \n";
! 343: PS += " gsave newpath xxx yyy .5 0 360 arc \n";
! 344: PS += " fill grestore \n";
! 345: PS += "} def \n";
! 346:
! 347: L = reverse(L);
! 348: N = length(L);
! 349: for (I=0; I<N; I++) {
! 350: C = L[I];
! 351: if (C[length(C)-1] != Prev_color) {
! 352: Prev_color = C[length(C)-1];
! 353: PS += rtostr(deval(ishift(Prev_color,16)/256)) + " " +
! 354: rtostr(deval(iand(ishift(Prev_color,8),0xff)/256)) + " " +
! 355: rtostr(deval(iand(Prev_color,0xff)/256)) + " setrgbcolor \n";
! 356: }
! 357: if (C[0] == "putpixel") {
! 358: PS += rtostr(C[1][0]) + " " + rtostr(C[1][1]) + " ifplot_putpixel \n";
! 359: }
! 360: if (C[0] == "line") {
! 361: PS += " newpath ";
! 362: PS += rtostr(C[1][0]) + " " + rtostr(C[1][1]) + " moveto " +
! 363: rtostr(C[1][2]) + " " + rtostr(C[1][3]) + " lineto stroke \n";
! 364: }
! 365: if (C[0] == "print") {
! 366: PS += "/Times-Roman findfont 10 scalefont setfont \n";
! 367: PS += rtostr(C[1][0]) + " " + rtostr(C[1][1]) + " moveto ";
! 368: PS += "(" + C[2] + ") show \n";
! 369: }
! 370: }
! 371:
! 372: /* Epilog */
! 373: PS += "0 0 0 setrgbcolor \n";
! 374: PS += "showpage \n";
! 375:
! 376: return PS;
1.1 takayama 377: }
378: end$
379:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>