Check whether a point lies inside a sphere or not
Given co-ordinates of the center of a sphere (cx, cy, cz) and its radius r. Our task is to check whether a point (x, y, z) lies inside, outside or on this sphere.
Examples:
Input : Centre(0, 0, 0) Radius 3
Point(1, 1, 1)
Output :Point is inside the sphere
Input :Centre(0, 0, 0) Radius 3
Point(2, 1, 2)
Output :Point lies on the sphere
Input :Centre(0, 0, 0) Radius 3
Point(10, 10, 10)
Output :Point is outside the sphere
Approach:
Whether a point lies inside a sphere or not, depends upon its distance from the centre.
A point (x, y, z) is inside the sphere with center (cx, cy, cz) and radius r if
( x-cx ) ^2 + (y-cy) ^2 + (z-cz) ^ 2 < r^2
A point (x, y, z) lies on the sphere with center (cx, cy, cz) and radius r if
( x-cx ) ^2 + (y-cy) ^2 + (z-cz) ^ 2 = r^2
A point (x, y, z) is outside the sphere with center (cx, cy, cz) and radius r if
( x-cx ) ^2 + (y-cy) ^2 + (z-cz) ^ 2 > r^2
C++
// CPP code to illustrate above approach #include <bits/stdc++.h> using namespace std; // function to calculate the distance between centre and the point int check(int cx, int cy, int cz, int x, int y, int z) { int x1 = pow((x - cx), 2); int y1 = pow((y - cy), 2); int z1 = pow((z - cz), 2); // distance between the centre // and given point return (x1 + y1 + z1); } // Driver program to test above function int main() { // coordinates of centre int cx = 1, cy = 2, cz = 3; int r = 5; // radius of the sphere int x = 4, y = 5, z = 2; // coordinates of point int ans = check(cx, cy, cz, x, y, z); // distance btw centre and point is less // than radius if (ans < (r * r)) cout << "Point is inside the sphere"; // distance btw centre and point is // equal to radius else if (ans == (r * r)) cout << "Point lies on the sphere"; // distance btw center and point is // greater than radius else cout << "Point is outside the sphere"; return 0; } |
Java
// Java code to illustrate above approach import java.io.*; import java.util.*; import java.lang.*; class GfG { // function to calculate the distance // between centre and the point public static int check(int cx, int cy, int cz, int x, int y, int z) { int x1 = (int)Math.pow((x - cx), 2); int y1 = (int)Math.pow((y - cy), 2); int z1 = (int)Math.pow((z - cz), 2); // distance between the centre // and given point return (x1 + y1 + z1); } // Driver program to test above function public static void main(String[] args) { // coordinates of centre int cx = 1, cy = 2, cz = 3; int r = 5; // radius of the sphere // coordinates of point int x = 4, y = 5, z = 2; int ans = check(cx, cy, cz, x, y, z); // distance btw centre and point is less // than radius if (ans < (r * r)) System.out.println("Point is inside" + " the sphere"); // distance btw centre and point is // equal to radius else if (ans == (r * r)) System.out.println("Point lies on" + " the sphere"); // distance btw center and point is // greater than radius else System.out.println("Point is outside" + " the sphere"); } } // This code is contributed by Sagar Shukla. |
Python
# Python3 code to illustrate above approach import math # function to calculate distance btw center and given point def check(cx, cy, cz, x, y, z, ): x1 = math.pow((x-cx), 2) y1 = math.pow((y-cy), 2) z1 = math.pow((z-cz), 2) return (x1 + y1 + z1) # distance between the centre and given point # driver code cx = 1cy = 2 # coordinates of centre cz = 3 r = 5 # radius of sphere x = 4y = 5 # coordinates of the given point z = 2# function call to calculate distance btw centre and given point ans = check(cx, cy, cz, x, y, z); # distance btw centre and point is less than radius if ans<(r**2): print("Point is inside the sphere") # distance btw centre and point is equal to radius elif ans ==(r**2): print("Point lies on the sphere") # distance btw centre and point is greater than radius else: print("Point is outside the sphere") |
C#
// C# code to illustrate // above approach using System; class GFG { // function to calculate // the distance between // centre and the point public static int check(int cx, int cy, int cz, int x, int y, int z) { int x1 = (int)Math.Pow((x - cx), 2); int y1 = (int)Math.Pow((y - cy), 2); int z1 = (int)Math.Pow((z - cz), 2); // distance between the // centre and given point return (x1 + y1 + z1); } // Driver Code public static void Main() { // coordinates of centre int cx = 1, cy = 2, cz = 3; int r = 5; // radius of the sphere // coordinates of point int x = 4, y = 5, z = 2; int ans = check(cx, cy, cz, x, y, z); // distance btw centre // and point is less // than radius if (ans < (r * r)) Console.WriteLine("Point is inside" + " the sphere"); // distance btw centre // and point is // equal to radius else if (ans == (r * r)) Console.WriteLine("Point lies on" + " the sphere"); // distance btw center // and point is greater // than radius else Console.WriteLine("Point is outside" + " the sphere"); } } // This code is contributed by anuj_67. |
PHP
<?php // function to calculate // the distance between // centre and the point function check($cx, $cy, $cz, $x, $y, $z) { $x1 = pow(($x - $cx), 2); $y1 = pow(($y - $cy), 2); $z1 = pow(($z - $cz), 2); // distance between the // centre and given point return ($x1 + $y1 + $z1); } // Driver Code // coordinates of centre $cx = 1; $cy = 2; $cz = 3; $r = 5; // radius of the sphere $x = 4; $y = 5; $z = 2; // coordinates of point $ans = check($cx, $cy, $cz, $x, $y, $z); // distance btw centre and // point is less than radius if ($ans < ($r * $r)) echo "Point is inside " . "the sphere"; // distance btw centre and // point is equal to radius else if ($ans == ($r * $r)) echo "Point lies on the sphere"; // distance btw center and point // is greater than radius else echo "Point is outside " . "the sphere"; // This code is contributed // by shiv_bhakt ?> |
Output:
Point is inside the sphere
Time Complexity:O(1)
Recommended Posts:
- Check whether a given point lies inside a triangle or not
- How to check if a given point lies inside or outside a polygon?
- Check whether a given point lies inside a rectangle or not
- Check if a point lies on or inside a rectangle | Set-2
- Check whether a given point lies on or inside the rectangle | Set 3
- Find a point that lies inside exactly K given squares
- Check if a given circle lies completely inside the ring formed by two concentric circles
- Check whether the point (x, y) lies on a given line
- Check if a point is inside, outside or on the ellipse
- Check if a point is inside, outside or on the parabola
- Check if a circle lies inside another circle or not
- Largest sphere that can be inscribed inside a cube
- Triangle with no point inside
- Find intersection point of lines inside a section
- Find minimum radius such that atleast k point lie inside the circle
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



