Orthogonality of 2 lines

#include<bits/stdc++.h>
#define ll long long

// check whether 2 lines are orthogonal or not
// input: line 1 represented by 2 points (x1, y1) and (x2, y2)
// and line 2 represented by 2 points (x3, y3) and (x4, y4)
// output: true or false
template <typename T> bool checkOrtho(T x1, T y1, T x2, T y2, 
                                      T x3, T y3, T x4, T y4) { 
  
    T m1, m2; 
  
    // Both lines have infinite slope 
    if (x2 - x1 == 0 && x4 - x3 == 0) 
        return false; 
  
    // Only line 1 has infinite slope 
    else if (x2 - x1 == 0) { 
  
        m2 = (y4 - y3) / (x4 - x3); 
  
        if (m2 == 0) 
            return true; 
        else
            return false; 
    } 
  
    // Only line 2 has infinite slope 
    else if (x4 - x3 == 0) { 
  
        m1 = (y2 - y1) / (x2 - x1); 
  
        if (m1 == 0) 
            return true; 
        else
            return false; 
    } 
  
    else { 
        // Find slopes of the lines 
        m1 = (y2 - y1) / (x2 - x1); 
        m2 = (y4 - y3) / (x4 - x3); 
  
        // Check if their product is -1 
        if (m1 * m2 == -1) 
            return true; 
        else
            return false; 
    } 
} 

Leave a Reply