Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /** * General Transitive utilities library */ import SphericalMercator from 'sphericalmercator' const TOLERANCE = 0.000001 function fuzzyEquals(a, b, tolerance = TOLERANCE) { return Math.abs(a - b) < tolerance } function distance(x1, y1, x2, y2) { return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) } function getRadiusFromAngleChord(angleR, chordLen) { return chordLen / 2 / Math.sin(angleR / 2) } /* * CCW utility function. Accepts 3 coord pairs; result is positive if points * have counterclockwise orientation, negative if clockwise, 0 if collinear. */ function ccw(ax, ay, bx, by, cx, cy) { const raw = ccwRaw(ax, ay, bx, by, cx, cy) return raw === 0 ? 0 : raw / Math.abs(raw) } function ccwRaw(ax, ay, bx, by, cx, cy) { return (bx - ax) * (cy - ay) - (cx - ax) * (by - ay) } /* * Compute angle formed by three points in cartesian plane using law of cosines */ function angleFromThreePoints(ax, ay, bx, by, cx, cy) { const c = distance(ax, ay, bx, by) const a = distance(bx, by, cx, cy) const b = distance(ax, ay, cx, cy) return Math.acos((a * a + c * c - b * b) / (2 * a * c)) } function pointAlongArc(x1, y1, x2, y2, r, theta, ccw, t) { ccw = Math.abs(ccw) / ccw // convert to 1 or -1 let rot = Math.PI / 2 - Math.abs(theta) / 2 const vectToCenter = normalizeVector( rotateVector( { x: x2 - x1, y: y2 - y1 }, ccw * rot ) ) // calculate the center of the arc circle const cx = x1 + r * vectToCenter.x const cy = y1 + r * vectToCenter.y let vectFromCenter = negateVector(vectToCenter) rot = Math.abs(theta) * t * ccw vectFromCenter = normalizeVector(rotateVector(vectFromCenter, rot)) return { x: cx + r * vectFromCenter.x, y: cy + r * vectFromCenter.y } } function getVectorAngle(x, y) { let t = Math.atan(y / x) if (x < 0 && t <= 0) t += Math.PI else if (x < 0 && t >= 0) t -= Math.PI return t } function rayIntersection(ax, ay, avx, avy, bx, by, bvx, bvy) { const u = ((by - ay) * bvx - (bx - ax) * bvy) / (bvx * avy - bvy * avx) const v = ((by - ay) * avx - (bx - ax) * avy) / (bvx * avy - bvy * avx) return { intersect: u > -TOLERANCE && v > -TOLERANCE, u: u, v: v } } function lineIntersection(x1, y1, x2, y2, x3, y3, x4, y4) { const d = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4) if (d === 0) { // lines are parallel return { intersect: false } } return { intersect: true, x: ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / d, y: ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / d } } /** * Parse a pixel-based style descriptor, returning an number. * * @param {String/Number} */ function parsePixelStyle(descriptor) { if (typeof descriptor === 'number') return descriptor return parseFloat(descriptor.substring(0, descriptor.length - 2), 10) } /** * Whether vector is projected into positive xy quadrant. */ function isOutwardVector(vector) { return !fuzzyEquals(vector.x, 0) ? vector.x > 0 : vector.y > 0 } /** * vector utilities */ function normalizeVector(v) { const d = Math.sqrt(v.x * v.x + v.y * v.y) return { x: v.x / d, y: v.y / d } } function rotateVector(v, theta) { return { x: v.x * Math.cos(theta) - v.y * Math.sin(theta), y: v.x * Math.sin(theta) + v.y * Math.cos(theta) } } function negateVector(v) { return { x: -v.x, y: -v.y } } function addVectors(v1, v2) { return { x: v1.x + v2.x, y: v1.y + v2.y } } /** * GTFS utilities */ function otpModeToGtfsType(otpMode) { switch (otpMode) { case 'TRAM': return 0 case 'SUBWAY': return 1 case 'RAIL': return 2 case 'BUS': return 3 case 'FERRY': return 4 case 'CABLE_CAR': return 5 case 'GONDOLA': return 6 case 'FUNICULAR': return 7 } } // Rendering utilities function renderDataToSvgPath(renderData) { return renderData .map((d, k) => { if (k === 0) return `M${d.x} ${d.y}` if (d.arc) { return `A${d.radius} ${d.radius} ${d.arc} 0 ${d.arc > 0 ? 0 : 1} ${ d.x } ${d.y}` } return `L${d.x} ${d.y}` }) .join(' ') } // An instance of the SphericalMercator converter const sm = new SphericalMercator() /** * @param {*} fontSize A CSS font size or a numerical (pixel) font size. * @returns A CSS font size ending with the provided CSS unit or 'px' if none provided. */ function getFontSizeWithUnit(fontSize) { return fontSize + (isFinite(fontSize) ? 'px' : '') } export { fuzzyEquals, distance, getRadiusFromAngleChord, ccw, ccwRaw, angleFromThreePoints, pointAlongArc, getVectorAngle, rayIntersection, lineIntersection, parsePixelStyle, isOutwardVector, normalizeVector, rotateVector, negateVector, addVectors, otpModeToGtfsType, renderDataToSvgPath, sm, getFontSizeWithUnit } |