plot.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include "grap.h"
  13. #include "y.tab.h"
  14. void line(int type, Point p1, Point p2, Attr *desc) /* draw a line segment */
  15. {
  16. fprintf(tfd, "%s %s from %s",
  17. type==LINE ? "line" : "arrow", desc_str(desc), xyname(p1));
  18. fprintf(tfd, " to %s", xyname(p2)); /* 'cause xyname is botched */
  19. fprintf(tfd, "\n");
  20. range(p1);
  21. range(p2);
  22. }
  23. void circle(double r, Point pt) /* draw a circle */
  24. {
  25. if (r > 0.0)
  26. fprintf(tfd, "circle rad %g at %s\n", r, xyname(pt));
  27. else
  28. fprintf(tfd, "\"\\s-3\\(ob\\s0\" at %s\n", xyname(pt));
  29. range(pt);
  30. }
  31. char *xyname(Point pt) /* generate xy name macro for point p */
  32. {
  33. static char buf[200];
  34. Obj *p;
  35. p = pt.obj;
  36. if (p->log & XFLAG) {
  37. if (pt.x <= 0.0)
  38. ERROR "can't take log of x coord %g", pt.x FATAL;
  39. logit(pt.x);
  40. }
  41. if (p->log & YFLAG) {
  42. if (pt.y <= 0.0)
  43. ERROR "can't take log of y coord %g", pt.y FATAL;
  44. logit(pt.y);
  45. }
  46. sprintf(buf, "xy_%s(%g,%g)", p->name, pt.x, pt.y);
  47. return buf; /* WATCH IT: static */
  48. }
  49. void pic(char *s) /* fire out pic stuff directly */
  50. {
  51. while (*s == ' ')
  52. s++;
  53. fprintf(tfd, "%s\n", s);
  54. }
  55. int auto_x = 0; /* counts abscissa if none provided */
  56. void numlist(void) /* print numbers in default way */
  57. {
  58. Obj *p;
  59. Point pt;
  60. int i;
  61. static char *spot = "\\(bu";
  62. Attr *ap;
  63. p = pt.obj = lookup(curr_coord, 1);
  64. if (nnum == 1) {
  65. nnum = 2;
  66. num[1] = num[0];
  67. num[0] = ++auto_x;
  68. }
  69. pt.x = num[0];
  70. if (p->attr && p->attr->sval)
  71. spot = p->attr->sval;
  72. for (i = 1; i < nnum; i++) {
  73. pt.y = num[i];
  74. if (p->attr == 0 || p->attr->type == 0) {
  75. ap = makesattr(tostring(spot));
  76. plot(ap, pt);
  77. } else
  78. next(p, pt, p->attr);
  79. }
  80. nnum = 0;
  81. }
  82. void plot(Attr *sl, Point pt) /* put stringlist sl at point pt */
  83. {
  84. fprintf(tfd, "%s at %s\n", slprint(sl), xyname(pt));
  85. range(pt);
  86. freeattr(sl);
  87. }
  88. void plotnum(double f, char *fmt, Point pt) /* plot value f at point */
  89. {
  90. char buf[100];
  91. if (fmt) {
  92. sprintf(buf, fmt, f);
  93. free(fmt);
  94. } else if (f >= 0.0)
  95. sprintf(buf, "%g", f);
  96. else
  97. sprintf(buf, "\\-%g", -f);
  98. fprintf(tfd, "\"%s\" at %s\n", buf, xyname(pt));
  99. range(pt);
  100. }
  101. void drawdesc(int type, Obj *p, Attr *desc, char *s) /* set line description for p */
  102. {
  103. p->attr = desc;
  104. p->attr->sval = s;
  105. if (type == NEW) {
  106. p->first = 0; /* so it really looks new */
  107. auto_x = 0;
  108. }
  109. }
  110. void next(Obj *p, Point pt, Attr *desc) /* add component to a path */
  111. {
  112. char *s;
  113. if (p->first == 0) {
  114. p->first++;
  115. fprintf(tfd, "L%s: %s\n", p->name, xyname(pt));
  116. } else {
  117. fprintf(tfd, "line %s from L%s to %s; L%s: Here\n",
  118. desc_str(desc->type ? desc : p->attr),
  119. p->name, xyname(pt), p->name);
  120. }
  121. if (p->attr && (s=p->attr->sval)) {
  122. /* BUG: should fix size here */
  123. fprintf(tfd, "\"%s\" at %s\n", s, xyname(pt));
  124. }
  125. range(pt);
  126. }