Re: compute trigonometric in Delphi

Posted by webmaster Guido on March 27, 2005

In Reply to: Re: Have 5 Parameters Select any two to compute results posted by Lionel on March 25, 2005

A User will be given two values that were measured in the field and the rest have to be computed.

Most of the time the Angle is measured and the Tangent distance is measured but sometimes someone will bring in a measured Chord and Tangent or a Radius and Angle and the rest of the parameters have to be computed but the user never knows which two values he will receive of the possible 5.

Let's declare some unit-wide variables for the parameters:

{$R *.DFM}
  An, Ra, Tg, Ar, Ch: real;
  // Which parameters are given?
  AnGiven, RaGiven, TgGiven, ArGiven, ChGiven: Boolean;
  // How many parameters are given and valid?
  ParamsGiven: integer;

Some example Delphi code for the calculate-button:

procedure TForm1.btnCalcClick(Sender: TObject);
  AnalyzeData; // count valid numbers and save in variables
  if ValidEntries  2 then
    ShowMessage('Too many data. Give me only 2 valid numbers.')
  else begin

Convert the text of the edit-boxes to numbers, remember which parameters are given and valid, and count the given parameters:

procedure TForm1.AnalyzeData;
  ErrorCode: integer;
  ParamsGiven := 0;
  Val(Edit1.Text, An, ErrorCode); AnGiven := ErrorCode = 0;
  Val(Edit2.Text, Ra, ErrorCode); RaGiven := ErrorCode = 0;
  Val(Edit3.Text, Tg, ErrorCode); TgGiven := ErrorCode = 0;
  Val(Edit4.Text, Ar, ErrorCode); ArGiven := ErrorCode = 0;
  Val(Edit5.Text, Ch, ErrorCode); ChGiven := ErrorCode = 0;
  if AnGiven then inc(ParamsGiven);
  if RaGiven then inc(ParamsGiven);
  if TgGiven then inc(ParamsGiven);
  if ArGiven then inc(ParamsGiven);
  if ChGiven then inc(ParamsGiven);

Calculate the 3 missing parameters from the 2 given entries.
I searched only for a couple of formulas. It's up to you to cook up the rest ;-)
Also add some error-checking, for example: no divide-by zero.

procedure TForm1.Calculate;  
  if AnGiven then      // if angle is given
     An := DegToRad(An)    // convert to radians
  else begin           // if angle is missing
    if RaGiven then begin
      if TgGiven then An := Ra + Tg       // fake
      else if ArGiven then An := Ar / Ra  
      else An := Ra + Ch;                 // fake
    else if TgGiven then begin
      if ArGiven then An := Tg + Ar  // fake
      else An := Tg + Ch;            // fake
      An := Ar + Ch;  // fake
  if not RaGiven then begin // if radius is missing
    if AnGiven then begin
      if TgGiven then Ra := An + Tg       // fake
      else if ArGiven then Ra := Ar / An  
      else Ra := An + Ch;                 // fake
    else if TgGiven then begin
      if ArGiven then Ra := Tg + Ar // fake
      else Ra := Tg + Ch;           // fake
      Ra := Ar + Ch;  // fake
  if not TgGiven then Tg := Tan(An) * Ra;
  if not ArGiven then Ar := An * Ra;
  if not ChGiven then Ch := Sqrt(2 * Ra - Ra * Ra * Cos(An));

If you only show the parameters that were calculated, and not redisplay the originally given values, it's less confusing for the user. Otherwise, the display could be changed by formatting the numbers.

procedure TForm1.ShowResults;
  if not AnGiven then begin
    An := RadToDeg(An);  // show angle in degrees
    Edit1.Text := FloatToStr(An);
  if not RaGiven then Edit2.Text := FloatToStr(Ra);
  if not TgGiven then Edit3.Text := FloatToStr(Tg);
  if not ArGiven then Edit4.Text := FloatToStr(Ar);
  if not ChGiven then Edit5.Text := FloatToStr(Ch);

Good luck!

Related Articles and Replies