-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Point.js
119 lines (110 loc) · 2.95 KB
/
Point.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* @module ol/geom/Point
*/
import SimpleGeometry from './SimpleGeometry.js';
import {containsXY, createOrUpdateFromCoordinate} from '../extent.js';
import {deflateCoordinate} from './flat/deflate.js';
import {squaredDistance as squaredDx} from '../math.js';
/**
* @classdesc
* Point geometry.
*
* @api
*/
class Point extends SimpleGeometry {
/**
* @param {import("../coordinate.js").Coordinate} coordinates Coordinates.
* @param {import("./Geometry.js").GeometryLayout} [layout] Layout.
*/
constructor(coordinates, layout) {
super();
this.setCoordinates(coordinates, layout);
}
/**
* Make a complete copy of the geometry.
* @return {!Point} Clone.
* @api
*/
clone() {
const point = new Point(this.flatCoordinates.slice(), this.layout);
point.applyProperties(this);
return point;
}
/**
* @param {number} x X.
* @param {number} y Y.
* @param {import("../coordinate.js").Coordinate} closestPoint Closest point.
* @param {number} minSquaredDistance Minimum squared distance.
* @return {number} Minimum squared distance.
*/
closestPointXY(x, y, closestPoint, minSquaredDistance) {
const flatCoordinates = this.flatCoordinates;
const squaredDistance = squaredDx(
x,
y,
flatCoordinates[0],
flatCoordinates[1],
);
if (squaredDistance < minSquaredDistance) {
const stride = this.stride;
for (let i = 0; i < stride; ++i) {
closestPoint[i] = flatCoordinates[i];
}
closestPoint.length = stride;
return squaredDistance;
}
return minSquaredDistance;
}
/**
* Return the coordinate of the point.
* @return {import("../coordinate.js").Coordinate} Coordinates.
* @api
*/
getCoordinates() {
return this.flatCoordinates.slice();
}
/**
* @param {import("../extent.js").Extent} extent Extent.
* @protected
* @return {import("../extent.js").Extent} extent Extent.
*/
computeExtent(extent) {
return createOrUpdateFromCoordinate(this.flatCoordinates, extent);
}
/**
* Get the type of this geometry.
* @return {import("./Geometry.js").Type} Geometry type.
* @api
*/
getType() {
return 'Point';
}
/**
* Test if the geometry and the passed extent intersect.
* @param {import("../extent.js").Extent} extent Extent.
* @return {boolean} `true` if the geometry and the extent intersect.
* @api
*/
intersectsExtent(extent) {
return containsXY(extent, this.flatCoordinates[0], this.flatCoordinates[1]);
}
/**
* @param {!Array<*>} coordinates Coordinates.
* @param {import("./Geometry.js").GeometryLayout} [layout] Layout.
* @api
*/
setCoordinates(coordinates, layout) {
this.setLayout(layout, coordinates, 0);
if (!this.flatCoordinates) {
this.flatCoordinates = [];
}
this.flatCoordinates.length = deflateCoordinate(
this.flatCoordinates,
0,
coordinates,
this.stride,
);
this.changed();
}
}
export default Point;