-- $Date: 2004/02/23 08:22:06 $
-- $Revision: 1.12 $
-- $Author: jcrocholl $

package body Straights is

   -- Create a straight line.
   function Create
     (To : in Vector)      -- End point of the line.
     return Line_Access is -- The newly created straight line.
   begin
      return new Straight'(To => To, Length => 0.0);
   end Create;

   -- Calculate the length of this line. Results are cached for
   -- reuse. So make sure the starting point always remain the same
   -- between calls.
   function Length
     (Start : in Vector;       -- Starting point of the line.
      This  : access Straight-- Get this line's length.
     return Real is            -- The distance from start to end point.
   begin
      if This.Length = 0.0 then
         This.Length := abs(This.To - Start);
      end if;
      return This.Length;
   end Length;

   -- Scale a line by a given factor.
   procedure Scale
     (This   : access Straight;
      Factor : in Real) is
   begin
      This.To := This.To * Factor;
      This.Length := This.Length * Factor;
   end Scale;

   -- Translate a line by a given offset.
   procedure Translate
     (This   : access Straight-- Translate this line.
      Offset : in Vector) is    -- Translating offset.
   begin
      This.To := This.To + Offset;
   end Translate;

   -- Get one point from a straight line.
   function Way_Point
     (Start : in Vector;       -- Starting point of the line.
      This  : access Straight-- Definition of the line.
      Part  : in Real)         -- Parametrization variable from 0 to 1.
     return Vector is          -- The point on the line.
   begin
      return Start + Part * (This.To - Start);
   end Way_Point;

   -- Format a straight line for Scalable Vector Graphics output.
   function To_SVG
     (This      : access Straight-- Format this line.
      Tolerance : in Real)         -- Tolerance level.
     return String is              -- The resulting SVG code.
   begin
      return "L " & To_String(This.To, Tolerance);
   end To_SVG;

   -- Format a straight line for postscript output.
   function To_Postscript
     (This      : access Straight-- Format this line.
      Tolerance : in Real)         -- Tolerance level.
     return String is              -- The resulting Postscript code.
   begin
      return To_String(Mirror_Y(This.To), Tolerance) & " lineto";
   end To_Postscript;

end Straights;