graphing.txt 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. Graphics in Scmutils
  2. We provide low-level support for plotting graphs and making simple
  3. drawings.
  4. One can make a window, draw lines and points in the window, clear the
  5. window and close it.
  6. A window is a data object that is made with the procedure frame.
  7. So, for example, one may make a window and give it the name win1 as
  8. follows:
  9. (define win1 (frame 0 7 -2 2))
  10. The window so constructed will have horizontal coordinates that range
  11. from 0 (inclusive) to 7 (exclusive) and vertical coordinates that
  12. range from -2 (inclusive) to 2 (exclusive).
  13. Execution of the frame procedure will construct the window and put it
  14. up on your screen. However, you must give it a name so that you can
  15. refer to the window to draw in it.
  16. Given such a window, you can use it to plot a function:
  17. (plot-function win1 sin 0 7 .01)
  18. This will plot in the window win1 the curve described by (sin theta),
  19. in the interval from theta=0 to theta=7, sampling at intervals of
  20. delta-theta=.01.
  21. The general pattern is
  22. (plot-function <window> <procedure> <x-min> <x-max> <delta-x>)
  23. where <procedure> takes one numerical argument and produces a
  24. numerical value.
  25. We can overlay other plots in the same window:
  26. (plot-function win1 cos 0 7 .01)
  27. If we want, we can clear the window:
  28. (graphics-clear win1)
  29. And we can make the window go away:
  30. (graphics-close win1)
  31. After a window is closed it is no longer useful for plotting so it is
  32. necessary to make a new one using frame if you want to plot further.
  33. There are other useful procedures for plotting.
  34. (plot-point <window> <x> <y>)
  35. drops a point at the coordinates (x, y) in the window.
  36. (plot-line <window> <x0> <y0> <x1> <y1>)
  37. draws a line segment from (x0, y0) to (x1, y1) in the window.
  38. (plot-parametric <window> <procedure> <t-min> <t-max> <delta-t>)
  39. draws a parametric curve. The <procedure> must implement a
  40. function of one real argument (the parameter) and must return the
  41. cons pair of two numbers, the x and the y value for the given
  42. value of the parameter.
  43. One can use the pointing device (mouse) to indicate a position. The
  44. procedure to interrogate the pointing device is:
  45. (get-pointer-coordinates <window> <continuation>)
  46. where <continuation> is a procedure that is called when the pointing
  47. device is positioned and a button is pressed. The continuation takes
  48. 3 arguments, the x-coordinate of the hit, the y-coordinate of the hit,
  49. and a designator of which mouse button was pressed.
  50. For example:
  51. (get-pointer-coordinates win1 list)
  52. ;Value: (.16791979949874686 .5037593984962406 0)
  53. The value returned indicates that the left mouse button was pressed
  54. when the pointer was placed at the coordinates .1679... .5037...
  55. The frame procedure takes a large number of optional arguments,
  56. allowing one to tailor a window to particular specifications. The
  57. default values shown below are for the X window system used with Unix.
  58. (frame xmin ;minimum x coordinate. 0.0
  59. xmax ;maximum x coordinate. 1.0
  60. ymin ;minimum y coordinate. 0.0
  61. ymax ;maximum y coordinate. 1.0
  62. frame-width ;width of window 400 pixels
  63. frame-height ;height of window 400 pixels
  64. frame-x ;horizontal screen position 750 pixels
  65. ; of left edge of window.
  66. frame-y ;vertical screen position 0 pixels
  67. ; of top edge of window
  68. )