Source code: com/port80/graph/dot/impl/DotPolyline.java
1 //
2 // Copyright(c) 2002, Chris Leung
3 //
4
5 package com.port80.graph.dot.impl;
6
7 /** Wrapper of a dynamic allocated array of pre-allocated DotPoint.
8 * The point array are pre-allocated with DotPoint so they can be
9 * reused without having to be re-allocated each time.
10 *
11 * . The class keep its own copy of the points so prevent cross
12 * linking with other objects.
13 */
14 public class DotPolyline {
15
16 ////////////////////////////////////////////////////////////////////////
17
18 private static final String NAME = "DotPolyline";
19
20 public DotPoint[] pts = new DotPoint[0];
21 public int size = 0;
22 public boolean isClosed = false;
23
24 ////////////////////////////////////////////////////////////////////////
25
26 public DotPolyline() {
27 grow(5);
28 }
29 public DotPolyline(int n) {
30 grow(n);
31 }
32
33 ////////////////////////////////////////////////////////////////////////
34
35 public void add(DotPoint pt) {
36 if (size >= pts.length)
37 grow(pts.length / 4 + 3);
38 pts[size].x = pt.x;
39 pts[size].y = pt.y;
40 ++size;
41 }
42 public void add(double x, double y) {
43 if (size >= pts.length)
44 grow(pts.length / 4 + 3);
45 pts[size].x = x;
46 pts[size].y = y;
47 ++size;
48 }
49 public DotPoint get(int i) {
50 return pts[i];
51 }
52 public int size() {
53 return size;
54 }
55 public DotPolyline reset() {
56 size = 0;
57 return this;
58 }
59 /** Close the polyline as a polygon with the starting point at the
60 * end, so we don't have to wrap around to 0.*/
61 public void close() {
62 if (!isClosed) {
63 add(pts[0]);
64 isClosed = true;
65 }
66 }
67 public boolean isClosed() {
68 return isClosed;
69 }
70 public String toString() {
71 StringBuffer ret = new StringBuffer(NAME + "\n");
72 for (int i = 0; i < size; ++i) {
73 ret.append(pts[i].toString() + " ");
74 if (i % 4 == 3)
75 ret.append("\n");
76 }
77 return ret.toString();
78 }
79 public String toGeneralPath() {
80 return toGeneralPath(1);
81 }
82 public String toGeneralPath(int lwidth) {
83 StringBuffer ret = new StringBuffer("<path lwidth=\"" + lwidth + "\">\n");
84 if (isClosed)
85 ret.append("<l closed=\"true\">\n");
86 else
87 ret.append("<l>\n");
88 int i = 0;
89 for (; i < size; ++i) {
90 if (i % 4 == 0)
91 ret.append(" " + pts[i].toString());
92 else
93 ret.append(" " + pts[i].toString());
94 if (i % 4 == 3)
95 ret.append("\n");
96 }
97 if (i % 4 != 0)
98 ret.append("\n");
99 ret.append("</l>\n</path>\n");
100 return ret.toString();
101 }
102
103 ////////////////////////////////////////////////////////////////////////
104
105 public void grow(int n) {
106 int max = pts.length + n;
107 DotPoint[] a = new DotPoint[max];
108 System.arraycopy(pts, 0, a, 0, pts.length);
109 for (int i = pts.length; i < max; ++i) {
110 a[i] = new DotPoint();
111 }
112 pts = a;
113 }
114
115 public void growTo(int max) {
116 if (max <= pts.length)
117 return;
118 DotPoint[] a = new DotPoint[max];
119 System.arraycopy(pts, 0, a, 0, pts.length);
120 for (int i = pts.length; i < max; ++i) {
121 a[i] = new DotPoint();
122 }
123 pts = a;
124 }
125
126 ////////////////////////////////////////////////////////////////////////
127 }