Annotation of OpenXM_contrib2/asir2000/lib/glib, Revision 1.2
1.2 ! takayama 1: /* $OpenXM: OpenXM_contrib2/asir2000/lib/glib,v 1.1 2001/12/10 05:02:46 takayama 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.1 takayama 153: draw_obj(Glib_process,Glib_canvas,
154: [floor(Glib_xmag*(X+Glib_xoffset)),
155: floor(Glib_ymag*(Y+Glib_yoffset))]);
156: }
157:
158: def glib_line(X0,Y0,X1,Y1) {
159: extern Glib_xmag, Glib_ymag, Glib_xoffset, Glib_yoffset;
1.2 ! takayama 160: glib_check_arg(X0,Y0);
! 161: glib_check_arg(X1,Y1);
1.1 takayama 162: glib_clip_line(floor(Glib_xmag*(X0+Glib_xoffset)),
163: floor(Glib_ymag*(Y0+Glib_yoffset)),
164: floor(Glib_xmag*(X1+Glib_xoffset)),
165: floor(Glib_ymag*(Y1+Glib_yoffset)));
166: }
167:
168: def glib_clip_line(X0,Y0,X1,Y1) {
169: /* X0, Y0, X1, Y1 should be integers.
170: Coordinates are already translated. */
171: extern Glib_process, Glib_canvas, Glib_canvas_x, Glib_canvas_y;
172: if (Glib_canvas < 0) glib_open();
173:
174: #ifdef CLIP
175: #ifdef DEBUG
176: print(["clip_line",[X0,Y0,X1,Y1]]);
177: #endif
178: /* clip by x = 0 */
179: S = glib_clip0_x(X0,Y0,X1,Y1,0);
180: #ifdef DEBUG
181: print(["clip0_x",S]);
182: #endif
183: if (type(S) == 0) return;
184: X0 = S[0]; Y0 = S[1]; X1 = S[2]; Y1 = S[3];
185:
186: S = glib_clip1_x(X0,Y0,X1,Y1,Glib_canvas_x-1);
187: #ifdef DEBUG
188: print(["clip1_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_clip0_y(X0,Y0,X1,Y1,0);
194: #ifdef DEBUG
195: print(["clip0_y",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_clip1_y(X0,Y0,X1,Y1,Glib_canvas_y-1);
201: #ifdef DEBUG
202: print(["clip1_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: #ifdef DEBUG
208: print([X0,Y0,X1,Y1]);
209: #endif
210: #endif
211: glib_check();
212: draw_obj(Glib_process,Glib_canvas,
213: [floor(X0),floor(Y0),floor(X1),floor(Y1)]);
214:
215: }
216:
217: def glib_clip0_x(X0,Y0,X1,Y1,Clip) {
218: if (X0 < Clip && X1 < Clip) return(0);
219: if (X0 < Clip && X1 > Clip) {
220: return([Clip, Y0+(Clip-X0)*(Y1-Y0)/(X1-X0),X1,Y1]);
221: }
222: if (X1 > Clip && X1 < Clip) {
223: return([X0,Y0,Clip, Y1+(Clip-X1)*(Y0-Y1)/(X0-X1)]);
224: }
225: return([X0,Y0,X1,Y1]);
226: }
227: def glib_clip0_y(X0,Y0,X1,Y1,Clip) {
228: if (Y0 < Clip && Y1 < Clip) return(0);
229: if (Y0 < Clip && Y1 > Clip) {
230: return([X0+(Clip-Y0)*(X1-X0)/(Y1-Y0),Clip,X1,Y1]);
231: }
232: if (Y1 > Clip && Y1 < Clip) {
233: return([X0,Y0,X1+(Clip-Y1)*(X0-X1)/(Y0-Y1),Clip]);
234: }
235: return([X0,Y0,X1,Y1]);
236: }
237: def glib_clip1_x(X0,Y0,X1,Y1,Clip) {
238: if (X0 > Clip && X1 > Clip) return(0);
239: if (X0 > Clip && X1 < Clip) {
240: return([Clip, Y0+(Clip-X0)*(Y1-Y0)/(X1-X0),X1,Y1]);
241: }
242: if (X1 < Clip && X1 > Clip) {
243: return([X0,Y0,Clip, Y1+(Clip-X1)*(Y0-Y1)/(X0-X1)]);
244: }
245: return([X0,Y0,X1,Y1]);
246: }
247: def glib_clip1_y(X0,Y0,X1,Y1,Clip) {
248: if (Y0 > Clip && Y1 > Clip) return(0);
249: if (Y0 > Clip && Y1 < Clip) {
250: return([X0+(Clip-Y0)*(X1-X0)/(Y1-Y0),Clip,X1,Y1]);
251: }
252: if (Y0 < Clip && Y1 > Clip) {
253: return([X0,Y0,X1+(Clip-Y1)*(X0-X1)/(Y0-Y1),Clip]);
254: }
255: return([X0,Y0,X1,Y1]);
256: }
257: end$
258:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>