Re: Delphi Math Unit: angles, sine, cosine, radians


[ Related Articles and Replies ] [ DelphiLand FAQ ] [ Delphi Tutorials ]

Posted by Lionel on December 16, 2002 at 04:07:49:

In Reply to: Re: Delphi Books & Math Unit posted by webmaster Guido on December 16, 2002 at 03:22:50:

Thanks,
I have used this format and it works fine but I need a better way to enter an angle with out having so many edit boxes. For example I would like to enter degrees,minutes,seconds in one edit box and use the following format:
Enter DD.MMSS in one edit box and then convert it to decimal degrees and then convert to radians. I saw this code someplace but can't find it now. As I remender, the code showed this done using the Trunc and Frac function but I have not been able to get my code to work. Any ideas?
X:=DMS;(Entered as DD.MMSS)
D:=Trunc(X);pull degrees out
M:=Frac(X)*100;remainder mm.ss
M:=Trunc(M);pull out minutes
S:=Frac(X)*100; mm.ss
S:=Frac(S)*100; pull out ss
//-------------------------------
FloatToStr(D) Display Degrees
FloatToStr(M) Display Minutes
FloatToStr(S) Display Seconds
//------------------------------
DecDeg:=D+(M/60)+(S/3600); Compute DecDeg
Radians:=DecDeg*pi/180; Compute Radians
************************************************
: : Anyone know of a book telling how to use the Delphi math unit's with examples. I work with Trig, Angles, Bearings, Sin, Arcsins, Tan, etc.. I have purchased several books on Delphi starting with Delphi 2, delphi 3, Delphi 5 and Delphi 6, now I see Delphi 7 books being advertised. I am tired of buying "How to books" only to find out that they avoid all mention of the Math Unit or Math applications. I am a past Turbo Pascal user and back in the old days(-; you could find many source codes and math examples. What happened?---If you know of a book source, I would appreciate hearing from you.
: ------------------------

: I don't recall any specific books, but there's a lot of info on the web, such as the very useful Math Pages of "efg's Computer Lab".

: Here's some basic info for those not familiar with Delphi's MATH unit:

: Starting with Delphi 4, every version has the Math unit (it was not included in the Standard edition of D2 nor D3, only the Professional and Client/Server versions had the Math unit).

: Some math functions like Sin() and Cos() are in the System unit, others like Tan() and the important DegToRad are in the Math-unit. DegToRad = "Degrees To Radians" is needed because the trigonometric functions require the angles expressed in radians; for a small example, see the end of this post.
:
: The Math-unit isn't included by default in the template of a new form, so you have to add it yourself to the "uses" directive on top of the unit. For example, if it says:

: uses
: Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

: then you have to change it to:

: uses
: Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, MATH;

: ----------------------------------------

:
: A simple example of code that needs the MATH unit:
: Enter degrees, minutes and seconds of an angle in 3 edit boxes, and display the sine and the cosine of the angle. Of course, in real life you have to do some error checking on what is entered into the TEdit boxes.

: procedure TForm1.btnCalculateClick(Sender: TObject);
: var
: Deg, Min, Sec: integer;
: D, R, S, C: real;
: begin
: Deg := StrToInt(editDeg.Text);
: Min := StrToInt(editMin.Text);
: Sec := StrToInt(editSec.Text);
: D := Deg + Min / 60 + Sec / 3600; // degrees plus decimal fraction
: R := DegToRad(D); // convert degrees to radians
: S := Sin(R);
: C := Cos(R);
: labelDegDec.Caption := FloatToStr(D);
: labelRad.Caption := FloatToStr(R);
: labelSin.Caption := FloatToStr(S);
: labelCos.Caption := FloatToStr(C);
: end;


Related Articles and Replies:


[ Related Articles and Replies ] [ DelphiLand FAQ ] [ Delphi Tutorials ]