Results 1 to 4 of 4

Math Help - Calculating Angle from Coordinates.

  1. #1
    Newbie
    Joined
    Feb 2007
    Posts
    2

    Calculating Angle from Coordinates.

    Edit: Hm. Possibly this would be better suited to the general forum area, but I missed that when scanning through the sections. Please could it be moved there, if deemed more suitable for that area? Sorry to be a bother!



    Hi all!

    I recently recieved an assignment from my college (I'm on a BTEC IT Course), to create a program in Delphi designed to aid high school students in learning angles and coordinates and the like.

    Now, the trouble with this is that I actually do not know the equations for the program I am supposed to be making, and so I came here for some help

    I created a quick example image in Photoshop, to help explain what I require:



    Assuming that the red dot is X, the blue dot is Y, and 0 degrees is straight up, I require some sort of equation that will calculate the angle in degrees to make point X face point Y. So in the example above, it would be 45 degrees.

    The program I have been told to make has to provide a simple graph, like the one above, and allow the user to plot two points, again like above. It would then calculate the angle and output it.

    I hope I have made myself clear enough, and I hope I chose the correct forum section... I couldn't find an angles/degrees specific forum, so Trigonometry seemed to be the next best thing.


    Thanks for reading, and thanks to any who help or attempt to
    Dave

    PS: I have absolutely no idea how easy/hard this is - I'm extremely bad at maths!
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Super Member
    earboth's Avatar
    Joined
    Jan 2006
    From
    Germany
    Posts
    5,829
    Thanks
    123
    Hello,

    the following Delphi(5!) program is only a skeleton of an useful program. I only want to show you how to calculate the angle:

    unit Unit1;
    interface
    uses
    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
    StdCtrls, ExtCtrls;

    type
    TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Label3: TLabel;
    Edit5: TEdit;
    Label4: TLabel;
    Button1: TButton;
    Button2: TButton;
    Image1: TImage;
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    private
    { Private-Deklarationen }
    public
    { Public-Deklarationen }
    end;

    var
    Form1: TForm1;

    implementation

    {$R *.DFM}

    procedure TForm1.FormCreate(Sender: TObject);
    begin
    form1.image1.canvas.fillrect(rect(0,0,320,320));
    form1.edit1.text := '';
    form1.edit2.text := '';
    form1.edit3.text := '';
    form1.edit4.text := '';
    form1.edit5.text := '';
    end;

    procedure TForm1.Button2Click(Sender: TObject);
    begin
    close;
    end;

    procedure TForm1.Button1Click(Sender: TObject);
    var p, q : tpoint;
    angle : extended;
    begin
    form1.edit1.setfocus;
    p.x := strtoint(form1.edit1.text);
    form1.edit2.setfocus;
    p.y := strtoint(form1.edit2.text);
    form1.edit3.setfocus;
    q.x := strtoint(form1.edit3.text);
    form1.edit4.setfocus;
    q.y := strtoint(form1.edit4.text);
    angle := (360/(2*pi))*arctan((q.y-p.y)/(q.x-p.x))+90;
    form1.Edit5.Text := floattostr(angle);
    form1.image1.canvas.moveto(p.x,0);
    form1.image1.canvas.lineto(p.x,320);
    form1.image1.canvas.moveto(p.x,p.y);
    form1.image1.canvas.lineto(q.x,q.y);

    end;

    end.


    Some additional remarks:
    A) in the procedure FormCreate I filled the background of the image with white colour and "deleted" the contents of the edit-windows (actually I overwrite the content by an empty string)
    B) The type tpoint is pre-defined in Delphi and is actually a record which contains the x- and y-coordinate of a point. In my example the Point P exist of p.x and p.y.
    C) The mathematic in this program is only one command
    angle := (360/(2*pi))*arctan((q.y-p.y)/(q.x-p.x));
    You calculate the differences between the y- and the x-coordinates. The quotient of these differences is the tangens of the angle you are looking for. The function arctan in Delphi gives the radient which has to be transformed into an angle. Therefore you have to use the constant factor (360/(2*pi)). you have to add 90 because one leg of the angle is pointing upward.

    I've added a screen-shot of an example P(60, 100) and Q(160, 200).

    EB
    Attached Thumbnails Attached Thumbnails Calculating Angle from Coordinates.-winkl_delphi.gif  
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Junior Member AlvinCY's Avatar
    Joined
    Feb 2007
    From
    I live in Sydney, Australia.
    Posts
    69
    Well... there is a mathematical explanation if you need one. I have no idea what the code as about, but that's cos I'm not a computer person.

    Say looking at your y and x, if you want to find the angle, call it \theta that the vertical axis makes with the line joining x,y... you know, using basic trigonometry that

    tan \theta = \frac{a}{b}

    Where a is the horizontal separation between the x-coordinate of x and the x-coordinate of y

    and b is the vertical separation between the y-coordinate of x and the y-coordinate of y.

    Does that help?
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Newbie
    Joined
    Feb 2007
    Posts
    2
    Thanks for the replies guys!

    I was only after the mathematical part of the problem, of course, but a delphi explaination is even better! Exactly what I needed.

    Thanks alot to both of you.
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Calculating an angle in 3D coordinates
    Posted in the Geometry Forum
    Replies: 8
    Last Post: September 6th 2011, 12:15 PM
  2. Calculating an angle of a none right angle triangle
    Posted in the Trigonometry Forum
    Replies: 14
    Last Post: August 19th 2011, 01:42 PM
  3. Calculating Coordinates
    Posted in the Differential Geometry Forum
    Replies: 1
    Last Post: March 14th 2010, 09:23 PM
  4. calculating coordinates for a pattern
    Posted in the Algebra Forum
    Replies: 2
    Last Post: March 1st 2010, 02:29 PM
  5. Help with calculating coordinates.
    Posted in the Algebra Forum
    Replies: 1
    Last Post: October 12th 2009, 05:16 PM

Search Tags


/mathhelpforum @mathhelpforum