rotate 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* Given node name and angle, rotate a layout using the given
  2. * node as origin.
  3. */
  4. BEGIN {
  5. double x,y;
  6. double x0,y0;
  7. double x1,y1;
  8. double angle, cosa, sina;
  9. int cnt, sz;
  10. void rotate (double a, double b) {
  11. a -= x0;
  12. b -= y0;
  13. x1 = a*cosa - b*sina;
  14. y1 = a*sina + b*cosa;
  15. }
  16. char* rotateE (char* p) {
  17. char* newpos = "";
  18. cnt = sscanf (p, "e,%lf,%lf%n", &x, &y, &sz);
  19. if (cnt == 2) {
  20. rotate (x,y);
  21. newpos = newpos + sprintf ("e%lf,%lf ", x1, y1);
  22. p = substr(p, sz);
  23. }
  24. cnt = sscanf (p, "s,%lf,%lf%n", &x, &y, &sz);
  25. if (cnt == 2) {
  26. rotate (x,y);
  27. newpos = newpos + sprintf ("s%lf,%lf ", x1, y1);
  28. p = substr(p, sz);
  29. }
  30. while (sscanf (p, "%lf,%lf%n", &x, &y, &sz) == 2) {
  31. rotate (x,y);
  32. newpos = newpos + sprintf ("%lf,%lf ", x1, y1);
  33. p = substr(p, sz);
  34. }
  35. return newpos;
  36. }
  37. }
  38. BEG_G {
  39. node_t ctr = node ($, ARGV[0]);
  40. sscanf (ARGV[1], "%f", &angle);
  41. cosa = cos(angle);
  42. sina = sin(angle);
  43. sscanf (ctr.pos, "%f,%f", &x0, &y0);
  44. $.bb ="";
  45. }
  46. N {
  47. sscanf ($.pos, "%f,%f", &x, &y);
  48. rotate (x,y);
  49. $.pos = sprintf ("%f,%f", x1, y1);
  50. }
  51. E {
  52. $.pos = rotateE($.pos);
  53. }