From 97adbc28c05505d96f541de0c5c709a1865a70ac Mon Sep 17 00:00:00 2001 From: Zeno Rogue Date: Wed, 27 Nov 2019 21:08:09 +0100 Subject: [PATCH] Euclidean distance restored --- cell.cpp | 16 +++++----------- euclid.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/cell.cpp b/cell.cpp index 59dabc5d..a66f4501 100644 --- a/cell.cpp +++ b/cell.cpp @@ -417,16 +417,6 @@ EX void verifycells(heptagon *at) { verifycell(at->c7); } -EX int eudist(int sx, int sy) { - int z0 = abs(sx); - int z1 = abs(sy); - if(a4 && BITRUNCATED) - return (z0 == z1 && z0 > 0) ? z0+1: max(z0, z1); - if(a4) return z0 + z1; - int z2 = abs(sx+sy); - return max(max(z0,z1), z2); - } - EX int compdist(int dx[]) { int mi = dx[0]; for(int u=0; ugamestart(), c); + if(euclid) return celldistance(currentmap->gamestart(), c); if(sphere || binarytiling || WDIM == 3 || cryst || solnih || penrose) return celldistance(currentmap->gamestart(), c); #if CAP_IRR if(IRREGULAR) return irr::celldist(c, false); @@ -1026,6 +1016,10 @@ EX int celldistance(cell *c1, cell *c2) { if(cryst) return crystal::precise_distance(c1, c2); #endif + if(euclid && WDIM == 2) { + return cyldist(euc2_coordinates(c1), euc2_coordinates(c2)); + } + if(archimedean || quotient || solnih || (penrose && euclid) || experimental || sl2 || nil) { if(saved_distances.count(make_pair(c1,c2))) diff --git a/euclid.cpp b/euclid.cpp index ea6d5ae0..f4113c06 100644 --- a/euclid.cpp +++ b/euclid.cpp @@ -901,4 +901,50 @@ EX bool chiral(gp::loc g) { EX euclid3::coord first_period() { return 0; } +EX void twist_once(gp::loc coo) { + coo = coo - euclid3::twisted_vec * gp::univ_param(); + if(euclid3::twisted&8) { + gp::loc ort = (S3 == 3 ? gp::loc(1, -2) : gp::loc(0, 1)) * euclid3::twisted_vec * gp::univ_param(); + auto s = ort * dscalar(coo, ort) * 2; + auto v = dscalar(ort, ort); + s.first /= v; + s.second /= v; + coo = coo - s; + } + } + +EX int eudist(int sx, int sy, bool reduce IS(true)) { + int z0 = abs(sx); + int z1 = abs(sy); + if(a4 && BITRUNCATED) + return (z0 == z1 && z0 > 0 && reduce) ? z0+1: max(z0, z1); + if(a4) return z0 + z1; + int z2 = abs(sx+sy); + return max(max(z0,z1), z2); + } + +EX int eudist(gp::loc a, gp::loc b) { + return eudist(a.first-b.first, a.second-b.second, (a.first ^ a.second)&1); + } + +EX int cyldist(gp::loc a, gp::loc b) { + a = as_gp(euclid3::getcoord(euclid3::canonicalize(as_coord(a)))); + b = as_gp(euclid3::getcoord(euclid3::canonicalize(as_coord(b)))); + + if(!quotient) return eudist(a, b); + + int best = 0; + for(int sa=0; sa<16; sa++) { + auto _a = a, _b = b; + if(sa&1) twist_once(_a); + if(sa&2) twist_once(_b); + if(sa&4) _a = _a + euclid3::ortho_vec * gp::univ_param(); + if(sa&8) _b = _b + euclid3::ortho_vec * gp::univ_param(); + int val = eudist(_a, _b); + if(sa == 0 || val < best) best = val; + } + + return best; + } + }