From 475f9de8aa46bf442da879e56914ff176e3791e0 Mon Sep 17 00:00:00 2001 From: Timothy Pratley Date: Tue, 27 Dec 2022 14:58:21 -0800 Subject: [PATCH 1/4] fix-333-allow-dimension-based-distances Convert dx*dx into a property function so that it can be overriden but users who would like to vary the descent distance by dimension. This is useful when laying out rectangles, because it allows one axis to be weighted more heavily than another axis. --- src/descent.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/descent.ts b/src/descent.ts index 63a14615..4a7bd1e1 100644 --- a/src/descent.ts +++ b/src/descent.ts @@ -103,6 +103,14 @@ DEBUG */ private random = new PseudoRandom(); public project: { (x0: number[], y0: number[], r: number[]): void }[] = null; + /** The dimension distance squared calculation (defaults to x*x) + * @property dimensionDistanceSquared {(number, number) => number} + * Can be replaced with a custom function. + * For example replacing with (i, x) => i==0? x*x/16 : x*x; + * has the effect of making horizontal distances larger than vertical + * distances. + */ + public dimensionDistanceSquared: (i: number, x: number) => number = (i, x) => x * x; /** * @method constructor @@ -215,7 +223,7 @@ DEBUG */ distanceSquared = 0; for (i = 0; i < this.k; ++i) { const dx = d[i] = x[i][u] - x[i][v]; - distanceSquared += d2[i] = dx * dx; + distanceSquared += d2[i] = this.dimensionDistanceSquared(i,dx); } if (distanceSquared > 1e-9) break; const rd = this.offsetDir(); From 35dae426e427ef847764bf79da71522a38045336 Mon Sep 17 00:00:00 2001 From: Timothy Pratley Date: Tue, 27 Dec 2022 22:13:29 -0800 Subject: [PATCH 2/4] make dimension functions static for convenience --- src/descent.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/descent.ts b/src/descent.ts index 4a7bd1e1..1c54b222 100644 --- a/src/descent.ts +++ b/src/descent.ts @@ -103,14 +103,23 @@ DEBUG */ private random = new PseudoRandom(); public project: { (x0: number[], y0: number[], r: number[]): void }[] = null; - /** The dimension distance squared calculation (defaults to x*x) + /** The dimension distance calculation (defaults to x[i][u]-x[i][v]) + * @property dimensionDistance{(number[][], number, number, number) => number} + * Can be replaced with a custom function. + * For example replacing with (x, i, u, v) => i==0 ? (x[i][u]-x[i][v])/4 : x[i][u]-x[i][v]; + * has the effect of making horizontal distances larger than vertical + * distances. + */ + public static dimensionDistance: (x: number[][], i: number, u: number, v: number) => number = (x, i, u, v) => x[i][u] - x[i][v]; + + /** The dimension distance squared calculation (defaults to dx*dx) * @property dimensionDistanceSquared {(number, number) => number} * Can be replaced with a custom function. - * For example replacing with (i, x) => i==0? x*x/16 : x*x; + * For example replacing with (i, dx) => i==0? dx*dx/16 : dx*dx; * has the effect of making horizontal distances larger than vertical * distances. */ - public dimensionDistanceSquared: (i: number, x: number) => number = (i, x) => x * x; + public static dimensionDistanceSquared: (i: number, dx: number) => number = (i, dx) => dx * dx; /** * @method constructor @@ -222,7 +231,7 @@ DEBUG */ while (maxDisplaces--) { distanceSquared = 0; for (i = 0; i < this.k; ++i) { - const dx = d[i] = x[i][u] - x[i][v]; + const dx = d[i] = this.dimensionDistance(x, i, u, v); distanceSquared += d2[i] = this.dimensionDistanceSquared(i,dx); } if (distanceSquared > 1e-9) break; From 1c5fbbbe8236fcdb0d463bb4131ef8b954065c75 Mon Sep 17 00:00:00 2001 From: Timothy Pratley Date: Wed, 28 Dec 2022 00:11:16 -0800 Subject: [PATCH 3/4] fix static calls --- src/descent.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/descent.ts b/src/descent.ts index 1c54b222..dc3f2a64 100644 --- a/src/descent.ts +++ b/src/descent.ts @@ -110,7 +110,9 @@ DEBUG */ * has the effect of making horizontal distances larger than vertical * distances. */ - public static dimensionDistance: (x: number[][], i: number, u: number, v: number) => number = (x, i, u, v) => x[i][u] - x[i][v]; + public static dimensionDistance(x: number[][], i: number, u: number, v: number): number { + return x[i][u] - x[i][v]; + } /** The dimension distance squared calculation (defaults to dx*dx) * @property dimensionDistanceSquared {(number, number) => number} @@ -119,7 +121,9 @@ DEBUG */ * has the effect of making horizontal distances larger than vertical * distances. */ - public static dimensionDistanceSquared: (i: number, dx: number) => number = (i, dx) => dx * dx; + public static dimensionDistanceSquared(i: number, dx: number): number { + return dx * dx; + } /** * @method constructor @@ -231,8 +235,8 @@ DEBUG */ while (maxDisplaces--) { distanceSquared = 0; for (i = 0; i < this.k; ++i) { - const dx = d[i] = this.dimensionDistance(x, i, u, v); - distanceSquared += d2[i] = this.dimensionDistanceSquared(i,dx); + const dx = d[i] = Descent.dimensionDistance(x, i, u, v); + distanceSquared += d2[i] = Descent.dimensionDistanceSquared(i,dx); } if (distanceSquared > 1e-9) break; const rd = this.offsetDir(); From ec2a8c720604cf23927cad89af532d781474b58a Mon Sep 17 00:00:00 2001 From: Timothy Pratley Date: Fri, 30 Dec 2022 12:55:56 -0800 Subject: [PATCH 4/4] remove dimensionDistanceSquared (redundant) --- src/descent.ts | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/descent.ts b/src/descent.ts index dc3f2a64..0c31a509 100644 --- a/src/descent.ts +++ b/src/descent.ts @@ -114,17 +114,6 @@ DEBUG */ return x[i][u] - x[i][v]; } - /** The dimension distance squared calculation (defaults to dx*dx) - * @property dimensionDistanceSquared {(number, number) => number} - * Can be replaced with a custom function. - * For example replacing with (i, dx) => i==0? dx*dx/16 : dx*dx; - * has the effect of making horizontal distances larger than vertical - * distances. - */ - public static dimensionDistanceSquared(i: number, dx: number): number { - return dx * dx; - } - /** * @method constructor * @param x {number[][]} initial coordinates for nodes @@ -236,7 +225,7 @@ DEBUG */ distanceSquared = 0; for (i = 0; i < this.k; ++i) { const dx = d[i] = Descent.dimensionDistance(x, i, u, v); - distanceSquared += d2[i] = Descent.dimensionDistanceSquared(i,dx); + distanceSquared += d2[i] = dx * dx; } if (distanceSquared > 1e-9) break; const rd = this.offsetDir();