Browse Source

fix acos bugs, simplify beta

Kirk Trombley 2 years ago
parent
commit
00ff1b9da0
1 changed files with 7 additions and 14 deletions
  1. 7 14
      main.js

+ 7 - 14
main.js

@@ -7,6 +7,7 @@ const vectorMag = v => Math.sqrt(vectorDot(v, v));
 const rad2deg = 180 / Math.PI;
 
 // Misc
+const clamp = (min, value, max) => Math.min(Math.max(value, min), max);
 const productLift =
   (...factors) =>
   (...args) =>
@@ -53,20 +54,12 @@ const buildClusterData = (
 ) => {
   const mu = buildVectorDataForSpace([mu1, mu2, mu3]);
   const nu = [nu1, nu2, nu3];
-  const muNuAngle = rad2deg * Math.acos(vectorDot(mu.unit, nu) / vectorMag(nu));
+  const muNuAngle =
+    rad2deg * Math.acos(clamp(-1, vectorDot(mu.unit, nu) / vectorMag(nu), 1));
   const proportion = size / totalSize;
-  const importance = // "Visual Importance"
-    mu.chroma +
-    Math.tanh(100 * (mu.chroma - 0.25)) + // penalty for being <25%
-    Math.tanh(100 * (mu.chroma - 0.4)) + // penalty for being <40%
-    mu.lightness +
-    Math.tanh(100 * (mu.lightness - 0.5)) + // penalty for being <50%
-    proportion +
-    Math.tanh(100 * (proportion - 0.05)) + // penalty for being <5%
-    Math.tanh(100 * (proportion - 0.1)) + // penalty for being <15%
-    Math.tanh(100 * (proportion - 0.15)) + // penalty for being <15%
-    Math.tanh(100 * (proportion - 0.25)) + // penalty for being <25%
-    Math.tanh(100 * (proportion - 0.8)); // penalty for being <50%
+  // "Visual Importance" - effectively a comparison where size is the
+  // biggest factor, then lightness, then chroma
+  const importance = 100 * proportion + 10 * mu.lightness + mu.chroma;
   return {
     size,
     inverseSize: 1 / size,
@@ -115,7 +108,7 @@ const calcScores = (data, target) => {
     bigTheta,
     alpha: sigma * Math.pow(bigTheta, target.chroma + target.lightness),
 
-    theta: rad2deg * Math.acos(vectorDot(data.mu.unit, target.unit)),
+    theta: rad2deg * Math.acos(clamp(-1, vectorDot(data.mu.unit, target.unit), 1)),
     phi: Math.min(rawPhi, 360 - rawPhi),
     delta: vectorMag(data.mu.vector.map((x, i) => x - target.vector[i])),
     manhattan: data.mu.vector