# Math Help - Calculating Angle from Coordinates.

1. ## 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!

2. 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.

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

3. 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?

4. 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.