Check if two given Circles are Orthogonal or not - GeeksforGeeks

19 Mar, 2022
Given are two circles with their centres C1(x1, y1) and C2(x2, y2) and radius r1 and r2, the task is to check if both the circles are orthogonal or not. Two curves are said to be orthogonal if their angle of intersection is a right angle i.e the tangents at their point of intersection are perpendicular. 
The above two circles are orthogonal
Examples:  
Input: C1(4, 3), C2(0, 1), r1 = 2, r2 = 4 
Output: Yes

Input: C1(4, 3), C2(1, 2), r1 = 2, r2 = 2 
Output: No
 
Approach:  
Find the distance between the centres of two circles ‘d’ with distance formula.
For the circles to be orthogonal we need to check if
r1 * r1 + r2 * r2 = d * d
If it is true, then both the circles are orthagonal. Else not.
Below is the implementation of the above approach: 
C++
Java
Python3
C#
Javascript
// C++ program to check if two// circles are orthogonal or not #include <bits/stdc++.h>using namespace std; // Function to Check if the given// circles are orthogonalbool orthogonality(int x1, int y1, int x2,                   int y2, int r1, int r2){     // calculating the square    // of the distance between C1 and C2    int dsquare = (x1 - x2) * (x1 - x2)                  + (y1 - y2) * (y1 - y2);     // Check if the given    // circles are orthogonal    if (dsquare == r1 * r1 + r2 * r2)        return true;    else        return false;} // Driver codeint main(){    int x1 = 4, y1 = 3;    int x2 = 0, y2 = 1;    int r1 = 2, r2 = 4;     bool f = orthogonality(x1, y1, x2,                           y2, r1, r2);     if (f)        cout << "Given circles are"             << " orthogonal.";    else        cout << "Given circles are"             << " not orthogonal.";    return 0;}
// Java program to check if two// circimport java.util.*;import java.lang.*;import java.io.*; class GFG{    // Function to Check if the given    // circles are orthogonal    public static boolean orthogonality(int x1, int y1, int x2,                                        int y2, int r1, int r2)    {         // calculating the square        // of the distance between C1 and C2        int dsquare = (x1 - x2) * (x1 - x2) +                      (y1 - y2) * (y1 - y2);         // Check if the given        // circles are orthogonal        if (dsquare == r1 * r1 + r2 * r2)            return true;        else            return false;    }     // Driver Code    public static void main(String[] args) throws java.lang.Exception    {        int x1 = 4, y1 = 3;        int x2 = 0, y2 = 1;        int r1 = 2, r2 = 4;         boolean f = orthogonality(x1, y1, x2, y2, r1, r2);         if (f)            System.out.println("Given circles are orthogonal.");        else            System.out.println("Given circles are not orthogonal.");    }} // This code is contributed by ashutosh450
# Python3 program to check if two# circles are orthogonal or not # Function to Check if the given# circles are orthogonaldef orthogonality(x1, y1, x2, y2, r1, r2):     # calculating the square    # of the distance between C1 and C2    dsquare = (x1 - x2) * (x1 - x2) + \              (y1 - y2) * (y1 - y2);     # Check if the given    # circles are orthogonal    if (dsquare == r1 * r1 + r2 * r2):        return True    else:        return False # Driver codex1, y1 = 4, 3x2, y2 = 0, 1r1, r2 = 2, 4 f = orthogonality(x1, y1, x2, y2, r1, r2) if (f):    print("Given circles are orthogonal.")else:    print("Given circles are not orthogonal.") # This code is contributed by Mohit Kumar
// C# implementation for above programusing System; class GFG{    // Function to Check if the given    // circles are orthogonal    public static bool orthogonality(int x1, int y1, int x2,                                     int y2, int r1, int r2)    {         // calculating the square        // of the distance between C1 and C2        int dsquare = (x1 - x2) * (x1 - x2) +                      (y1 - y2) * (y1 - y2);         // Check if the given        // circles are orthogonal        if (dsquare == r1 * r1 + r2 * r2)            return true;        else            return false;    }     // Driver Code    public static void Main()    {        int x1 = 4, y1 = 3;        int x2 = 0, y2 = 1;        int r1 = 2, r2 = 4;         bool f = orthogonality(x1, y1, x2, y2, r1, r2);         if (f)            Console.WriteLine("Given circles are orthogonal.");        else            Console.WriteLine("Given circles are not orthogonal.");    }} // This code is contributed by AnkitRai01
<script>    // Javascript program to check if two    // circles are orthogonal or not          // Function to Check if the given    // circles are orthogonal    function orthogonality(x1, y1, x2, y2, r1, r2)    {         // calculating the square        // of the distance between C1 and C2        let dsquare = (x1 - x2) * (x1 - x2)                      + (y1 - y2) * (y1 - y2);         // Check if the given        // circles are orthogonal        if (dsquare == r1 * r1 + r2 * r2)            return true;        else            return false;    }           // Driver code    let x1 = 4, y1 = 3;    let x2 = 0, y2 = 1;    let r1 = 2, r2 = 4;       let f = orthogonality(x1, y1, x2,                           y2, r1, r2);       if (f)        document.write("Given circles are orthogonal.");    else        document.write("Given circles are not orthogonal."); // This code is contributed by divyesh072019.</script>
Given circles are orthogonal.
 
Time Complexity: O(1)
Auxiliary Space: O(1)
mohit kumar 29
ashutosh450
ankthon
divyesh072019
subham348
C++ Programs
Geometric
Mathematical
Mathematical
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Passing a function as a parameter in C++
Program to implement Singly Linked List in C++ using class
Const keyword in C++
cout in C++
Dynamic _Cast in C++
Closest Pair of Points using Divide and Conquer algorithm
How to check if two given line segments intersect?
Program for distance between two points on earth
How to check if a given point lies inside or outside a polygon?
Find if two rectangles overlap