% File "loc.tu". % % Written by Paul Gries, Fall 96. % % This class defines a location on the screen, and provides % methods for working with locations. For example, LocDiff tells % the difference between the present location and another one. unit class Location export SetX, SetY, GetX, GetY, Copy, LocDiff, XDiff, YDiff % Instance variables %------------------- var x : real var y : real % SetX %---------------------------------------------------------------- % Set my distance from the left side of the screen to xloc. proc SetX (xloc : real) x := xloc end SetX % SetY %---------------------------------------------------------------- % Set my distance from the bottom of the screen to yloc. proc SetY (yloc : real) y := yloc end SetY % GetX %---------------------------------------------------------------- % Get my distance from the left side of the screen. function GetX : real result x end GetX % GetY %---------------------------------------------------------------- % Get my distance from the bottom of the screen. function GetY : real result y end GetY % LocDiff %---------------------------------------------------------------- % Return the distance between me and loc. function LocDiff(loc : ^Location) : real % Find the appropriate unit vector. var xDelta := GetX - loc->GetX var yDelta := GetY - loc->GetY var vectorLen := sqrt(xDelta*xDelta + yDelta * yDelta) result vectorLen end LocDiff % XDiff %---------------------------------------------------------------- % Return the distance between me and loc horizontally. function XDiff(loc : ^Location) : real result GetX - loc->GetX end XDiff % YDiff %---------------------------------------------------------------- % Return the distance between me and loc vertically. function YDiff(loc : ^Location) : real result GetY - loc->GetY end YDiff % Copy %---------------------------------------------------------------- % Copy the contents of loc into me. proc Copy (loc : ^Location) SetX(loc->GetX) SetY(loc->GetY) end Copy end Location