How to resolve the algorithm Find the intersection of two lines step by step in the ALGOL 68 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 ALGOL 68 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 ALGOL 68 programming language

Source code in the algol programming language

BEGIN
    # mode to hold a point #
    MODE POINT = STRUCT( REAL x, y );
    # mode to hold a line expressed as y = mx + c #
    MODE LINE  = STRUCT( REAL m, c );
    # returns the line that passes through p1 and p2 #
    PROC find line = ( POINT p1, p2 )LINE:
         IF x OF p1 = x OF p2 THEN
             # the line is vertical                 #
             LINE( 0, x OF p1 )
         ELSE
             # the line is not vertical             #
             REAL m = ( y OF p1 - y OF p2 ) / ( x OF p1 - x OF p2 );
             LINE( m, y OF p1 - ( m * x OF p1 ) )
         FI # find line # ;

    # returns the intersection of two lines - the lines must be distinct and not parallel #
    PRIO INTERSECTION = 5;
    OP   INTERSECTION = ( LINE l1, l2 )POINT:
         BEGIN
             REAL x = ( c OF l2 - c OF l1 ) / ( m OF l1 - m OF l2 );
             POINT( x, ( m OF l1 * x ) + c OF l1 )
         END # INTERSECTION # ;

    # find the intersection of the lines as per the task #
    POINT i = find line( POINT( 4.0, 0.0 ), POINT( 6.0, 10.0 ) )
              INTERSECTION find line( ( 0.0, 3.0 ), ( 10.0, 7.0 ) );
    print( ( fixed( x OF i, -8, 4 ), fixed( y OF i, -8, 4 ), newline ) )

END

  

You may also check:How to resolve the algorithm Minimum positive multiple in base 10 using only 0 and 1 step by step in the Haskell programming language
You may also check:How to resolve the algorithm Integer comparison step by step in the ALGOL W programming language
You may also check:How to resolve the algorithm Vector products step by step in the uBasic/4tH programming language
You may also check:How to resolve the algorithm Inheritance/Multiple step by step in the Java programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the Raku programming language