Software & Finance





Visual C++ - Check whether a point is inside or outside the Circle





Here is the Visual C++ program to check whether a given point is inside or outside the Circle.

 

The distance between the center of the circle and the given point is calculated first. If the distance is less than or equal to the radius, then the point is inside the circle.

 


Source Code


 

#include <iostream>

#include <math.h>

 

void main()

{

    int nCountIntersections = 0;

    float x, y, cx, cy, radius;

    float distance;

    std::cout << "Program to find the given point inside or outside the circle:\n";

   

    std::cout << "Enter Center Point - X: ";

    std::cin >> cx;

 

    std::cout << "Enter Center Point - Y: ";

    std::cin >> cy;

 

    std::cout << "Enter Radius: ";

    std::cin >> radius;

 

    std::cout << "Enter Point - X: ";

    std::cin >> x;

 

    std::cout << "Enter Point - Y: ";

    std::cin >> y;

 

    distance = sqrt( (double)(cx-x)*(cx-x) + (cy-y)*(cy-y));

   

    std::cout << "\nDistance between the point and center of the circle: " << distance;

    if(distance <= radius)

        std::cout << "\nGiven point is inside the circle";

    else

        std::cout << "\nGiven point is outside the circle";

}

 

Output


 

Program to find the given point inside or outside the circle:

Enter Center Point - X: 10

Enter Center Point - Y: 10

Enter Radius: 7

Enter Point - X: 12

Enter Point - Y: 4

 

Distance between the point and center of the circle: 6.32456

Given point is inside the circle