home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / fractal / kaos.lha / binsrclib / sgraphlib.c < prev   
Encoding:
C/C++ Source or Header  |  1990-01-25  |  5.7 KB  |  281 lines

  1. /* 
  2. This is a modified version of sgraphlib for kaos
  3. =================================================
  4. Bug:
  5.     circle: draw filled circle
  6.     arc: draw filled arc + strange object
  7.     title label: not located properly
  8. */
  9. #include <math.h>
  10. #include <suntool/sunview.h>
  11. #include <suntool/canvas.h>
  12. #include <sunwindow/cms_rainbow.h>
  13.  
  14. #define CMAP_SIZE 128
  15. static unsigned char red[CMAP_SIZE],green[CMAP_SIZE],blue[CMAP_SIZE];
  16. static int my_colormap_size=CMAP_SIZE;
  17. static double twopi=6.2831853071795864; 
  18.  
  19. Frame graph_frame;
  20. Canvas graph_canvas;
  21. Pixwin *graph_pw;
  22. Pixfont *graph_font;
  23.  
  24. static int graph_xwindow=400,graph_ywindow=400;
  25. static int graph_xcanvas,graph_ycanvas;
  26. static int graph_xwidth,graph_ywidth;
  27. static int graph_lmargin=10,graph_bmargin=10;
  28.  
  29. static int color=1;    /* current color */
  30. static int graph_xc=0,graph_yc=0;    /* current point */
  31. static int graph_xmin,graph_xmax,graph_ymin,graph_ymax;    /* bounds in graph coordinates */
  32. static int graph_npts[1] = {100};
  33. static struct pr_pos graph_vlist[100];
  34.  
  35. openpl()
  36. {
  37.     void window_init();
  38.  
  39.     window_init();
  40.     create_graph_frame();
  41.     create_graph_canvas();
  42.     graph_pw = (Pixwin *) canvas_pixwin(graph_canvas);
  43.     graph_font = (Pixfont *) pf_open("/usr/lib/fonts/fixedwidthfonts/screen.b.12");
  44.     cms_rainbowsetup(red,green,blue);
  45.     pw_setcmsname(graph_pw,"graph colormap");
  46.     pw_putcolormap(graph_pw,0,my_colormap_size,red,green,blue);
  47. }
  48.  
  49. space(x1,y1,x2,y2)
  50. int x1,y1,x2,y2;
  51. {
  52.     graph_xmin = x1;
  53.     graph_ymin = y1;
  54.     graph_xmax = x2;
  55.     graph_ymax = y2;
  56.     if(graph_xmin>graph_xmax) {
  57.         printf("Error: graph_xmin > graph_xmax.\n");
  58.         exit(0);
  59.     }
  60.     if(graph_ymin>graph_ymax) {
  61.         printf("Error: graph_ymin > graph_ymax.\n");
  62.         exit(0);
  63.     }
  64.     graph_xc = 0;
  65.     graph_yc = 0;
  66. }    
  67.  
  68. erase()
  69. {
  70.      pw_writebackground(graph_pw,0,0,window_get(graph_canvas,CANVAS_WIDTH),
  71.          window_get(graph_canvas,CANVAS_HEIGHT),PIX_CLR);
  72. }
  73.  
  74. label(s)
  75. char *s;
  76. {
  77.     int xt,yt;
  78.     xt = graph_xc;
  79.     yt = graph_yc;
  80.     to_sun_coords(&xt,&yt);
  81.     pw_text(graph_pw,xt,yt,(PIX_SRC|PIX_DST)|PIX_COLOR(color),graph_font,s);
  82. }
  83.  
  84. line(x1,y1,x2,y2)
  85. int x1,y1,x2,y2;
  86. {
  87.     graph_xc = x2;
  88.     graph_yc = y2;
  89.     to_sun_coords(&x1,&y1);
  90.     to_sun_coords(&x2,&y2);
  91.     pw_vector(graph_pw,x1,y1,x2,y2,PIX_SRC|PIX_COLOR(color),1);
  92. }
  93.  
  94. circle(x,y,r)
  95. int x,y,r;
  96. {
  97.     int i,ii,imax,dum;
  98.     double thetai;
  99.  
  100.     graph_xc = x;
  101.     graph_yc = y;
  102.     to_sun_coords(&x,&y);
  103.     to_sun_coords(&r,&dum);
  104.     imax = (int) twopi * r;
  105.     if(imax>100)
  106.         imax = 100;
  107.     for(i=0;i<imax;i++){
  108.         thetai = twopi * i / imax;
  109.         graph_vlist[i].x = r * cos(thetai);
  110.         graph_vlist[i].y = r * sin(thetai);
  111.     }
  112.     pw_polygon_2(graph_pw,x,y,1,graph_npts,graph_vlist,PIX_SRC|PIX_COLOR(color),(Pixrect *)0,0,0);
  113. }
  114.  
  115. arc(x,y,x0,y0,x1,y1)
  116. int x,y,x0,y0,x1,y1;
  117. {
  118.     int i,ii,imax;
  119.     double r,r2,theta,thetai;
  120.  
  121.     graph_xc = x;
  122.     graph_yc = y;
  123.     to_sun_coords(&x,&y);
  124.     to_sun_coords(&x0,&y0);
  125.     to_sun_coords(&x1,&y1);
  126.  
  127.     r2 = (x0-x)*(x0-x)+(y0-y)*(y0-y);
  128.     r = sqrt(r2);
  129.     theta = acos(((x1-x)*(x0-x)+(y1-y)*(y0-y))/r2);
  130.     theta = ((x0-x)*(y1-y)-(x1-x)*(y0-y)) > 0 ? theta : twopi - theta;
  131.     imax = (int) theta/twopi*r;
  132.     if(imax>100)
  133.         imax = 100;
  134.     for(i=0;i<imax;i++){
  135.         thetai = theta * i / imax;
  136.         graph_vlist[i].x = r * cos(thetai);
  137.         graph_vlist[i].y = r * sin(thetai);
  138.     }
  139.     pw_polygon_2(graph_pw,x,y,1,graph_npts,graph_vlist,PIX_SRC|PIX_COLOR(color),0,0,0);
  140. }
  141.  
  142. move(x,y)
  143. int x,y;
  144. {
  145.     graph_xc = x;
  146.     graph_yc = y;
  147. }
  148.  
  149. cont(x,y)
  150. int x,y;
  151. {
  152.     int xt,yt;
  153.     xt = graph_xc;
  154.     yt = graph_yc;
  155.     graph_xc = x;
  156.     graph_yc = y;
  157.     to_sun_coords(&xt,&yt);
  158.     to_sun_coords(&x,&y);
  159.     pw_vector(graph_pw,xt,yt,x,y,PIX_SRC|PIX_COLOR(color),1);
  160. }
  161.  
  162. point(x,y)
  163. int x,y;
  164. {
  165.     graph_xc = x;
  166.     graph_yc = y;
  167.     to_sun_coords(&x,&y);
  168.     /*
  169.     pw_put(graph_pw,x,y,1);
  170.     */
  171.     pw_rop(graph_pw,x,y,1,1,PIX_SRC|PIX_COLOR(color),0,0,0);
  172. }
  173.  
  174. linemod(s)
  175. char *s;
  176. {
  177.     extern int color;
  178.  
  179.     if(strcmp(s,"disconnected")==0){
  180.         color = 93;
  181.     }
  182.     else if(strcmp(s,"solid")==0){
  183.         color = 59; /* for kaos */
  184.     }    
  185.     else if(strcmp(s,"dotted")==0){
  186.         color = 3;
  187.     }    
  188.     else if(strcmp(s,"dotdashed")==0){
  189.         color = 4;
  190.     }    
  191.     else if(strcmp(s,"shortdashed")==0){
  192.         color = 5;
  193.     }    
  194.     else if(strcmp(s,"longdashed")==0){
  195.         color = 6;
  196.     }    
  197. }
  198.  
  199. closepl()
  200. {
  201.     window_main_loop(graph_frame);
  202. }
  203.  
  204. closevt()
  205. {
  206.     window_main_loop(graph_frame);
  207. }
  208.  
  209. create_graph_frame()
  210. {
  211.     graph_frame = window_create(NULL,FRAME,
  212.         FRAME_NO_CONFIRM,FALSE,
  213.         FRAME_SHOW_LABEL,TRUE,
  214.         FRAME_LABEL,"sgraph",
  215.         WIN_X,300,
  216.         WIN_Y,300,
  217.         WIN_WIDTH,graph_xwindow,
  218.         WIN_HEIGHT,graph_ywindow,
  219.         0);
  220.     if(graph_frame == NULL) {
  221.         printf("No more windows. Clean up some windows to make room.\n");    
  222.         exit(0);
  223.     }
  224. }
  225.  
  226. create_graph_canvas()
  227. {
  228.     void handle_event(),canvas_resize_proc(),canvas_repaint_proc();
  229.  
  230.     graph_canvas = window_create(graph_frame, CANVAS,
  231.         CANVAS_RETAINED,    TRUE,
  232.             CANVAS_AUTO_SHRINK,     TRUE,
  233.             CANVAS_AUTO_EXPAND,     TRUE,
  234.             CANVAS_WIDTH,           graph_xcanvas,
  235.             CANVAS_HEIGHT,          graph_ycanvas,
  236.         WIN_CONSUME_PICK_EVENTS,     WIN_MOUSE_BUTTONS,    LOC_DRAG,0,
  237.         WIN_EVENT_PROC,             handle_event,
  238.         CANVAS_RESIZE_PROC,    canvas_resize_proc,
  239.         CANVAS_REPAINT_PROC,    canvas_repaint_proc,
  240.         0);
  241.     if(graph_canvas == NULL) {
  242.         printf("No more windows. Clean up some windows to make room.\n");    
  243.         exit(0);
  244.     }
  245. }
  246.  
  247. to_sun_coords(xp,yp)
  248. int *xp,*yp;
  249. {
  250.     *xp = (int) (graph_lmargin + graph_xwidth * (*xp-graph_xmin)/(graph_xmax-graph_xmin));
  251.     *yp = (int) (graph_ycanvas - graph_ywidth * (*yp-graph_ymin)/(graph_ymax-graph_ymin) - graph_bmargin);
  252. }
  253.  
  254. void window_init()
  255. {
  256.     graph_xcanvas = graph_xwindow -10;
  257.     graph_ycanvas = graph_ywindow -15;
  258.     graph_xwidth = graph_xcanvas-graph_lmargin*2;
  259.     graph_ywidth = graph_ycanvas-graph_bmargin*2;
  260. }
  261.  
  262. void handle_event()
  263. {
  264. }
  265.  
  266. void canvas_resize_proc()
  267. {
  268.     void window_init();
  269.     extern Frame graph_frame;
  270.     extern int graph_xwindow,graph_ywindow;
  271.  
  272.     graph_xwindow = (int) window_get(graph_frame,WIN_WIDTH);
  273.     graph_ywindow = (int) window_get(graph_frame,WIN_HEIGHT);
  274.     (void) window_init();
  275. }
  276.  
  277. void canvas_repaint_proc()
  278. {
  279.     draw_all();
  280. }
  281.