1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.vectomatic.common.model.geometry;
19
20 import com.google.gwt.user.client.rpc.IsSerializable;
21
22
23
24
25
26
27 public abstract class Segment implements IsSerializable {
28 protected Point[] _pts;
29 protected BoundingBox _bbox;
30
31 public Segment() {
32
33 }
34
35
36
37
38
39
40 public Segment(Point[] pts) {
41 _pts = new Point[pts.length];
42 for (int i = 0; i < pts.length; i++) {
43 _pts[i] = new Point(pts[i]);
44 }
45 _bbox = new BoundingBox();
46 _updateBoundingBox();
47 }
48
49
50
51
52
53
54 public Segment(Segment segment) {
55 _pts = new Point[segment._pts.length];
56 for (int i = 0; i < _pts.length; i++) {
57 _pts[i] = new Point(segment._pts[i]);
58 }
59 _bbox = new BoundingBox(segment._bbox);
60 }
61
62
63
64
65
66 public Point getStartPoint() {
67 return _pts[0];
68 }
69
70
71
72
73
74 public Point getEndPoint() {
75 return _pts[_pts.length - 1];
76 }
77
78
79
80
81
82
83 public BoundingBox getBoundingBox() {
84 return _bbox;
85 }
86
87 private void _updateBoundingBox() {
88 _bbox.xmin = _pts[0].x;
89 _bbox.ymin = _pts[0].y;
90 _bbox.xmax = _pts[0].x;
91 _bbox.ymax = _pts[0].y;
92 for (int i = 1; i < _pts.length; i++) {
93 if (_pts[i].x < _bbox.xmin) {
94 _bbox.xmin = _pts[i].x;
95 }
96 if (_pts[i].y < _bbox.ymin) {
97 _bbox.ymin = _pts[i].y;
98 }
99 if (_pts[i].x > _bbox.xmax) {
100 _bbox.xmax = _pts[i].x;
101 }
102 if (_pts[i].y > _bbox.ymax) {
103 _bbox.ymax = _pts[i].y;
104 }
105 }
106 }
107
108
109
110
111
112
113
114
115
116
117 public float squaredDistanceToPoint(Point p) {
118 Point nearestPoint = new Point();
119 nearestPointOnSegment(p, nearestPoint);
120 return nearestPoint.squareDistance(p);
121 }
122
123
124
125
126
127
128
129
130 public abstract void nearestPointOnSegment(Point p, Point dest);
131
132 public abstract Segment clone();
133
134 public Point[] getVertices() {
135 return _pts;
136 }
137
138 }