1 package fi.jyu.mit.graphics;
2
3 import java.awt.*;
4
5
10 public class Polygon extends BasicShape {
11
12 protected RPoint[] ps;
13 protected int count;
14
15 public Polygon() {
16 super();
17 }
18
19
25 public Polygon(double[] xpoints, double[] ypoints) {
26 super();
27 if (xpoints.length < ypoints.length) this.count = xpoints.length;
28 else this.count = ypoints.length;
29 initialize();
30 for (int i = 0; i < getCount(); i++) this.ps[i] = new RPoint(xpoints[i], ypoints[i]); }
32
33
47 public Polygon(double x, double dx, double[] ypoints) {
48 super();
49 this.count = ypoints.length;
50 initialize();
51 for (int i = 0; i < getCount(); i++) this.ps[i] = new RPoint(x+i*dx, ypoints[i]); }
53
54
61 public Polygon(double[] xpoints, double[] ypoints, double[] zpoints) {
62 super();
63 if (xpoints.length < ypoints.length) this.count = xpoints.length;
64 else this.count = ypoints.length;
65 initialize();
66 for (int i = 0; i < this.count; i++) this.ps[i] = new RPoint(xpoints[i], ypoints[i], zpoints[i]); }
68
69
79 public Polygon(double[][] points) {
80 super();
81 int dim = 4;
82 for (int i = 0; i < points.length; i++) {
83 if (points[i].length < dim) dim = points[i].length;
84 }
85 this.count = points.length;
86 initialize();
87 for (int i = 0; i < getCount(); i++) {
88 if (dim == 2) this.ps[i] = new RPoint(points[i][0], points[i][1]); if (dim == 3) this.ps[i] = new RPoint(points[i][0], points[i][1], points[i][2]); }
91 }
92
93
97 public void setPoints(double[][] points) {
98 int dim = 4;
99 for (int i = 0; i < points.length; i++) {
100 if (points[i].length < dim) dim = points[i].length;
101 }
102 this.count = points.length;
103 initialize();
104 for (int i = 0; i < this.count; i++) {
105 if (dim == 2) this.ps[i] = new RPoint(points[i][0], points[i][1]); if (dim == 3) this.ps[i] = new RPoint(points[i][0], points[i][1], points[i][2]); }
108 redraw();
109 }
110
111
115 public void setPoints(RPoint[] points) {
116 this.count = points.length;
117 initialize();
118 for (int i = 0; i < this.count; i++) {
119 this.ps[i] = points[i];
120 }
121 redraw();
122 }
123
124
128 public Polygon(RPoint[] points) {
129 super();
130 this.count = points.length;
131 initialize();
132 for (int i = 0; i < points.length; i++) {
133 this.ps[i] = points[i];
134 }
135 }
136
137
140 protected void initialize() {
141 this.ps = new RPoint[getCount()];
142 }
143
144
149 protected int[][] makeScreenPoints(Matrix a) {
150 int[][] sPoints = new int[2][getCount()];
151 SPoint sp = new SPoint(0,0);
152 for (int i = 0; i < this.getCount(); i++) {
153 a.transform(this.ps[i],sp);
154 sPoints[0][i] = sp.getX();
155 sPoints[1][i] = sp.getY();
156 }
157 return sPoints;
158 }
159
160
164 public final int getCount() {
165 return count;
166 }
167
168
173 public Polygon add(RPoint point) {
174 if ( ps.length <= this.count ) {
175 RPoint newps[] = new RPoint[getCount() + 20];
176 for (int i=0; i<this.count; i++)
177 newps[i] = ps[i];
178 ps = newps;
179 }
180 ps[this.count++] = point;
181 redraw();
182 return this;
183 }
184
185
191 public Polygon add(double x, double y) {
192 return add(new RPoint(x,y));
193 }
194
195
202 public Polygon add(double x, double y, double z) {
203 return add(new RPoint(x,y,z));
204 }
205
206 @Override
207 protected void drawShape(Graphics g, Matrix a) {
208 int[][] sPoints = makeScreenPoints(a);
209 g.drawPolygon(sPoints[0], sPoints[1], getCount());
210 }
211 }
212