Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Length of path in coordinate system !
06-21-2009, 07:13 PM (This post was last modified: 12-21-2009 02:54 AM by drdebcol.)
Post: #1
Length of path in coordinate system !
Here is one mathematical problem ! It was on some competitions in programming !
You need to find path between dots in coordinate system !
If you have n dots and every dot has it's coordinates x and y !
First you need to have formula for finding length of path between two dots !
It is Euclid formula or Euclidean distance, look at the picture :
[Image: formula_coordinate.jpg]
And i wrote that in Pascal as function :
Code:
function length_between_two_dots(x1,y1,x2,y2:integer):real;
begin
  length_between_two_dots:=sqrt(sqr(x2-x1)+sqr(y2-y1));
end;
where sqrt represents sqare root and sqr represents square (for example sqr(5)=25 and sqrt(25)=5).
And you just need to find sum of those paths !
Number of paths is number_of_dots-1 ! or n-1 !
Because of that i made this loop :
Code:
for i:=1 to n-1 do
So here is the code in Turbo Pascal for Windows :
Code:
program length_in_system;
uses wincrt;
type
  tcoordinates=record
  x,y:integer;
  end;
var
  i,j,n:integer;
  total_length,k:real;
  a:array[1..100] of tcoordinates;
  s:string;
function length_between_two_dots(x1,y1,x2,y2:integer):real;
begin
  length_between_two_dots:=sqrt(sqr(x2-x1)+sqr(y2-y1));
end;
begin
writeln('************************************');
writeln('*Program for finding length of path*');
writeln('* on every segment and total path  *');
writeln('*           by Dr DEBCOL           *');
writeln('************************************');
writeln('Enter number of coordinates !');
readln(n);
writeln('Enter x and y coordinats in lines !');
for i:=1 to n do
readln(a[i].x,a[i].y);
    total_length:=0;
for i:=1 to n-1 do
  begin
    if i=n-1 then
      s:=''
    else
      s:='+';
    k:=length_between_two_dots(a[i].x,a[i].y,a[i+1].x,a[i+1].y);
    writeln(k:9:3,s);
    total_length:=total_length+k;
  end;
writeln(' = ',total_length:9:3);
end.

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: