unit nkpoint;
{
   Class for on XY-plane point

   Author:  Vesa Lappalainen
   Date:    15.2.1998
   Changes: 

}

interface
uses Classes;

type
  TDPoint = class;
  TInformDPoint = procedure (const p:TDPoint) of object;

  TDPoint = class(TPersistent)
  private
    FX : double;
    FY : double;
    FInform : TInformDPoint;
    FDoInform : boolean;
  protected

  public
    constructor Create; virtual;
    constructor Create2(ix,iy:double); virtual;
    constructor Create3(ix,iy:double;Iinform:TInformDPoint); virtual;
    destructor Destroy; override;
    procedure SetX(value:double); virtual;
    procedure SetY(value:double); virtual;
    function SetXY(ix,iy:double):TDPoint; virtual;
    function SetXYnu(ix,iy:double):TDPoint; virtual;
    function Mul(d:double):TDPoint; virtual;
    function MulV(const dp:TDPoint):TDPoint; virtual;
    function AddV(const dp:TDPoint):TDPoint; virtual;
    function SubV(const dp:TDPoint):TDPoint; virtual;
    function AddXY(dx,dy:double):TDPoint; virtual;
    function Neg : TDPoint; virtual;
    property Inform : TInformDPoint read FInform write FInform;
    property DoInform : boolean read FDoInform write FDoInform;
    procedure Assign(Source: TPersistent); override;
  published
    property x : double read FX write SetX;
    property y : double read FY write SetY;
  end;

implementation


//-----------------------------------------------------------------------------
constructor TDPoint.Create; //  override;
begin
  Create2(0,0);
end;

//-----------------------------------------------------------------------------
constructor TDPoint.Create2(ix,iy:double); //  virtual;
begin
  Create3(ix,iy,nil);
end;

//-----------------------------------------------------------------------------
constructor TDPoint.Create3(ix,iy:double;Iinform:TInformDPoint); //  virtual;
begin
  inherited Create;
  x := ix; y := iy;
  FInform := Iinform;
  DoInform := true;
end;


//-----------------------------------------------------------------------------
destructor TDPoint.Destroy; //  override;
begin
  inherited;
end;


//-----------------------------------------------------------------------------
procedure TDPoint.SetX(value:double); //  virtual;
begin
  FX := value;
  if ( Assigned(FInform) and DoInform ) then FInform(self);
end;

//-----------------------------------------------------------------------------
procedure TDPoint.SetY(value:double); //  virtual;
begin
  FY := value;
  if ( Assigned(FInform) and DoInform ) then FInform(self);
end;

//-----------------------------------------------------------------------------
function TDPoint.SetXYnu(ix,iy:double):TDPoint; //  virtual;
begin
  Fx := ix;
  Fy := iy;
  Result := Self;
end;

//-----------------------------------------------------------------------------
function TDPoint.SetXY(ix,iy:double):TDPoint; //  virtual;
begin
  Result := SetXYnu(ix,iy);
  if ( Assigned(FInform) and DoInform ) then FInform(self);
end;

//-----------------------------------------------------------------------------
function TDPoint.Mul(d:double):TDPoint; //  virtual;
begin
  Result := SetXY(x*d,y*d);
end;

//-----------------------------------------------------------------------------
function TDPoint.MulV(const dp:TDPoint):TDPoint; //  virtual;
begin
  Result := SetXY(x*dp.x,y*dp.y);
end;

//-----------------------------------------------------------------------------
function TDPoint.AddV(const dp:TDPoint):TDPoint; //  virtual;
begin
  Result := SetXY(x+dp.x,y+dp.y);
end;

//-----------------------------------------------------------------------------
function TDPoint.SubV(const dp:TDPoint):TDPoint; //  virtual;
begin
  Result := SetXY(x-dp.x,y-dp.y);
end;

//-----------------------------------------------------------------------------
function TDPoint.AddXY(dx,dy:double):TDPoint; //  virtual;
begin
  Result := SetXY(x+dx,y+dy);
end;


//-----------------------------------------------------------------------------
function TDPoint.Neg; //  virtual;
begin
  Result := SetXY(-x,-y);
end;


//-----------------------------------------------------------------------------
procedure TDPoint.Assign(Source: TPersistent);
var p: TDPoint;
begin
  if ( Source is TDPoint ) then begin
    p := Source as TDPoint;
    SetXY(p.x,p.y);
  end;
end;



end.


