/su/plot/drawcurve.c

https://github.com/ahay/src · C · 163 lines · 59 code · 14 blank · 90 comment · 7 complexity · 92f3ef2511ab16fe96b387e0c1495a1b MD5 · raw file

  1. /* Copyright (c) Colorado School of Mines, 2007.*/
  2. /* All rights reserved. */
  3. /* DRAWCURVE: $Revision: 1.2 $ ; $Date: 1999/01/14 22:50:24 $ */
  4. /*********************** self documentation **********************/
  5. /*****************************************************************************
  6. DRAWCURVE - Functions to draw a curve from a set of points
  7. xDrawCurve draw a curve from a set of points
  8. *****************************************************************************
  9. Function Prototypes:
  10. void xDrawCurve(Display *dpy, Window win,
  11. int x, int y, int width, int height,
  12. float x1beg, float x1end, float p1beg, float p1end,
  13. float x2beg, float x2end, float p2beg, float p2end,
  14. float *x1curve, float *x2curve, int ncurve,
  15. char *curvecolor, int style);
  16. *****************************************************************************
  17. xDrawCurve:
  18. Input:
  19. dpy display pointer
  20. win window
  21. x x coordinate of upper left corner of box
  22. y y coordinate of upper left corner of box
  23. width width of box
  24. height height of box
  25. x1beg axis value at beginning of axis 1
  26. x1end axis value at end of axis 1
  27. p1beg pad value at beginning of axis 1
  28. p1end pad value at end of axis 1
  29. x2beg axis value at beginning of axis 2
  30. x2end axis value at end of axis 2
  31. p2beg pad value at beginning of axis 2
  32. p2end pad value at end of axis 2
  33. x1curve vector of x1 coordinates for points along curve
  34. x2curve vector of x2 coordinates for points along curve
  35. ncurve number of points along curve
  36. curvecolor name of color to use for axes
  37. int style NORMAL (axis 1 on bottom, axis 2 on left)
  38. SEISMIC (axis 1 on left, axis 2 on top)
  39. ******************************************************************************
  40. Author: Brian Macy, Phillips Petroleum Co., 11/14/98
  41. (Adapted after Dave Hale's xDrawAxesBox routine)
  42. *****************************************************************************/
  43. /**************** end self doc ********************************/
  44. #include <X11/Xlib.h>
  45. #include <rsf.h>
  46. #include "axesbox.h"
  47. void xDrawCurve(Display *dpy, Window win,
  48. int x, int y, int width, int height,
  49. float x1beg, float x1end, float p1beg, float p1end,
  50. float x2beg, float x2end, float p2beg, float p2end,
  51. float *x1curve, float *x2curve, int ncurve,
  52. char *curvecolor, int style)
  53. /*< draw a curve from a set of points >*/
  54. /******************************************************************************
  55. Input:
  56. dpy display pointer
  57. win window
  58. x x coordinate of upper left corner of box
  59. y y coordinate of upper left corner of box
  60. width width of box
  61. height height of box
  62. x1beg axis value at beginning of axis 1
  63. x1end axis value at end of axis 1
  64. p1beg pad value at beginning of axis 1
  65. p1end pad value at end of axis 1
  66. x2beg axis value at beginning of axis 2
  67. x2end axis value at end of axis 2
  68. p2beg pad value at beginning of axis 2
  69. p2end pad value at end of axis 2
  70. x1curve vector of x1 coordinates for points along curve
  71. x2curve vector of x2 coordinates for points along curve
  72. ncurve number of points along curve
  73. curvecolor name of color to use for axes
  74. int style NORMAL (axis 1 on bottom, axis 2 on left)
  75. SEISMIC (axis 1 on left, axis 2 on top)
  76. ******************************************************************************
  77. Author: Brian Macy, Phillips Petroleum Co., 11/14/98
  78. (Adapted after Dave Hale's xDrawAxesBox routine)
  79. *****************************************************************************/
  80. {
  81. GC gcc;
  82. XGCValues *values=NULL;
  83. XColor scolor,ecolor;
  84. XWindowAttributes wa;
  85. Colormap cmap;
  86. float xbase,ybase,xscale,yscale;
  87. /* float xamin,xamax,yamin,yamax; */
  88. float *xcurve,*ycurve;
  89. XPoint *lpoints; /* points for drawing line */
  90. XRectangle rectclip;
  91. int i;
  92. /* allocate memory for lpoints */
  93. lpoints=(XPoint *) sf_alloc(ncurve,sizeof(XPoint));
  94. /* create graphics contexts */
  95. gcc = XCreateGC(dpy,win,0,values);
  96. /* determine window's current colormap */
  97. XGetWindowAttributes(dpy,win,&wa);
  98. cmap = wa.colormap;
  99. /* get and set colors */
  100. if (XAllocNamedColor(dpy,cmap,curvecolor,&scolor,&ecolor))
  101. XSetForeground(dpy,gcc,ecolor.pixel);
  102. else
  103. XSetForeground(dpy,gcc,1L);
  104. if (style==NORMAL) {
  105. /* xamin = (x1beg<x1end)?x1beg:x1end;
  106. xamax = (x1beg>x1end)?x1beg:x1end; */
  107. xscale = width/(x1end+p1end-x1beg-p1beg);
  108. xbase = x-xscale*(x1beg+p1beg);
  109. /* yamin = (x2beg<x2end)?x2beg:x2end;
  110. yamax = (x2beg>x2end)?x2beg:x2end; */
  111. yscale = -height/(x2end+p2end-x2beg-p2beg);
  112. ybase = y+height-yscale*(x2beg+p2beg);
  113. xcurve=x1curve;
  114. ycurve=x2curve;
  115. } else {
  116. /* xamin = (x2beg<x2end)?x2beg:x2end;
  117. xamax = (x2beg>x2end)?x2beg:x2end; */
  118. xscale = width/(x2end+p2end-x2beg-p2beg);
  119. xbase = x-xscale*(x2beg+p2beg);
  120. /* yamin = (x1beg<x1end)?x1beg:x1end;
  121. yamax = (x1beg>x1end)?x1beg:x1end; */
  122. yscale = height/(x1end+p1end-x1beg-p1beg);
  123. ybase = y-yscale*(x1beg+p1beg);
  124. ycurve=x1curve;
  125. xcurve=x2curve;
  126. }
  127. /* Set up clip rectangle (only allows drawing inside rect.) */
  128. rectclip.x = (short) x;
  129. rectclip.y = (short) y;
  130. rectclip.width = (unsigned short) width;
  131. rectclip.height = (unsigned short) height;
  132. XSetClipRectangles(dpy,gcc,0,0,&rectclip,1,Unsorted);
  133. /*
  134. * Draw a curve from the input data xcurve,ycurve.
  135. */
  136. for (i=0; i<ncurve; ++i) {
  137. lpoints[i].x=(short) (xbase+xscale*xcurve[i]);
  138. lpoints[i].y=(short) (ybase+yscale*ycurve[i]);
  139. }
  140. if (ncurve==1)
  141. XDrawPoints(dpy,win,gcc,lpoints,ncurve,CoordModeOrigin);
  142. else if (ncurve > 1)
  143. XDrawLines(dpy,win,gcc,lpoints,ncurve,CoordModeOrigin);
  144. /* free resources before returning */
  145. XFreeGC(dpy,gcc);
  146. free(lpoints);
  147. }