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

Source code in the fortran programming language

program intersect_two_lines
  implicit none
  
  type point
    real::x,y
  end type point
  
  integer, parameter :: n = 4
  type(point)        :: p(n)
  
  p(1)%x = 4; p(1)%y = 0; p(2)%x = 6;  p(2)%y = 10 ! fist line 
  p(3)%x = 0; p(3)%y = 3; p(4)%x = 10; p(4)%y = 7  ! second line
  
  call intersect(p, n)
  
  contains
  
  subroutine intersect(p,m)
  integer, intent(in)       :: m
  type(point), intent(in)   :: p(m)
  integer   :: i
  real      :: a(2), b(2) ! y = a*x + b, for each line
  real      :: x, y       ! intersect point
  real      :: dx,dy      ! working variables
  
  do i = 1, 2
    dx = p(2*i-1)%x - p(2*i)%x
    dy = p(2*i-1)%y - p(2*i)%y
    if( dx == 0.) then    ! in case this line is of the form y = b
        a(i) = 0.
        b(i) = p(2*i-1)%y
    else
        a(i)= dy / dx
        b(i) = p(2*i-1)%y - a(i)*p(2*i-1)%x
    endif
  enddo
  
  if( a(1) - a(2) == 0. ) then
    write(*,*)"lines are not intersecting"
    return
  endif
  
  x = ( b(2) - b(1) ) / ( a(1) - a(2) )
  y = a(1) * x + b(1)
  write(*,*)x,y
  end subroutine intersect
end program intersect_two_lines


  

You may also check:How to resolve the algorithm Longest string challenge step by step in the Pascal programming language
You may also check:How to resolve the algorithm Introspection step by step in the Logo programming language
You may also check:How to resolve the algorithm Sorting algorithms/Shell sort step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Call a function step by step in the True BASIC programming language
You may also check:How to resolve the algorithm Caesar cipher step by step in the Crystal programming language