Debian Codegraph @

cmd/gvpr/lib/rotate

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