mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-24 04:56:00 +00:00
solve overlaps alg layout
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { arrayDifference } from 'interval-operations';
|
||||
import _ from 'lodash';
|
||||
export interface IPoint {
|
||||
x: number;
|
||||
y: number;
|
||||
@@ -134,3 +136,27 @@ export class Vector2D {
|
||||
return this.divide(this.magnitude());
|
||||
}
|
||||
}
|
||||
|
||||
export function solveOverlapsInIntervalArray(position: number, size: number, usedIntervals: [number, number][]) {
|
||||
const freeIntervals = arrayDifference([[-Infinity, Infinity]], usedIntervals) as [number, number][];
|
||||
|
||||
const candidates = [];
|
||||
|
||||
for (const interval of freeIntervals) {
|
||||
const intervalSize = interval[1] - interval[0];
|
||||
if (intervalSize < size) continue;
|
||||
if (interval[1] < position) {
|
||||
candidates.push(interval[1] - size / 2);
|
||||
} else if (interval[0] > position) {
|
||||
candidates.push(interval[0] - size / 2);
|
||||
} else {
|
||||
// position is in interval
|
||||
let candidate = position;
|
||||
if (candidate - size / 2 < interval[0]) candidate = interval[0] + size / 2;
|
||||
if (candidate + size / 2 > interval[1]) candidate = interval[1] - size / 2;
|
||||
candidates.push(candidate);
|
||||
}
|
||||
}
|
||||
|
||||
return _.minBy(candidates, x => Math.abs(x - position));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user