View Javadoc

1   
2   package org.catacomb.graph.drawing;
3   
4   
5   
6   import org.catacomb.be.Position;
7   import org.catacomb.report.E;
8   
9   
10  public class ShapeSymmetry {
11  
12      // if npt=4 and not extensible, following can apply:
13      public final static int NONE = 0;
14      public final static int RECTANGLE = 1;
15      public final static int DIAMOND = 2;
16      public final static int SQUARE = 3;
17  
18      public final static String[] symmetryNames= {"none", "rectangle",
19                                   "diamond", "square"
20                                                  };
21  
22  
23      public static String[] getSymmetryNames() {
24          return symmetryNames;
25      }
26  
27  
28      public static void applySymmetry(Shape shape, ShapePoint sp, Position pos) {
29          int isym = shape.getSymmetry();
30  
31          if (isym == NONE) {
32              sp.setPosition(pos);
33  
34          } else {
35  
36              ShapePoint[] spa = shape.getPoints();
37              int isp = -1;
38              int np = 4; // ADHOC
39              for (int i = 0; i < np; i++) {
40                  if (sp == spa[i]) {
41                      isp = i;
42                  }
43              }
44  
45              //adjacent clockwise, adjacent anticlockwise and opposite
46              ShapePoint adjc = spa[(isp+1) % np];
47              ShapePoint opp = spa[(isp+2) % np];
48              ShapePoint adja = spa[(isp+3) % np];
49  
50              double ox = opp.getPosition().getX();
51              double oy = opp.getPosition().getY();
52              double dx = pos.getX() - ox;
53              double dy = pos.getY() - oy;
54  
55              double zx = dx;
56              double zy = dy;
57  
58              if (isym == SQUARE) {
59                  double z = Math.sqrt(0.5 * (dx*dx + dy * dy));
60  
61                  zx = (dx > 0 ? z : -z);
62                  zy = (dy > 0 ? z : -z);
63  
64              } else if (isym == RECTANGLE) {
65                  // leave as is;
66              } else {
67                  E.missing("sym=" + isym);
68              }
69  
70              adjc.setPosition(new Position(ox +zx, oy));
71              adja.setPosition(new Position(ox, oy + zy));
72              sp.setPosition(new Position(ox + zx, oy + zy));
73          }
74      }
75  
76  
77      public static String getStringSymmetry(int isym) {
78          return symmetryNames[isym];
79      }
80  
81  
82  }