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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | 1x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /** * 2D vector. */ export interface Vector2d { x: number; y: number; } /** * 2D plane. */ export interface Plane2d { A: Vector2d; B: Vector2d; C: Vector2d; D: Vector2d; } /** * Origin vector. */ export const originVector: Vector2d = { x: 0, y: 0 }; /** * Clone a vector. * * @param vector The vector to clone. * * @returns A new instance of Vector2d with the same coordinates. */ export function clone(vector: Vector2d): Vector2d { return { x: vector.x, y: vector.y }; } /** * Compute the euclidean norm of a 2D vector. * * @param vector The vector for which the euclidean norm must be computed. * * @returns The norm of the input vector. */ export function norm(vector: Vector2d): number { return Math.sqrt(vector.x * vector.x + vector.y * vector.y); } /** * Get the normal vector of a 2D line (another 2D vector). * * @param vector The vector for which the normal must be computed. * * @returns A new 2D vector normal to the input. */ export function normal(vector: Vector2d): Vector2d { const vectorNorm = norm(vector); return { x: -vector.y / vectorNorm, y: vector.x / vectorNorm }; } /** * Add two vectors. * * @param vector1 A Vector2d. * @param vector2 Another Vector2d to add to the first. * * @returns A new Vector2d corresponding to the addition of the inputs. */ export function add(vector1: Vector2d, vector2: Vector2d): Vector2d { return { x: vector1.x + vector2.x, y: vector1.y + vector2.y }; } /** * Subtract two vectors. * * @param vector1 A Vector2d to subtract from. * @param vector2 The Vector2d to subtract from the first. * * @returns A new Vector2d corresponding to the subtraction of the two inputs. */ export function subtract(vector1: Vector2d, vector2: Vector2d): Vector2d { return { x: vector1.x - vector2.x, y: vector1.y - vector2.y }; } /** * 2D cross product between vectors. * * @param vector1 The first vector in the product. * @param vector2 The second vector in the product. * * @returns The result of the 2D cross product between the vectors. */ export function crossProduct(vector1: Vector2d, vector2: Vector2d): number { return vector1.x * vector2.y - vector1.y * vector2.x; } /** * Multiply a vector by a scalar. * * @param vector The Vector2d which must be multiplied by a scalar. * @param scalar The scalar to multiply the vector by. * * @returns A new Vector2d corresponding to the result of the multiplication. */ export function scalarMultiplication( vector: Vector2d, scalar: number ): Vector2d { return { x: vector.x * scalar, y: vector.y * scalar }; } /** * Divide a vector by a scalar. * * @param vector The Vector2d which must be divided by a scalar. * @param scalar The scalar to divide the vector by. * * @returns A new Vector2d corresponding to the result of the division. */ export function scalarDivision( vector: Vector2d, scalar: number ): Vector2d { return { x: vector.x / scalar, y: vector.y / scalar }; } /** * Convert a Vector2d to an array of numbers. * * @param vector The Vector2d to convert to an array. * * @returns An array containing the x and y coordinates of the input vector. */ export function vectorToArray(vector: Vector2d): Array<number> { return [vector.x, vector.y]; } /** * Convert a an array of 2 numbers to a Vector2d. * * @param vector The Vector2d to convert to an array. * * @returns An array containing the x and y coordinates of the input vector. */ export function arrayToVector(array: number[]): Vector2d { return { x: array[0], y: array[1] }; } /** * Transform coordinates from a device plane to a map plane. * * We use the barycentric coordinate system to apply linear transformation helping * to find the final coordinates in an well scaled plan from a deformed plan * * Algo: * - Divide the plan between two triangles * - Find if a point is on the left or on the right of BC * - Apply barycentric coordinate system to find alpha, beta and gamma * parameters * - Thanks to these parameters, transform the coordinates according * to the well scaled vertices. * * * A ------ C A --------- C * / __/ | | ___/ | * | __/ \ => | ___/ | * / / x \ | / x | * B -----_____ D B --------- D * * @param pressedPoint The point pressed on the device plane. * @param devicePlane The device's plane. * @param mapPlane The map plane to transform the coordinates to. */ export function transformCoordinates( pressedPoint: Vector2d, devicePlane: Plane2d, mapPlane: Plane2d ): Vector2d { if (devicePlane.A === undefined || devicePlane.B === undefined || devicePlane.C === undefined || devicePlane.D === undefined) { return {x: 0, y: 0} as Vector2d; } const PA = subtract(devicePlane.A, pressedPoint); const PB = subtract(devicePlane.B, pressedPoint); const PC = subtract(devicePlane.C, pressedPoint); const PD = subtract(devicePlane.D, pressedPoint); const BC = subtract(devicePlane.C, devicePlane.B); const BP = subtract(pressedPoint, devicePlane.B); let PX; let mapX; if (crossProduct(BP, BC) >= 0) { /* * A --------- C * | P ___/ | * | ___/ | * | / | * B --------- D */ PX = PA; mapX = mapPlane.A; } else { /* * A --------- C * | ___/ | * | ___/ | * | / P | * B --------- D */ PX = PD; mapX = mapPlane.D; } // Apply barycentric coordinates system to find the main parameters const alpha = crossProduct(PB, PC); const beta = crossProduct(PC, PX); const gamma = crossProduct(PX, PB); // transformation. P_ is P' const total = alpha + beta + gamma; const P_ = add( scalarMultiplication(mapX, alpha), add( scalarMultiplication(mapPlane.B, beta), scalarMultiplication(mapPlane.C, gamma) ) ); P_.x /= total; P_.y /= total; return P_; } |