|
| 1 | +import { calcPlus, calcMinus, calcDiv } from './strict-calculator'; |
| 2 | +import { BoundingPoints, BoundingBox, Coords, EllipseItemBounds } from '@/types'; |
| 3 | + |
| 4 | +export class BoundsTransformer { |
| 5 | + bounds: BoundingPoints; |
| 6 | + |
| 7 | + static fromBBox (bBox: BoundingBox) { |
| 8 | + return new BoundsTransformer({ |
| 9 | + x1: bBox.x, |
| 10 | + y1: bBox.y, |
| 11 | + x2: calcPlus(bBox.x, bBox.width), |
| 12 | + y2: calcPlus(bBox.y, bBox.height) |
| 13 | + }); |
| 14 | + } |
| 15 | + |
| 16 | + constructor (bounds: BoundingPoints) { |
| 17 | + this.bounds = bounds; |
| 18 | + } |
| 19 | + |
| 20 | + relativeFrom (coords: Coords): BoundsTransformer { |
| 21 | + return new BoundsTransformer({ |
| 22 | + x1: calcMinus(this.bounds.x1, coords.x), |
| 23 | + y1: calcMinus(this.bounds.y1, coords.y), |
| 24 | + x2: calcMinus(this.bounds.x2, coords.x), |
| 25 | + y2: calcMinus(this.bounds.y2, coords.y) |
| 26 | + }); |
| 27 | + } |
| 28 | + |
| 29 | + expand (coords: Coords): BoundsTransformer { |
| 30 | + return new BoundsTransformer({ |
| 31 | + x1: calcPlus(this.bounds.x1, coords.x), |
| 32 | + y1: calcPlus(this.bounds.y1, coords.y), |
| 33 | + x2: calcPlus(this.bounds.x2, coords.x), |
| 34 | + y2: calcPlus(this.bounds.y2, coords.y) |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + toBPoints (): BoundingPoints { |
| 39 | + return { ...this.bounds }; |
| 40 | + } |
| 41 | + |
| 42 | + toBBox (): BoundingBox { |
| 43 | + return { |
| 44 | + x: Math.min(this.bounds.x1, this.bounds.x2), |
| 45 | + y: Math.min(this.bounds.y1, this.bounds.y2), |
| 46 | + width: Math.abs(calcMinus(this.bounds.x2, this.bounds.x1)), |
| 47 | + height: Math.abs(calcMinus(this.bounds.y2, this.bounds.y1)) |
| 48 | + }; |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +export const convertBPointsToEllipseItemBounds = (bPoints: BoundingPoints): EllipseItemBounds => { |
| 53 | + const bBox = new BoundsTransformer(bPoints).toBBox(); |
| 54 | + const rx = calcDiv(bBox.width, 2); |
| 55 | + const ry = calcDiv(bBox.height, 2); |
| 56 | + |
| 57 | + return { |
| 58 | + cx: calcPlus(bBox.x, rx), |
| 59 | + cy: calcPlus(bBox.y, ry), |
| 60 | + rx, |
| 61 | + ry |
| 62 | + }; |
| 63 | +}; |
0 commit comments