Check whether a point lies inside a sphere or not
Last Updated :
25 Aug, 2022
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++
#include <bits/stdc++.h>
using namespace std;
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);
return (x1 + y1 + z1);
}
int main()
{
int cx = 1, cy = 2, cz = 3;
int r = 5;
int x = 4, y = 5, z = 2;
int ans = check(cx, cy, cz, x, y, z);
if (ans < (r * r))
cout << "Point is inside the sphere";
else if (ans == (r * r))
cout << "Point lies on the sphere";
else
cout << "Point is outside the sphere";
return 0;
}
|
Java
import java.io.*;
import java.util.*;
import java.lang.*;
class GfG {
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);
return (x1 + y1 + z1);
}
public static void main(String[] args)
{
int cx = 1, cy = 2, cz = 3;
int r = 5;
int x = 4, y = 5, z = 2;
int ans = check(cx, cy, cz, x, y, z);
if (ans < (r * r))
System.out.println("Point is inside"
+ " the sphere");
else if (ans == (r * r))
System.out.println("Point lies on"
+ " the sphere");
else
System.out.println("Point is outside"
+ " the sphere");
}
}
|
Python
import math
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)
cx = 1
cy = 2
cz = 3
r = 5
x = 4
y = 5
z = 2
ans = check(cx, cy, cz, x, y, z);
if ans<(r**2):
print("Point is inside the sphere")
elif ans ==(r**2):
print("Point lies on the sphere")
else:
print("Point is outside the sphere")
|
C#
using System;
class GFG
{
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);
return (x1 + y1 + z1);
}
public static void Main()
{
int cx = 1, cy = 2, cz = 3;
int r = 5;
int x = 4, y = 5, z = 2;
int ans = check(cx, cy, cz,
x, y, z);
if (ans < (r * r))
Console.WriteLine("Point is inside" +
" the sphere");
else if (ans == (r * r))
Console.WriteLine("Point lies on" +
" the sphere");
else
Console.WriteLine("Point is outside" +
" the sphere");
}
}
|
PHP
<?php
function check($cx, $cy, $cz,
$x, $y, $z)
{
$x1 = pow(($x - $cx), 2);
$y1 = pow(($y - $cy), 2);
$z1 = pow(($z - $cz), 2);
return ($x1 + $y1 + $z1);
}
$cx = 1;
$cy = 2;
$cz = 3;
$r = 5;
$x = 4;
$y = 5;
$z = 2;
$ans = check($cx, $cy, $cz,
$x, $y, $z);
if ($ans < ($r * $r))
echo "Point is inside " .
"the sphere";
else if ($ans == ($r * $r))
echo "Point lies on the sphere";
else
echo "Point is outside " .
"the sphere";
?>
|
Javascript
<script>
function check(cx, cy, cz, x, y, z)
{
let x1 = Math.pow((x - cx), 2);
let y1 = Math.pow((y - cy), 2);
let z1 = Math.pow((z - cz), 2);
return (x1 + y1 + z1);
}
let cx = 1, cy = 2, cz = 3;
let r = 5;
let x = 4, y = 5, z = 2;
let ans = check(cx, cy, cz, x, y, z);
if (ans < (r * r))
document.write("Point is inside the sphere");
else if (ans == (r * r))
document.write("Point lies on the sphere");
else
document.write("Point is outside the sphere");
</script>
|
Output:
Point is inside the sphere
Time Complexity: O(1)
Auxiliary Space: O(1)
Please Login to comment...