!********o*********o*********o*********o*********o*********o*********o**
! This program, given three points in the X-Y plane, determines if they
! are collinear.
!
! Inputs
!   x1,y1:          one point
!   x2,y2:          another point
!   x3,y2:          yet another point
!
! Outputs
!   no specific variables;  output is determined by tests 
!
! Constants
!   tolerance:      small number that controls how much round off error
!                   we'll tolerate
!
! Other Variables
!   slope12:        the slope (deltaY/deltaX) of the segment between
!                   x1,y1 and x2,y2
!   slope23:        the slope (deltaY/deltaX) of the segment between
!                   x2,y2 and x3,y3
!
! Method Used
!   The slope between x1,y1 and x2,y2 is compared to the slope between
!   x2,y2 and x3,y3.  If the two line segments have the same slope, they
!   are collinear (because they have a point in common).  Because of
!   possible roundoff error, we cannot test for exact equality, and
!   instead test for equality within a specified tolerance.
!---------------------
! Author:   Bob Harris
! Class:    CMPSC 201F
! Section:  000
! Date Due: 1 Oct 2003
!********o*********o*********o*********o*********o*********o*********o**

PROGRAM Collinearifier

IMPLICIT NONE

REAL, PARAMETER :: tolerance = 0.000001
REAL :: x1,y1, x2,y2, x3,y3
REAL :: slope12, slope23

! fetch the three points from the user (we could do better on how we
! accept input, but for this example program that's not the focus)

PRINT *, "Please enter three points, as X Y X Y X Y"
READ  *, x1,y1, x2,y2, x3,y3

! handle special cases separately, since our slope calculation will
! 'blow up' if we divide by zero

if ((x1==x2) .and. (x2==x3)) then
    PRINT *, "These points are collinear"
else if ((x1==x2) .or. (x2==x3)) then
    PRINT *, "These points are not collinear"
else
    ! determine slopes of the segments

    slope12 = (y2-y1)/(x2-x1)
    slope23 = (y3-y2)/(x3-x2)

    ! compare slopes, allowing for some roundoff error

    if (abs(slope12-slope23) <= tolerance) then
        PRINT *, "These points are (probably) collinear"
    else
        PRINT *, "These points are not collinear"
    end if

end if

END PROGRAM Collinearifier
