code wiki / _hdl_build / nx_geo_centroid_gate.nx

nx_geo_centroid_gate.nx source

↩ module page · 56 lines · 2775 B

1// nx_geo_centroid_gate.nx -- GATE for GEO-011 polygon centroid. Polygon = the 6e5 square with an EXTRA 2// collinear vertex (0,3e5) on its bottom edge (5 vertices). Proves: 3// area-weighted : centroid == (300000, 300000) -- the true center, UNCHANGED by the redundant vertex 4// vertex-mean : centroid == (240000, 300000) -- pulled toward the densely-sampled bottom edge 5// => the two DIFFER (lat 300000 vs 240000): area-weighted is the robust/correct centroid. 6// Both exact integers (no float drift). 7// 8// Evidence -> knowledge/status/geo_centroid.log (GEOCENTROIDGATE authored=organ ... verdict=GREEN). 9// license_tier: ORIGINAL 10import "nx_geo_centroid.nx" 11import "nx_syscalls.nx" 12 13const GC_LOG: *u8 = "knowledge/status/geo_centroid.log" 14 15func gc_w(fd: i64, s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(fd, s, n); return 0 } 16func gc_wn(fd: i64, v: i64) -> i64 { let bb: *u8 = sys_mmap(28); var m: i64=v; if m<0 {m=0-m; sys_write(fd,"-" as *u8,1)}; let t: *u8 = sys_mmap(28); var k: i64=0; if m==0 {t[0]=48;k=1}; while m>0 {t[k]=(48+(m%10)) as u8; m=m/10; k=k+1}; var i: i64=0; while i<k {bb[i]=t[k-1-i]; i=i+1}; sys_write(fd, bb, k); return 0 } 17 18func gc_set(p: *i64, i: i64, lat: i64, lon: i64) -> i64 { p[i * 2] = lat; p[i * 2 + 1] = lon; return 0 } 19 20func gc_emit(fd: i64, alat: i64, alon: i64, vlat: i64, vlon: i64, ok: i64) -> i64 { 21 gc_w(fd, "GEOCENTROIDGATE authored=organ area_lat=" as *u8); gc_wn(fd, alat) 22 gc_w(fd, " area_lon=" as *u8); gc_wn(fd, alon) 23 gc_w(fd, " vertex_lat=" as *u8); gc_wn(fd, vlat) 24 gc_w(fd, " vertex_lon=" as *u8); gc_wn(fd, vlon) 25 if ok == 1 { gc_w(fd, " verdict=GREEN\n" as *u8) } else { gc_w(fd, " verdict=RED\n" as *u8) } 26 return 0 27} 28 29func main() -> i64 { 30 // CCW square [0,6e5]^2 with extra collinear vertex (0, 3e5) on the bottom (lat=0) edge. 31 let poly: *i64 = sys_mmap(8 * 10) as *i64 32 gc_set(poly, 0, 0, 0) 33 gc_set(poly, 1, 0, 300000) // redundant collinear bottom-edge vertex 34 gc_set(poly, 2, 0, 600000) 35 gc_set(poly, 3, 600000, 600000) 36 gc_set(poly, 4, 600000, 0) 37 38 let ca: *i64 = sys_mmap(16) as *i64 39 geo_centroid_area(poly, 5, ca) 40 let cv: *i64 = sys_mmap(16) as *i64 41 geo_centroid_vertex(poly, 5, cv) 42 43 var ok: i64 = 1 44 if ca[0] != 300000 { ok = 0 } // area-weighted true center 45 if ca[1] != 300000 { ok = 0 } 46 if cv[0] != 240000 { ok = 0 } // vertex-mean pulled down 47 if cv[1] != 300000 { ok = 0 } 48 if ca[0] == cv[0] { ok = 0 } // they MUST differ (robustness vs sampling) 49 50 gc_emit(1, ca[0], ca[1], cv[0], cv[1], ok) 51 let lf: i64 = sys_openat_append(GC_LOG, 420) 52 if lf >= 0 { gc_emit(lf, ca[0], ca[1], cv[0], cv[1], ok); sys_close(lf) } 53 54 if ok == 1 { return 0 } 55 return 1 56}