Annotation of OpenXM_contrib2/asir2000/lib/glib, Revision 1.4
1.4 ! takayama 1: /* $OpenXM: OpenXM_contrib2/asir2000/lib/glib,v 1.3 2002/06/07 06:00:28 noro Exp $ */
1.1 takayama 2: /* $Id: drill,v 1.9 2000/11/20 01:44:49 taka Exp $ */
3:
4: /* Todo:
5: 1. Some functions should be moved to longname
6: */
7:
8: /* #define CLIP 1 */
9: /* #define DEBUG 1 */
10: #define LIST 4
11:
12: /* General sugar functions */
13: def floor(N) {
14: return(pari(floor,N));
15: }
16:
17: def join(A,B) {
18: if (type(B) == 0) return(A);
19: return(append(A,B));
20: }
21:
22: def eigenvalues(M) {
23: return(pari(eigen,M));
24: }
25:
26: def roots(F) {
27: return(pari(roots,F));
28: }
29:
30: /* ---------------------------------------------- */
31: def ox_get_errors(P) {
32: ox_push_cmd(P,276);
33: return(ox_pop_cmo(P));
34: }
35:
36: def reset_plot() {
37: extern Glib_process;
38: print(" Sending signal to ox_plot",0);
39: ox_reset(Glib_process);
40: print(" Done.");
41: }
42: Glib_canvas_x = 400$
43: Glib_canvas_y = 400$
44: Glib_xmin=0$ Glib_xmax=Glib_canvas_x$
45: Glib_ymin=0$ Glib_ymax=Glib_canvas_y$
46: #define START_SERVER \
47: extern Glib_server_started, Glib_process$ \
48: if (type(Glib_server_started) == 0) { \
49: Glib_process = ox_launch_nox(0,"ox_plot")$ \
50: register_handler(reset_plot); \
51: Glib_server_started = 1$ \
52: }$
53:
54: Glib_canvas = -1$
55: Glib_xmag = 1$ Glib_ymag=1$ Glib_xoffset=0$ Glib_yoffset=0$
56: Glib_safe_mode = 2 $
57: Glib_counter = 100$
58:
59: def open_Canvas(P,S) {
60: open_canvas(P,S);
61: R = ox_pop_cmo(P);
62: /*
63: if (ox_get_errors(P) == []) {
64: R = ox_pop_cmo(P);
65: }else{
66: debug;
67: }
68: */
69: return(R);
70: }
71:
72: def glib_open() {
73: extern Glib_canvas_x, Glib_canvas_y,
74: Glib_process, Glib_canvas;
75: START_SERVER
76: Glib_canvas = open_Canvas(Glib_process,[Glib_canvas_x,Glib_canvas_y])$
77: glib_check_strict();
78: return(Glib_canvas);
79: }
80:
81: def glib_check() {
82: extern Glib_process, Glib_canvas, Glib_safe_mode,
83: Glib_canvas_x, Glib_canvas_y, Glib_counter;
84: if (Glib_safe_mode == 0) {
85: return(0);
86: }
87: if (Glib_safe_mode == 2) {
88: if (Glib_counter > 0) {
89: Glib_counter--;
90: return(0);
91: }else{
92: Glib_counter=100;
93: }
94: }
95: glib_check_strict();
96: }
97: def glib_check_strict() {
98: extern Glib_process, Glib_canvas, Glib_safe_mode,
99: Glib_canvas_x, Glib_canvas_y, Glib_counter;
100: if (Glib_canvas < 0) {
101: glib_open();
102: }
103: E = ox_get_errors(Glib_process);
104: if (E != []) {
105: ox_pops(Glib_process,200);
106: print(E);
107: print("Warning: ",0);
108: print("Drawing canvas seems to be closed.");
109: print("Opening a new canvas.");
110: Glib_canvas = open_Canvas(Glib_process,[Glib_canvas_x,Glib_canvas_y])$
111: error("Drawing aborted");
112: }
113: }
114:
115: def glib_clear() {
116: extern Glib_process, Glib_canvas;
117: if (Glib_canvas < 0) glib_open();
118: clear_canvas(Glib_process,Glib_canvas);
119: }
120:
121: def glib_window(Xmin,Ymin,Xmax,Ymax) {
122: extern Glib_xmin, Glib_xmax, Glib_ymin, Glib_ymax,
123: Glib_canvas_x, Glib_canvas_y, Glib_process, Glib_canvas,
124: Glib_xoffset, Glib_yoffset, Glib_xmag, Glib_ymag;
125: if (Xmax <= Xmin) error("glib window: Invalid size");
126: if (Ymax <= Ymin) error("glib window: Invalid size");
1.2 takayama 127: glib_check_arg(Xmin,Ymin); glib_check_arg(Xmax,Ymax);
1.1 takayama 128: Glib_xmin = Xmin; Glib_xmax = Xmax;
129: Glib_ymin = Ymin; Glib_ymax = Ymax;
130: Glib_xoffset = -Xmin;
131: Glib_yoffset = -Ymin;
132: Glib_xmag = Glib_canvas_x/(Xmax-Xmin);
133: Glib_ymag = Glib_canvas_y/(Ymax-Ymin);
134: if (Glib_canvas < 0) glib_open();
135: }
136:
1.2 takayama 137: def glib_check_arg(X,Y) {
138: if (type(X) <= 1 && type(Y) <= 1) return 1;
139: else {
140: print("Error in glib: arguments ",0);
141: print([X,Y],0);
142: print(" are not numbers.");
143: error("Invalid argument for glib_window, glib_putpixel, glib_line.");
144: }
145: }
146:
1.1 takayama 147: def glib_putpixel(X,Y) {
148: extern Glib_process, Glib_canvas,
149: Glib_xoffset, Glib_yoffset, Glib_xmag, Glib_ymag;
150: if (Glib_canvas < 0) glib_open();
151: glib_check();
1.2 takayama 152: glib_check_arg(X,Y);
1.3 noro 153: C = getopt(color);
154: if ( type(C) != -1 )
155: draw_obj(Glib_process,Glib_canvas,
156: [floor(Glib_xmag*(X+Glib_xoffset)),
157: floor(Glib_ymag*(Y+Glib_yoffset))],C);
158: else
159: draw_obj(Glib_process,Glib_canvas,
1.1 takayama 160: [floor(Glib_xmag*(X+Glib_xoffset)),
161: floor(Glib_ymag*(Y+Glib_yoffset))]);
162: }
163:
164: def glib_line(X0,Y0,X1,Y1) {
165: extern Glib_xmag, Glib_ymag, Glib_xoffset, Glib_yoffset;
1.3 noro 166: C = getopt(color);
1.2 takayama 167: glib_check_arg(X0,Y0);
168: glib_check_arg(X1,Y1);
1.1 takayama 169: glib_clip_line(floor(Glib_xmag*(X0+Glib_xoffset)),
170: floor(Glib_ymag*(Y0+Glib_yoffset)),
171: floor(Glib_xmag*(X1+Glib_xoffset)),
1.3 noro 172: floor(Glib_ymag*(Y1+Glib_yoffset)),C);
1.1 takayama 173: }
174:
1.3 noro 175: def glib_clip_line(X0,Y0,X1,Y1,Color) {
1.1 takayama 176: /* X0, Y0, X1, Y1 should be integers.
177: Coordinates are already translated. */
178: extern Glib_process, Glib_canvas, Glib_canvas_x, Glib_canvas_y;
179: if (Glib_canvas < 0) glib_open();
180:
181: #ifdef CLIP
182: #ifdef DEBUG
183: print(["clip_line",[X0,Y0,X1,Y1]]);
184: #endif
185: /* clip by x = 0 */
186: S = glib_clip0_x(X0,Y0,X1,Y1,0);
187: #ifdef DEBUG
188: print(["clip0_x",S]);
189: #endif
190: if (type(S) == 0) return;
191: X0 = S[0]; Y0 = S[1]; X1 = S[2]; Y1 = S[3];
192:
193: S = glib_clip1_x(X0,Y0,X1,Y1,Glib_canvas_x-1);
194: #ifdef DEBUG
195: print(["clip1_x",S]);
196: #endif
197: if (type(S) == 0) return;
198: X0 = S[0]; Y0 = S[1]; X1 = S[2]; Y1 = S[3];
199:
200: S = glib_clip0_y(X0,Y0,X1,Y1,0);
201: #ifdef DEBUG
202: print(["clip0_y",S]);
203: #endif
204: if (type(S) == 0) return;
205: X0 = S[0]; Y0 = S[1]; X1 = S[2]; Y1 = S[3];
206:
207: S = glib_clip1_y(X0,Y0,X1,Y1,Glib_canvas_y-1);
208: #ifdef DEBUG
209: print(["clip1_y",S]);
210: #endif
211: if (type(S) == 0) return;
212: X0 = S[0]; Y0 = S[1]; X1 = S[2]; Y1 = S[3];
213:
214: #ifdef DEBUG
215: print([X0,Y0,X1,Y1]);
216: #endif
217: #endif
218: glib_check();
1.3 noro 219: if ( type(Color) != -1 )
220: draw_obj(Glib_process,Glib_canvas,
221: [floor(X0),floor(Y0),floor(X1),floor(Y1)],Color);
222: else
223: draw_obj(Glib_process,Glib_canvas,
1.1 takayama 224: [floor(X0),floor(Y0),floor(X1),floor(Y1)]);
225: }
226:
227: def glib_clip0_x(X0,Y0,X1,Y1,Clip) {
228: if (X0 < Clip && X1 < Clip) return(0);
229: if (X0 < Clip && X1 > Clip) {
230: return([Clip, Y0+(Clip-X0)*(Y1-Y0)/(X1-X0),X1,Y1]);
231: }
232: if (X1 > Clip && X1 < Clip) {
233: return([X0,Y0,Clip, Y1+(Clip-X1)*(Y0-Y1)/(X0-X1)]);
234: }
235: return([X0,Y0,X1,Y1]);
236: }
237: def glib_clip0_y(X0,Y0,X1,Y1,Clip) {
238: if (Y0 < Clip && Y1 < Clip) return(0);
239: if (Y0 < Clip && Y1 > Clip) {
240: return([X0+(Clip-Y0)*(X1-X0)/(Y1-Y0),Clip,X1,Y1]);
241: }
242: if (Y1 > Clip && Y1 < Clip) {
243: return([X0,Y0,X1+(Clip-Y1)*(X0-X1)/(Y0-Y1),Clip]);
244: }
245: return([X0,Y0,X1,Y1]);
246: }
247: def glib_clip1_x(X0,Y0,X1,Y1,Clip) {
248: if (X0 > Clip && X1 > Clip) return(0);
249: if (X0 > Clip && X1 < Clip) {
250: return([Clip, Y0+(Clip-X0)*(Y1-Y0)/(X1-X0),X1,Y1]);
251: }
252: if (X1 < Clip && X1 > Clip) {
253: return([X0,Y0,Clip, Y1+(Clip-X1)*(Y0-Y1)/(X0-X1)]);
254: }
255: return([X0,Y0,X1,Y1]);
256: }
257: def glib_clip1_y(X0,Y0,X1,Y1,Clip) {
258: if (Y0 > Clip && Y1 > Clip) return(0);
259: if (Y0 > Clip && Y1 < Clip) {
260: return([X0+(Clip-Y0)*(X1-X0)/(Y1-Y0),Clip,X1,Y1]);
261: }
262: if (Y0 < Clip && Y1 > Clip) {
263: return([X0,Y0,X1+(Clip-Y1)*(X0-X1)/(Y0-Y1),Clip]);
264: }
265: return([X0,Y0,X1,Y1]);
1.4 ! takayama 266: }
! 267:
! 268: /*&usage begin:glib_print(X,Y,Text|color)
! 269: It put a string {Text} at [{X},{Y}] on the glib canvas.
! 270: example: glib_print(100,100,"Hello Worlds" | color=0xff0000);
! 271: end: */
! 272: def glib_print(X,Y,Text) {
! 273: extern Glib_process, Glib_canvas,
! 274: Glib_xoffset, Glib_yoffset, Glib_xmag, Glib_ymag;
! 275: if (Glib_canvas < 0) glib_open();
! 276: glib_check();
! 277: glib_check_arg(X,Y);
! 278: if (type(Text) != 7) error("glib_print(X,Y,Text): Text must be a string.");
! 279: C = getopt(color);
! 280: if ( type(C) != -1 )
! 281: draw_string(Glib_process,Glib_canvas,
! 282: [X,Y],Text,C);
! 283: else
! 284: draw_string(Glib_process,Glib_canvas,
! 285: [X,Y],Text);
1.1 takayama 286: }
287: end$
288:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>