code wiki / _hdl_build / nx_geo_area_gate.nx
nx_geo_area_gate.nx source
↩ module page · 92 lines · 4669 B
1// nx_geo_area_gate.nx -- GATE for GEO-010 polygon area + orientation (integer-exact shoelace). Proves:
2// square CCW : 1deg x 1deg square (side 1e6) -> signed doubled area = +2e12, orientation = +1
3// square CW : same square reversed -> signed doubled area = -2e12, orientation = -1
4// triangle : legs 2e6, area 2e12 -> signed doubled area = +4e12
5// degenerate : 3 collinear points -> doubled area EXACTLY 0, orientation = 0 (the exceed)
6// micro-apex : base 2e6, apex 1 microdeg up -> doubled area EXACTLY 2e6 (1-microdeg resolution)
7// All comparisons are EXACT equality (no tolerance): the shoelace is pure-integer, so the answers are
8// reproducible to the bit -- the property float libs cannot promise on these edge cases.
9//
10// Evidence -> knowledge/status/geo_area.log (GEOAREAGATE authored=organ ... verdict=GREEN).
11// license_tier: ORIGINAL
12import "nx_geo_area.nx"
13import "nx_syscalls.nx"
14
15const GA_LOG: *u8 = "knowledge/status/geo_area.log"
16
17func ga_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 }
18func ga_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 }
19
20func ga_set(p: *i64, i: i64, lat: i64, lon: i64) -> i64 { p[i * 2] = lat; p[i * 2 + 1] = lon; return 0 }
21
22func ga_emit(fd: i64, sq: i64, sqo: i64, sqcw: i64, sqcwo: i64, tri: i64, deg: i64, dego: i64, mic: i64, ok: i64) -> i64 {
23 ga_w(fd, "GEOAREAGATE authored=organ exact=integer-shoelace sq_ccw_area2=" as *u8); ga_wn(fd, sq)
24 ga_w(fd, " sq_ccw_orient=" as *u8); ga_wn(fd, sqo)
25 ga_w(fd, " sq_cw_area2=" as *u8); ga_wn(fd, sqcw)
26 ga_w(fd, " sq_cw_orient=" as *u8); ga_wn(fd, sqcwo)
27 ga_w(fd, " tri_area2=" as *u8); ga_wn(fd, tri)
28 ga_w(fd, " degenerate_area2=" as *u8); ga_wn(fd, deg)
29 ga_w(fd, " degenerate_orient=" as *u8); ga_wn(fd, dego)
30 ga_w(fd, " microapex_area2=" as *u8); ga_wn(fd, mic)
31 if ok == 1 { ga_w(fd, " verdict=GREEN\n" as *u8) } else { ga_w(fd, " verdict=RED\n" as *u8) }
32 return 0
33}
34
35func main() -> i64 {
36 // CCW square: (0,0)->(0,1e6)->(1e6,1e6)->(1e6,0) [lat,lon]; in (x=lon,y=lat) = right,up,left,down = CCW.
37 let sq: *i64 = sys_mmap(8 * 8) as *i64
38 ga_set(sq, 0, 0, 0)
39 ga_set(sq, 1, 0, 1000000)
40 ga_set(sq, 2, 1000000, 1000000)
41 ga_set(sq, 3, 1000000, 0)
42 let sq_area: i64 = geo_signed_area2(sq, 4) // expect +2e12
43 let sq_orient: i64 = geo_orientation(sq, 4) // expect +1
44
45 // same square, vertex order reversed -> clockwise.
46 let cw: *i64 = sys_mmap(8 * 8) as *i64
47 ga_set(cw, 0, 1000000, 0)
48 ga_set(cw, 1, 1000000, 1000000)
49 ga_set(cw, 2, 0, 1000000)
50 ga_set(cw, 3, 0, 0)
51 let cw_area: i64 = geo_signed_area2(cw, 4) // expect -2e12
52 let cw_orient: i64 = geo_orientation(cw, 4) // expect -1
53
54 // right triangle legs 2e6 -> area 2e12, doubled 4e12. CCW.
55 let tri: *i64 = sys_mmap(8 * 6) as *i64
56 ga_set(tri, 0, 0, 0)
57 ga_set(tri, 1, 0, 2000000)
58 ga_set(tri, 2, 2000000, 0)
59 let tri_area: i64 = geo_signed_area2(tri, 3) // expect +4e12
60
61 // degenerate: 3 collinear points (same lat). Float libs can emit tiny noise; we are EXACTLY 0.
62 let deg: *i64 = sys_mmap(8 * 6) as *i64
63 ga_set(deg, 0, 0, 0)
64 ga_set(deg, 1, 0, 1000000)
65 ga_set(deg, 2, 0, 2000000)
66 let deg_area: i64 = geo_signed_area2(deg, 3) // expect 0
67 let deg_orient: i64 = geo_orientation(deg, 3) // expect 0
68
69 // micro-apex: base lon 0..2e6 at lat 0, apex 1 microdeg up -> exact doubled area 2e6.
70 let mic: *i64 = sys_mmap(8 * 6) as *i64
71 ga_set(mic, 0, 0, 0)
72 ga_set(mic, 1, 0, 2000000)
73 ga_set(mic, 2, 1, 1000000)
74 let mic_area: i64 = geo_signed_area2(mic, 3) // expect +2e6
75
76 var ok: i64 = 1
77 if sq_area != 2000000000000 { ok = 0 }
78 if sq_orient != 1 { ok = 0 }
79 if cw_area != (0 - 2000000000000) { ok = 0 }
80 if cw_orient != (0 - 1) { ok = 0 }
81 if tri_area != 4000000000000 { ok = 0 }
82 if deg_area != 0 { ok = 0 }
83 if deg_orient != 0 { ok = 0 }
84 if mic_area != 2000000 { ok = 0 }
85
86 ga_emit(1, sq_area, sq_orient, cw_area, cw_orient, tri_area, deg_area, deg_orient, mic_area, ok)
87 let lf: i64 = sys_openat_append(GA_LOG, 420)
88 if lf >= 0 { ga_emit(lf, sq_area, sq_orient, cw_area, cw_orient, tri_area, deg_area, deg_orient, mic_area, ok); sys_close(lf) }
89
90 if ok == 1 { return 0 }
91 return 1
92}