code wiki / _hdl_build / nx_geo_hull_gate.nx
nx_geo_hull_gate.nx source
↩ module page · 90 lines · 4172 B
1// nx_geo_hull_gate.nx -- GATE for GEO-012 convex hull (integer-exact monotone chain). Input: the 4
2// corners of a 4e6-microdeg square PLUS an interior point and a collinear bottom-edge point. Proves:
3// hull_size == 4 : interior + collinear points are excluded (minimal exact hull)
4// has_interior == 0 : the interior point (idx 4) is not a hull vertex
5// has_collinear == 0 : the collinear edge point (idx 5) is not a hull vertex
6// hull_area2 == 3.2e13 : the hull encloses EXACTLY the square's doubled area (2 * (4e6)^2) --
7// cross-checked by REUSING GEO-010 geo_area2, so the hull is provably
8// the right polygon without hard-coding a vertex order
9// hull_orient == +1 : counter-clockwise (the monotone-chain output orientation)
10// n2 == 2 : degenerate hull of 2 points returns both
11//
12// Evidence -> knowledge/status/geo_hull.log (GEOHULLGATE authored=organ ... verdict=GREEN).
13// license_tier: ORIGINAL
14import "nx_geo_hull.nx"
15import "nx_geo_area.nx"
16import "nx_syscalls.nx"
17
18const GHL_LOG: *u8 = "knowledge/status/geo_hull.log"
19
20func ghl_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 }
21func ghl_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 }
22
23func ghl_set(p: *i64, i: i64, lat: i64, lon: i64) -> i64 { p[i * 2] = lat; p[i * 2 + 1] = lon; return 0 }
24
25func ghl_emit(fd: i64, hsz: i64, area2: i64, orient: i64, hasI: i64, hasC: i64, n2: i64, ok: i64) -> i64 {
26 ghl_w(fd, "GEOHULLGATE authored=organ method=integer-monotone-chain hull_size=" as *u8); ghl_wn(fd, hsz)
27 ghl_w(fd, " hull_area2=" as *u8); ghl_wn(fd, area2)
28 ghl_w(fd, " hull_orient=" as *u8); ghl_wn(fd, orient)
29 ghl_w(fd, " has_interior=" as *u8); ghl_wn(fd, hasI)
30 ghl_w(fd, " has_collinear=" as *u8); ghl_wn(fd, hasC)
31 ghl_w(fd, " n2=" as *u8); ghl_wn(fd, n2)
32 if ok == 1 { ghl_w(fd, " verdict=GREEN\n" as *u8) } else { ghl_w(fd, " verdict=RED\n" as *u8) }
33 return 0
34}
35
36func main() -> i64 {
37 let pts: *i64 = sys_mmap(8 * 12) as *i64
38 ghl_set(pts, 0, 0, 0) // P0 corner (x=0,y=0)
39 ghl_set(pts, 1, 0, 4000000) // P1 corner (x=4e6,y=0)
40 ghl_set(pts, 2, 4000000, 4000000) // P2 corner (x=4e6,y=4e6)
41 ghl_set(pts, 3, 4000000, 0) // P3 corner (x=0,y=4e6)
42 ghl_set(pts, 4, 2000000, 2000000) // P4 interior
43 ghl_set(pts, 5, 0, 2000000) // P5 collinear on bottom edge (x=2e6,y=0)
44
45 let out: *i64 = sys_mmap(8 * 6) as *i64
46 let hsz: i64 = geo_convex_hull(pts, 6, out)
47
48 // gather hull polygon vertices, then verify enclosed area via GEO-010.
49 let hp: *i64 = sys_mmap(8 * 12) as *i64
50 var i: i64 = 0
51 while i < hsz {
52 hp[i * 2] = pts[out[i] * 2]
53 hp[i * 2 + 1] = pts[out[i] * 2 + 1]
54 i = i + 1
55 }
56 let area2: i64 = geo_area2(hp, hsz)
57 let orient: i64 = geo_orientation(hp, hsz)
58
59 // are the interior (4) / collinear (5) indices present in the hull?
60 var hasI: i64 = 0
61 var hasC: i64 = 0
62 i = 0
63 while i < hsz {
64 if out[i] == 4 { hasI = 1 }
65 if out[i] == 5 { hasC = 1 }
66 i = i + 1
67 }
68
69 // degenerate hull of 2 points.
70 let two: *i64 = sys_mmap(8 * 4) as *i64
71 ghl_set(two, 0, 0, 0)
72 ghl_set(two, 1, 1000000, 1000000)
73 let out2: *i64 = sys_mmap(8 * 2) as *i64
74 let n2: i64 = geo_convex_hull(two, 2, out2)
75
76 var ok: i64 = 1
77 if hsz != 4 { ok = 0 }
78 if area2 != 32000000000000 { ok = 0 }
79 if orient != 1 { ok = 0 }
80 if hasI != 0 { ok = 0 }
81 if hasC != 0 { ok = 0 }
82 if n2 != 2 { ok = 0 }
83
84 ghl_emit(1, hsz, area2, orient, hasI, hasC, n2, ok)
85 let lf: i64 = sys_openat_append(GHL_LOG, 420)
86 if lf >= 0 { ghl_emit(lf, hsz, area2, orient, hasI, hasC, n2, ok); sys_close(lf) }
87
88 if ok == 1 { return 0 }
89 return 1
90}