classdef Interval < handle
% An Interval has a left endpoint and a right endpoint.

properties
left
right
end

methods
function Inter = Interval(lt, rt)
% Constructor: construct an Interval object
Inter.left= lt;
Inter.right= rt;
end

function scale(self, f)
% Scale self by a factor f
w= self.right - self.left;
self.right= self.left + w*f;
end

function shift(self, s)
% Shift self by s
self.left= self.left + s;
self.right= self.right + s;
end

function tf = isIn(self, other)
% tf is true (1) if self is in the other Interval
tf= self.left>=other.left && self.right<=other.right;
end

function Inter = overlap(self, other)
% If self and the other Interval overlap, then Inter is the
% overlapped Interval; otherwise Inter is empty.
Inter = Interval.empty();
Oleft = max(self.left, other.left);
Oright = min(self.right, other.right);
if Oleft <= Oright
Inter = Interval(Oleft, Oright);
end
end

function disp(self)
% Display self using interval notation
fprintf('(%f,%f)\n', self.left, self.right)
end

end %methods

end %classdef