1 package org.catacomb.interlish.content;
2
3 import org.catacomb.be.Direction;
4 import org.catacomb.be.Position;
5
6
7 public class Polypoint {
8
9 public double[] xpts;
10 public double[] ypts;
11
12 public static final int OPEN = 0;
13 public static final int CLOSED = 1;
14 public static final int FILLED = 2;
15
16 public String closure;
17 private int p_closure;
18 final public static String[] p_closureNames = { "open", "closed", "filled" };
19
20
21 public Polypoint() {
22 xpts = new double[2];
23 ypts = new double[2];
24 }
25
26 public Polypoint(double[] xp, double[] yp) {
27 this(xp, yp, OPEN);
28 }
29
30
31 public Polypoint(double[] xp, double[] yp, int icl) {
32 xpts = xp;
33 ypts = yp;
34 p_closure = icl;
35 }
36
37
38
39 public String exportAsString() {
40 StringBuffer sb = new StringBuffer();
41 sb.append("pp(" + xpts.length + ": ");
42 for (int i = 0; i < xpts.length; i++) {
43 sb.append(xpts[i] + ", " + ypts[i] + ", ");
44 }
45 sb.append(")");
46 return sb.toString();
47 }
48
49
50
51 public int getClosure() {
52 return p_closure;
53 }
54 public void setClosure(int pc) {
55 p_closure = pc;
56 closure = p_closureNames[p_closure];
57 }
58
59 public boolean isFilled() {
60 return (p_closure == FILLED);
61 }
62 public boolean isOpen() {
63 return (p_closure == OPEN);
64 }
65 public boolean isClosed() {
66 return (p_closure == CLOSED || p_closure == FILLED);
67 }
68
69
70 public void setXpts(double[] d) {
71 xpts = d;
72 }
73
74
75 public void setYpts(double[] d) {
76 ypts = d;
77 }
78
79 public double[] getXPts() {
80 return xpts;
81 }
82
83 public double[] getYPts() {
84 return ypts;
85 }
86
87
88 public Polypoint getCopy() {
89 int np = xpts.length;
90 double[] xp = new double[np];
91 double[] yp = new double[np];
92 for (int i = 0; i < np; i++) {
93 xp[i] = xpts[i];
94 yp[i] = ypts[i];
95 }
96 return new Polypoint(xp, yp, p_closure);
97 }
98
99
100
101 public void translate(Position pos) {
102 double dx = pos.getX();
103 double dy = pos.getY();
104 for (int i = 0; i < xpts.length; i++) {
105 xpts[i] += dx;
106 ypts[i] += dy;
107 }
108 }
109
110 public void rotate(Direction dir) {
111 double c = dir.getCosine();
112 double s = dir.getSine();
113 for (int i = 0; i < xpts.length; i++) {
114 double x = xpts[i];
115 double y = ypts[i];
116 xpts[i] = c * x - s * y;
117 ypts[i] = s * x + c * y;
118 }
119
120 }
121
122 }