How to resolve the algorithm Find the intersection of two lines step by step in the Ada programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Find the intersection of two lines step by step in the Ada programming language

Table of Contents

Problem Statement

Find the point of intersection of two lines in 2D.

The 1st line passes though   (4,0)   and   (6,10) . The 2nd line passes though   (0,3)   and   (10,7) .

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Find the intersection of two lines step by step in the Ada programming language

Source code in the ada programming language

with Ada.Text_IO;

procedure Intersection_Of_Two_Lines
is
   Do_Not_Intersect : exception;

   type Line is record
      a : Float;
      b : Float;
   end record;

   type Point is record
      x : Float;
      y : Float;
   end record;

   function To_Line(p1, p2 : in Point) return Line
   is
      a : constant Float := (p1.y - p2.y) / (p1.x - p2.x);
      b : constant Float := p1.y - (a * p1.x);
   begin
      return (a,b);
   end To_Line;

   function Intersection(Left, Right : in Line) return Point is
   begin
      if Left.a = Right.a then
         raise Do_Not_Intersect with "The two lines do not intersect.";
      end if;

      declare
         b : constant Float := (Right.b - Left.b) / (Left.a - Right.a);
      begin
         return (b, Left.a * b + Left.b);
      end;
   end Intersection;

   A1 : constant Line := To_Line((4.0, 0.0), (6.0, 10.0));
   A2 : constant Line := To_Line((0.0, 3.0), (10.0, 7.0));
   p : constant Point := Intersection(A1, A2);
begin
   Ada.Text_IO.Put(p.x'Img);
   Ada.Text_IO.Put_Line(p.y'Img);
end Intersection_Of_Two_Lines;


  

You may also check:How to resolve the algorithm Random Latin squares step by step in the Perl programming language
You may also check:How to resolve the algorithm Hailstone sequence step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Real constants and functions step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Loops/N plus one half step by step in the Salmon programming language
You may also check:How to resolve the algorithm Palindrome detection step by step in the Simula programming language