Mega Code Archive

 
Categories / Delphi / Examples
 

Numberlinedist

function TMainForm.NumberLineDistReal(valOne, valTwo: Real): Real; //UTILITY FUNCTION //simple arithmetic operations of the kind that you do all the time //in graphics can produce erroneous outputs if one is not extremely //careful about 'abs-ing' values where appropriate: this function //bundles up the algorithm to find the 'absolute distance along the //number line' between two Real values so that we can just call it //up as appropriate... begin Result := InitialValue; if ((valOne >= 0) and (valTwo >= 0)) then begin if (valOne >= valTwo) then Result := (valOne - valTwo) else Result := (valTwo - valOne); end; if ((valOne < 0) and (valTwo >= 0)) then begin if (valOne >= valTwo) then Result := (abs(valOne) + valTwo) else Result := (abs(valOne) + valTwo); end; if ((valOne >= 0) and (valTwo < 0)) then begin if (valOne >= valTwo) then Result := (valOne + abs(valTwo)) else Result := (abs(valTwo) + valOne); end; if ((valOne < 0) and (valTwo < 0)) then begin if (valOne >= valTwo) then Result := (abs(valTwo) - abs(valOne)) else Result := (abs(valOne) - abs(valTwo)); end; end; ////////////////////////////////////////////////////////////////////////// function TMainForm.NumberLineDistInteger(valOne, valTwo: Integer): Integer; //UTILITY FUNCTION //see notes in NumberLineDistReal() above... begin Result := InitialValue; if ((valOne >= 0) and (valTwo >= 0)) then begin if (valOne >= valTwo) then Result := (valOne - valTwo) else Result := (valTwo - valOne); end; if ((valOne < 0) and (valTwo >= 0)) then begin if (valOne >= valTwo) then Result := (abs(valOne) + valTwo) else Result := (abs(valOne) + valTwo); end; if ((valOne >= 0) and (valTwo < 0)) then begin if (valOne >= valTwo) then Result := (valOne + abs(valTwo)) else Result := (abs(valTwo) + valOne); end; if ((valOne < 0) and (valTwo < 0)) then begin if (valOne >= valTwo) then Result := (abs(valTwo) - abs(valOne)) else Result := (abs(valOne) - abs(valTwo)); end; end;