Trigonometry Formulas for Delphi / Pascal


type
tdPoint= record x,y,z:double; end;
tdLine= record Point1,Point2:tdPoint; end;
tdArc= record Center:tdPoint; r,sa,ea:double; end;
const
RADIANS=57.29577951;

Find Distance Between Two Points:

function distance2d(x1,y1,x2,y2:double):double;
var a,b,c:double;
begin
 a:=abs(x1-x2);
 b:=abs(y1-y2);
 c:=sqr(a)+sqr(b);
 if c>0 then result:=sqrt(c) else result:=0; 
end;
function distance3d(x1,y1,z1,x2,y2,z2:double):double;
var c:double;
begin
 c:=sqr(abs(x1-x2))+sqr(abs(y1-y2))+sqr(abs(z1-z2));
 if c>0 then result:=sqrt(c) else result:=0;
end;
   

Find Midpoint Between Two Points

function midpoint(x1,y1,z1,x2,y2,z2:double):tdpoint;
var point1:tdpoint;
begin
 point1.x:=((x2-x1)/2)+x1;
 point1.y:=((y2-y1)/2)+y1;
 point1.z:=((z2-z1)/2)+z1;
 result:=point1;
end;

Get Angle in Degrees

// angle going from x1,y1 to x2,y2
function get_angle_degrees(x1,y1,x2,y2:double):double; 
var
 part1, part2:double;
 angle:double;
begin
 if (x1=x2) and (y1=y2) then
  begin
   result:=0.0;
   exit;
  end;
 part1:=abs(y2-y1);
 if (part1=0) then begin part1:=0.0000001; y1:=y1+0.0000001; end;
 part2:=abs(x2-x1);
 if (part2=0) then begin part2:=0.0000001; x1:=x1+0.0000001; end;
 angle:=arctan(part1/part2)*RADIANS;
 if ((x1>x2) and (y1<y2)) then angle:=180-angle;
 if ((x1>x2) and (y1>y2)) then angle:=angle +180;
 if ((x1<x2) and (y1>y2)) then angle:=360-angle;
 angle:=fix_angle(angle);
 result:=angle;
end;

Fix Angle

// if angle is outside of 0-360 then put it within 0-360
function fix_angle(angle:double):double;
begin
 while angle>= 360.0 do
  angle := angle - 360.0;
 while angle < 0 do
  angle := angle + 360.0;
 result:=angle;
end;

Point On an Arc at an Angle

function point_arc_angle(circle1:TDArc;angle:double):TDPoint;
var a9:double;
  point1:tdPoint;
begin
 if circle1.r=0 then
  begin
   result:=circle1.center;
   exit;
  end;
 a9:=angle/RADIANS;
 point1.x:=circle1.center.x+circle1.r*cos(a9);
 point1.y:=circle1.center.y+circle1.r*sin(a9);
 point1.z:=circle1.center.z;
 result:=point1;
end;

Get Closest Point to a Line

function get_closest_point_line(line1:TDLine;pick_point:TDPoint):TDPoint;
var angle:double;
 arc1:TDArc;
 temp_point:TDPoint;
 TDLine2:TDLine;
 return_point:tdPoint;
begin
 angle:=get_angle_degrees(line1.point1.x,line1.point1.y,line1.point2.x,line1.point2.y);
 arc1.center.x:=pick_point.x;
 arc1.center.y:=pick_point.y;
 arc1.center.z:=pick_point.z;
 arc1.r:=1;
 temp_point:=point_arc_angle(arc1,angle+90);
 line_2_points(pick_point,temp_point,TDLine2);
 point_inter_2_lines(line1,TDLine2,return_point);
end;

Offset a Line

function offset_line(orig_line:TDLine;dist:double;pick_point:TDPoint):TDLine);
var angle1:double;
 temp_point,new_end_point1,new_end_point2:TDPoint;
 temp_line:T2DLine;
 temp_circle:TDArc;
begin
 get_closest_point_line(orig_line,pick_point,temp_point);
 temp_line.point1:=temp_point;
 temp_line.point2:=pick_point;
 angle1:=get_angle_line(temp_line);
 temp_circle.center:=orig_line.point1;
temp_circle.r:=dist;
point_arc_angle(temp_circle,angle1,new_end_point1);
temp_circle.center:=orig_line.point2;
point_arc_angle(temp_circle,angle1,new_end_point2);
new_line.point1:=new_end_point1;
new_line.point2:=new_end_point2;
end;

Find Point at the Intersection of Two Lines

function point_inter_2_lines(line1,line2:TDLine;var point1:TDPoint):integer;
var x1_start,y1_start,x1_end,y1_end,
 x2_start,y2_start,x2_end,y2_end,
 k1,b1,k2,b2:double;
 ka,xa,xb:double;
begin
 point_inter_2_lines:=0;

 x1_start:=line1.point1.x;
 y1_start:=line1.point1.y;
 x1_end:=line1.point2.x;
 y1_end:=line1.point2.y;

 x2_start:=line2.point1.x;
 y2_start:=line2.point1.y;
 x2_end:=line2.point2.x;
 y2_end:=line2.point2.y;

 xa:=(x1_start-x1_end);
 if abs(xa)<0.000001 then
  begin
   xa:=0.000001;
  end;
 k1:=(y1_start-y1_end)/xa;
 b1:=(y1_start-(k1*x1_start));

 xb:=(x2_start-x2_end);
 if abs(xb)<0.000001 then
  begin
   xb:=0.000001;
  end;

 k2:=(y2_start-y2_end)/xb;
 b2:=(y2_start-(k2*x2_start));

 ka:=(k1-k2);
 if ka=0.0 then begin point_inter_2_lines:=902;{Parallel Lines} exit; end;
 if abs(ka)<0.000001 then
  begin
   ka:=0.000001;
  end;

 point1.x :=(b2-b1)/ka;
 point1.y:=(k1*point1.x)+b1;
end;

function line_2_points(point1,point2:TDPoint;var line1:TDLine):integer;
begin
 if (point1.x=point2.x)and(point1.y=point2.y)then line_2_points:=903{undefined line} else line_2_points:=0;
 line1.point1.x:=point1.x;
 line1.point1.y:=point1.y;
 line1.point1.z:=point1.z;
 line1.point2.x:=point2.x;
 line1.point2.y:=point2.y;
 line1.point2.z:=point2.z;
end;

Find Sides and Angles of Right Triangle

Right Triangle

Some require system.math unit

To find side a:

a:=c * sin(A);
a:=b * tan(A);
a:=c * cos(B);
a:=b * cotan(B);

To find side b:

b:=c * cos(A);
b:=a * cotan(A);
b:=c * sin(B);
b:=a * tan(B);

To find side c:

c:=b * sec((A); 
c:=a * csc(A); 
c:=a * sec(B); 
c:=b * csc(B);

c:= sqrt(sqr(a) + sqr(b));

To find angle A:

A:=arctan(a/b) * RADIANS;
A:=arcsin(a/c) * RADIANS;

A:=arccos(b/c) * RADIANS;

To find angle B:

B:=arctan(b/a) * RADIANS;
B:=arcsin(b/c) * RADIANS;

B:=arccos(a/c) * RADIANS;