View Javadoc

1   /**********************************************
2    * Copyright (C) 2009 Lukas Laag
3    * This file is part of Vectomatic.
4    * 
5    * Vectomatic is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    * 
10   * Vectomatic is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License
16   * along with Vectomatic.  If not, see http://www.gnu.org/licenses/
17   **********************************************/
18  package org.vectomatic.common.model.geometry;
19  
20  import org.vectomatic.common.model.IShapeVisitor;
21  import org.vectomatic.common.model.Shape;
22  
23  import com.google.gwt.user.client.rpc.IsSerializable;
24  
25  /**
26   * Class to represent a polyline
27   */
28  public class Polyline extends Shape implements IsSerializable {
29  	private Point[] _pts;
30  	
31  	public Polyline() {
32  		// For GWT serialization
33  	}
34  	public Polyline(Point[] pts, int count) {
35  		super();
36  		_pts = new Point[count];
37  		for (int i = 0; i < count; i++) {
38  			_pts[i] = new Point(pts[i]);
39  		}
40  		_bbox = new BoundingBox();
41  		_updateBoundingBox();
42  		updateTransform();
43  	}
44  
45  	public Polyline(Polyline polyline) {
46  		super(polyline);
47  		_pts = new Point[polyline._pts.length];
48  		for (int i = 0; i < _pts.length; i++) {
49  			_pts[i] = new Point(polyline._pts[i]);
50  		}
51  	}
52  	
53  	public boolean isClosed() {
54  		return _pts[0].equals(_pts[_pts.length - 1]);
55  	}
56  	
57  	@Override
58  	public void acceptVisitor(IShapeVisitor visitor) {
59  		visitor.visitPolyline(this);
60  	}
61  	
62  	@Override
63  	public boolean isSame(Shape shape) {
64  		if (shape instanceof Polyline) {
65  			Polyline polyline = (Polyline)shape;
66  			if (getTransform().equals(polyline.getTransform())) {
67  				if (_pts.length == polyline._pts.length) {
68  					for (int i = 0; i < _pts.length; i++) {
69  						if (!_pts[i].equals(polyline._pts[i])) {
70  							return false;
71  						}
72  					}
73  					return _attributes.equals(polyline._attributes);
74  				}
75  			}
76  		}
77  		return false;
78  	}
79  
80  	
81  	public Point[] getVertices() {
82  		return _pts;
83  	}
84  	
85  	public void setVertices(Point[] pts) {
86  		_pts = pts;
87  		Point P0 = pts[0].transform(_m, new Point());
88  		Point P1 = pts[1].transform(_m, new Point());
89  		_updateBoundingBox();
90  		Point C = _bbox.getPoint(BoundingBox.PT_C, new Point());
91  		float cos = (float)Math.cos(_r);
92  		float sin = (float)Math.sin(_r);
93  		
94  		float d = sin * (pts[1].y - pts[0].y) * sin * (pts[0].x - pts[1].x) - cos * (pts[0].y - pts[1].y) * cos * (pts[0].x - pts[1].x);
95  		_s.x = ((P1.x - P0.x) * cos * (pts[0].y - pts[1].y) - (P1.y - P0.y) * sin * (pts[1].y - pts[0].y)) / d;
96  		_s.y = ((P0.x - P1.x) * sin * (pts[0].x - pts[1].x) - (P0.y - P1.y) * cos * (pts[0].x - pts[1].x)) / d; 
97  		
98  		_t.x = P0.x - _s.x * cos * (pts[0].x - C.x) - _s.y * sin * (C.y - pts[0].y);
99  		_t.y = P0.y - _s.x * sin * (pts[0].x - C.x) - _s.y * cos * (pts[0].y - C.y);
100 		_dirty = true;
101 		updateTransform();
102 	}
103 	
104 	private void _updateBoundingBox() {
105 		_bbox.xmin = _pts[0].x;
106 		_bbox.ymin = _pts[0].y;
107 		_bbox.xmax = _pts[0].x;
108 		_bbox.ymax = _pts[0].y;
109 		for (int i = 1; i < _pts.length; i++) {
110 			if (_pts[i].x < _bbox.xmin) {
111 				_bbox.xmin = _pts[i].x;
112 			}
113 			if (_pts[i].y < _bbox.ymin) {
114 				_bbox.ymin = _pts[i].y;
115 			}
116 			if (_pts[i].x > _bbox.xmax) {
117 				_bbox.xmax = _pts[i].x;
118 			}
119 			if (_pts[i].y > _bbox.ymax) {
120 				_bbox.ymax = _pts[i].y;
121 			}
122 		}
123 		_bbox.getPoint(BoundingBox.PT_C, _t);
124 	}
125 	
126 	@Override
127 	public String toString() {
128 		StringBuffer buffer = new StringBuffer();
129 		buffer.append("Polyline(m=");
130 		buffer.append(getTransform().toString());
131 		buffer.append(" bbox=");
132 		buffer.append(_bbox.toString());
133 		buffer.append(" vertices=");
134 		for (int i = 0; i < _pts.length; i++) {
135 			if (i > 0) {
136 				buffer.append(", ");
137 			}
138 			buffer.append(_pts[i].toString());
139 		}
140 		buffer.append(" atts=");
141 		buffer.append(_attributes.toString());
142 		buffer.append(")");
143 		return buffer.toString();
144 	}
145 }