1 package org.catacomb.graph.drawing;
2
3 import org.catacomb.graph.gui.Painter;
4 import org.catacomb.util.ArrayUtil;
5
6
7 import java.awt.Color;
8 import java.util.StringTokenizer;
9
10
11
12 public class SegmentLine extends FixedDrawingComponent {
13
14
15
16
17
18
19
20
21
22 public String xyxy;
23
24 public double[] xpts;
25 public double[] ypts;
26
27
28 private double[] p_x;
29 private double[] p_y;
30
31
32 private double[] p_wkx;
33 private double[] p_wky;
34
35
36
37 public SegmentLine() {
38
39 }
40
41
42
43 public SegmentLine(double[] ax, double[] ay, Color c) {
44 p_x = ax;
45 p_y = ay;
46 setWidth(1.0);
47 setColor(c);
48 init();
49 }
50
51
52
53 public double[] getXPts() {
54 return p_x;
55 }
56
57
58 public double[] getYPts() {
59 return p_y;
60 }
61
62
63
64 public void instruct(Painter p, double offx, double offy, double scale) {
65
66
67 int n = p_x.length;
68 for (int i = 0; i < n; i++) {
69 p_wkx[i] = offx + scale * p_x[i];
70 p_wky[i] = offy + scale * p_y[i];
71 }
72
73 p.drawPolyline(p_wkx, p_wky, n, getColor(), getWidth(), widthIsPixels());
74 }
75
76
77
78 public void reReference() {
79 super.reReference();
80
81 if (xyxy != null) {
82 StringTokenizer st = new StringTokenizer(xyxy, ", \n");
83 int nt = st.countTokens();
84 int hn = nt / 2;
85 p_x = new double[hn];
86 p_y = new double[hn];
87 for (int i = 0; i < hn; i++) {
88 p_x[i] = (new Double(st.nextToken())).doubleValue();
89 p_y[i] = (new Double(st.nextToken())).doubleValue();
90 }
91 } else if (xpts != null) {
92 p_x = xpts;
93 p_y = ypts;
94
95 }
96
97 init();
98 }
99
100
101 private void init() {
102 if (p_x == null) {
103 p_x = new double[0];
104 p_y = new double[0];
105 }
106
107
108 int n = p_x.length;
109 p_wkx = new double[n];
110 p_wky = new double[n];
111 }
112
113
114
115 public void shift(double dx, double dy) {
116 for (int i = 0; i < p_x.length; i++) {
117 p_x[i] += dx;
118 p_y[i] += dy;
119 }
120 }
121
122
123 public void scaleBy(double fac) {
124 for (int i = 0; i < p_x.length; i++) {
125 p_x[i] *= fac;
126 p_y[i] *= fac;
127 }
128 }
129
130
131
132 public void applyToShape(Shape shp) {
133 shp.setClosure(Shape.OPEN);
134 shp.setCurviness(0.0);
135 shp.setSymmetry(ShapeSymmetry.NONE);
136 shp.setXpts(ArrayUtil.copyDArray(getXPts()));
137 shp.setYpts(ArrayUtil.copyDArray(getYPts()));
138 }
139
140
141 public static SegmentLine unitBox() {
142 double[] xp = { -1., 1., 1., -1., -1. };
143 double[] yp = { 1., 1., -1., -1., 1. };
144
145 SegmentLine ret = new SegmentLine(xp, yp, Color.gray);
146 return ret;
147 }
148
149
150 }