* Calculate the overlapping area between two grid positions. * * @param a first position * @param b second position * @returns the area of overlap (0 if no overlap) * * @example * const overlap = Utils.areaIntercept( * {x: 0, y: 0, w: 3, h: 2}, * {x: 1, y: 0, w: 3, h:
(a: GridStackPosition, b: GridStackPosition)
| 276 | * ); // returns 4 (2x2 overlap) |
| 277 | */ |
| 278 | static areaIntercept(a: GridStackPosition, b: GridStackPosition): number { |
| 279 | const x0 = (a.x > b.x) ? a.x : b.x; |
| 280 | const x1 = (a.x+a.w < b.x+b.w) ? a.x+a.w : b.x+b.w; |
| 281 | if (x1 <= x0) return 0; // no overlap |
| 282 | const y0 = (a.y > b.y) ? a.y : b.y; |
| 283 | const y1 = (a.y+a.h < b.y+b.h) ? a.y+a.h : b.y+b.h; |
| 284 | if (y1 <= y0) return 0; // no overlap |
| 285 | return (x1-x0) * (y1-y0); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Calculate the total area of a grid position. |
no outgoing calls
no test coverage detected