+ Box operations

This commit is contained in:
Vovanium 2023-08-03 00:04:58 +03:00
parent 276807acdf
commit 72a3438910
1 changed files with 27 additions and 3 deletions

View File

@ -57,15 +57,24 @@ package Video.Integer_Geometry with Pure is
function Empty_Interval return Interval is (Coordinate'Last, Coordinate'First);
function Full_Interval return Interval is (Coordinate'First, Coordinate'Last);
-- Note that transformations can cause overflow when applied to
-- Empty and Full intervals. User code should detect that corner cases.
function Is_Empty (A : Interval) return Boolean is (A.First >= A.Last);
function Length (A : Interval) return Distance is
(if Is_Empty (A) then 0 else A.Last - A.First);
function Contains (A : Interval; X : Coordinate) return Boolean is (X in A.First .. A.Last);
function Contains (A : Interval; X : Coordinate) return Boolean is
(X in A.First .. A.Last);
function Center (A : Interval) return Coordinate
is (A.First / 2 + A.Last / 2 + (A.First rem 2 + A.Last rem 2) / 2);
function Contains (Outer, Inner : Interval) return Boolean is
(Is_Empty (Inner) or else (Outer.First <= Inner.First and Inner.Last <= Outer.Last));
function Intersects (A, B : Interval) return Boolean is
((not Is_Empty (A)) and then (not Is_Empty (B)) and then (A.First <= B.Last and B.First <= A.Last));
function "+" (A : Interval; B : Coordinate) return Interval is
(A.First + B, A.Last + B);
@ -81,14 +90,29 @@ package Video.Integer_Geometry with Pure is
else (Coordinate'Min (A.First, B.First), Coordinate'Max (A.Last, B.Last)));
-- Minimal interval enclosing both arguments
function Center (A : Interval) return Coordinate
is (A.First / 2 + A.Last / 2 + (A.First rem 2 + A.Last rem 2) / 2);
type Box is record
X, Y : Interval;
end record;
function Empty_Box return Box is (Empty_Interval, Empty_Interval);
function Full_Box return Box is (Full_Interval, Full_Interval);
-- A "no clip" box
function Is_Empty (B : Box) return Boolean is (Is_Empty (B.X) or else Is_Empty (B.Y));
function Contains (B : Box; Q : Point) return Boolean is
(Contains (B.X, Q.X) and then Contains (B.Y, Q.Y));
function Contains (Outer, Inner : Box) return Boolean is
(Contains (Outer.X, Inner.X) and then Contains (Outer.Y, Inner.Y));
function Intersects (A, B : Box) return Boolean is
(Intersects (A.X, B.X) and then Intersects (A.Y, B.Y));
function "+" (P : Box; Q : Point) return Box is
(P.X + Q.X, P.Y + Q.Y);
-- Translate box