// Geometry: Point and Rect records with computed properties. Demonstrates // small value-records, deriving fields from nested records, and float // tolerance in assertions. cell Geometry { face { signal dist(a: Map, b: Map) -> Float signal area(r: Map) -> Int signal center(r: Map) -> Map signal contains_pt(r: Map, p: Map) -> Bool signal unit_square() -> Map } on dist(a: Map, b: Map) { let dx = a.x - b.x let dy = a.y - b.y return sqrt(dx * dx + dy * dy) } on area(r: Map) { return (r.hi.x - r.lo.x) * (r.hi.y - r.lo.y) } on center(r: Map) { return Point { x: (r.lo.x + r.hi.x) / 2, y: (r.lo.y + r.hi.y) / 2 } } on contains_pt(r: Map, p: Map) { return p.x >= r.lo.x && p.x <= r.hi.x && p.y >= r.lo.y && p.y <= r.hi.y } on unit_square() { return Rect { lo: Point { x: 0, y: 0 }, hi: Point { x: 1, y: 1 } } } } cell test GeometryTest { rules { assert abs(dist(Point { x: 0, y: 0 }, Point { x: 3, y: 4 }) - 5.0) < 0.0001 assert area(Rect { lo: Point { x: 0, y: 0 }, hi: Point { x: 4, y: 3 } }) == 12 assert center(unit_square()).x == 0.5 assert contains_pt(unit_square(), Point { x: 0.5, y: 0.5 }) == true assert contains_pt(unit_square(), Point { x: 2, y: 0 }) == false } }