home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / gle / gle / easydev.c < prev    next >
C/C++ Source or Header  |  1992-11-29  |  4KB  |  167 lines

  1. /* Easy to use default routines for simple drivers (line output only) */
  2. /*---------------------------------------------------------------------------*/
  3. #include <math.h>
  4. #include "all.h"
  5. #include "mygraph.h"
  6. #include "mydev.h"
  7. #include "core.h"
  8. #include "justify.h"
  9. extern struct gmodel g;
  10. /*---------------------------------------------------------------------------*/
  11. #define PI 3.141592653
  12. #define pi 3.141592653
  13. #define true (!false)
  14. #define false 0
  15. int clipvec(dbl x1,dbl y1,dbl x2,dbl y2,dbl wxmin,dbl wymin,dbl wxmax,dbl wymax);
  16. int xdf_barc(double r,dbl t1,dbl t2,dbl cx,dbl cy);
  17. /*---------------------------------------------------------------------------*/
  18. #define CSTEP (360/6)
  19.  
  20. df_bezier(dbl x1,dbl y1,dbl x2,dbl y2,dbl x3,dbl y3)
  21. {
  22.     double ax,bx,cx,ay,by,cy,dist;
  23.     double xxx,yyy,i,t,nstep,x0,y0;
  24.     g_get_xy(&x0,&y0);
  25.     dist = fabs(x3-x0) + fabs(y3-y0);
  26.     nstep = 10;
  27.     if (dist>3) nstep = 20;
  28.     if (dist<.5) nstep = 5;
  29.     if (dist<.3) nstep = 3;
  30.      if (dist<.1) {
  31.         g_line(x3,y3);
  32.         return;
  33.     }
  34.     cx = (x1-x0)*3;
  35.     bx = (x2-x1)*3-cx;
  36.     ax = x3-x0-cx-bx;
  37.     cy = (y1-y0)*3;
  38.     by = (y2-y1)*3-cy;
  39.     ay = y3-y0-cy-by;
  40.     for (i=0;i<=nstep;i++) {
  41.         t = i/nstep;
  42.         xxx = ax*pow(t,3.0) + bx*t*t + cx*t + x0;
  43.         yyy = ay*pow(t,3.0) + by*t*t + cy*t + y0;
  44.         g_line(xxx,yyy);
  45.     }
  46. }
  47.  
  48. df_arcto(dbl x1,dbl y1,dbl x2,dbl y2,dbl rrr)
  49. {
  50.     double x0,y0,r1,a1,r2,a2,r3,a3,a4,r5,sx,sy,ex,ey;
  51.     double bx1,by1,bx2,by2,dist,neg;
  52.     g_get_xy(&x0,&y0);
  53.     xy_polar(x1-x0,y1-y0,&r1,&a1);
  54.     xy_polar(x2-x1,y2-y1,&r2,&a2);
  55.     neg = 1;
  56.     a4 = (180-a2+a1);
  57.     a3 = a2 + (a4/2);
  58.     if ((a4/2)>90 && (a4/2)<180 ) neg = -1;
  59.     if ((a4/2)<0 && (a4/2)>-90 ) neg = -1;
  60.     r3 = neg*rrr/(tan((pi/180)*a4/2));
  61.     polar_xy(-r3,a1,&sx,&sy); sx += x1; sy += y1;
  62.     polar_xy(r3,a2,&ex,&ey); ex += x1; ey += y1;
  63.     g_line(sx,sy);
  64.     dist = sqrt((ex-sx)*(ex-sx) + (ey-sy)*(ey-sy));
  65.     polar_xy(r1+ dist/2.5-r3,a1,&bx1,&by1); bx1 += x0; by1 += y0;
  66.     polar_xy(-r2+ -dist/2.5+r3,a2,&bx2,&by2); bx2 += x2; by2 += y2;
  67.     g_bezier(bx1,by1,bx2,by2,ex,ey);
  68.     g_line(x2,y2);
  69. }
  70. df_arc(dbl r,dbl t1,dbl t2,dbl cx,dbl cy)
  71. {
  72.     /* circle from t1 to t2, lets use 6 bezier's for a circle */
  73.     double stz;
  74.     int nst,i;
  75.  
  76.     if (t2<t1) t2 = t2 + 360 ;
  77.     nst = floor((t2-t1)/CSTEP)+1;
  78.     stz = (t2-t1) / nst;
  79.     for (i=1;i<=nst;i++)
  80.         xdf_barc(r,t1+stz*(i-1),t1+stz*i,cx,cy);
  81. }
  82. xdf_barc(double r,dbl t1,dbl t2,dbl cx,dbl cy)
  83. {
  84.     double rx1,ry1,rx2,ry2,d,dx1,dy1,dx2,dy2;
  85.  
  86.     polar_xy(r,t1,&rx1,&ry1);
  87.     polar_xy(r,t2,&rx2,&ry2);
  88.     d = sqrt( (rx2-rx1)*(rx2-rx1) + (ry2-ry1)*(ry2-ry1));
  89.     polar_xy(d/3,t1+90,&dx1,&dy1);
  90.     polar_xy(d/3,t2-90,&dx2,&dy2);
  91.     if (g.inpath) {
  92.         g_line(rx1+cx,ry1+cy);
  93.         g_bezier(rx1+cx+dx1,ry1+cy+dy1
  94.             ,rx2+cx+dx2,ry2+cy+dy2,rx2+cx,ry2+cy);
  95.     } else {
  96.         g_move(rx1+cx,ry1+cy);
  97.         g_bezier(rx1+cx+dx1,ry1+cy+dy1
  98.             ,rx2+cx+dx2,ry2+cy+dy2,rx2+cx,ry2+cy);
  99.         g_move(cx,cy);
  100.     }
  101. }
  102. df_box_stroke(dbl x1, dbl y1, dbl x2, dbl y2)
  103. {
  104.     double ox,oy;
  105.     g_get_xy(&ox,&oy);
  106.     g_move(x1,y1);
  107.     g_line(x2,y1);
  108.     g_line(x2,y2);
  109.     g_line(x1,y2);
  110.     g_closepath();
  111.     g_move(ox,oy);
  112. }
  113.  
  114. df_box_fill(dbl x1, dbl y1, dbl x2, dbl y2)
  115. {
  116.     double dx,dy,cs,x,y;
  117.     long fc,oldcolor;
  118.     colortyp  cc;
  119.     g_get_fill(&fc);
  120.     cc.l = fc;
  121.     cs = cc.b[B_R]*3 + cc.b[B_B] + cc.b[B_G]*2;
  122.     if (cs>(6*250)) return; /* white, dont try and fill it in */
  123.     cs = (cs+50)/700;
  124.  
  125.     if (x1>x2) {dx = x1; x1 = x2; x2 = dx; }
  126.     if (y1>y2) {dy = y1; y1 = y2; y2 = dy; }
  127.     dx = x2-x1;
  128.     dy = y2-y1;
  129.     if (dx==0 || dy==0) return;
  130.         /* cs = color intensity  (measure in cm) */
  131.         /* set pen color to fill color now */
  132.     g_get_color(&oldcolor);
  133.     g_get_fill(&fc);
  134.     g_set_color(fc);
  135.     for (y = y1-dx; y<y2; y+=cs) {
  136.         clipvec(x1,y,x2,y+cs-.4,x1,y1,x2,y2);
  137.     }
  138.     g_set_color(oldcolor);
  139. }
  140. clipvec(dbl x1,dbl y1,dbl x2,dbl y2,dbl wxmin,dbl wymin,dbl wxmax,dbl wymax)
  141. {
  142.  
  143.     int invis;
  144.     if (x1>=wxmin && x1<=wxmax && y1>=wymin && y1<=wymax
  145.        && x2 >= wxmin && x2<=wxmax && y2>=wymin && y2<=wymax);
  146.     else {         /* ok one or both are outside our box */
  147.         gclip(&x1,&y1,&x2,&y2,wxmin,wymin,wxmax,wymax,&invis);
  148.         if (invis) return;
  149.     }
  150.     g_move(x1,y1);
  151.     g_line(x2,y2);
  152. }
  153. df_circle_stroke(dbl r)
  154. {
  155.     double cx,cy;
  156.     g_get_xy(&cx,&cy);
  157.     df_arc(r,0.0,360.0,cx,cy);
  158. }
  159. df_circle_fill(dbl r)
  160. {
  161.     double cx,cy;
  162.     g_get_xy(&cx,&cy);
  163.     df_arc(r,0.0,360.0,cx,cy);
  164. }
  165.  
  166.  
  167.