Check whether a point exists in circle sector or not.
We have a circle centered at origin (0, 0). As input we are given with starting angle of the circle sector and the size of the circle sector in percentage.
Examples:
Input : Radius = 8
StartAngle = 0
Percentage = 12
x = 3 y = 4
Output : Point (3, 4) exists in the circle
sector
Input : Radius = 12
Startangle = 45
Percentage = 25
x = 3 y = 4
Output : Point (3, 4) does not exist in
the circle sector
Source:wikibooks.org
In this image starting angle is 0 degree, radius r and suppose that percentage of colored area is 12% then we calculate Ending Angle as 360/percentage + starting angle.
To find whether a point (x, y) exists in a circle sector (centered at origin) or not we find polar coordinates of that point and then go through the following steps:
- Convert x, y to polar coordinates using this
Angle = atan(y/x); Radius = sqrt(x * x + y * y);
- Then Angle must be between StartingAngle and EndingAngle, and Radius between 0 and your Radius.
C++
#include<bits/stdc++.h>
using namespace std;
void checkPoint( int radius, int x, int y, float percent,
float startAngle)
{
float endAngle = 360/percent + startAngle;
float polarradius = sqrt (x*x+y*y);
float Angle = atan (y/x);
if (Angle>=startAngle && Angle<=endAngle && polarradius<radius)
printf ( "Point (%d, %d) exist in the circle sector\n" , x, y);
else
printf ( "Point (%d, %d) does not exist in the circle sector\n" ,
x, y);
}
int main()
{
int radius = 8, x = 3, y = 4;
float percent = 12, startAngle = 0;
checkPoint(radius, x, y, percent, startAngle);
return 0;
}
|
Java
class GFG
{
static void checkPoint( int radius, int x, int y, float percent,
float startAngle)
{
float endAngle = 360 /percent + startAngle;
double polarradius = Math.sqrt(x*x+y*y);
double Angle = Math.atan(y/x);
if (Angle>=startAngle && Angle<=endAngle && polarradius<radius)
System.out.print( "Point" + "(" +x+ "," +y+ ")" +
" exist in the circle sector\n" );
else
System.out.print( "Point" + "(" +x+ "," +y+ ")" +
" exist in the circle sector\n" );
}
public static void main(String arg[])
{
int radius = 8 , x = 3 , y = 4 ;
float percent = 12 , startAngle = 0 ;
checkPoint(radius, x, y, percent, startAngle);
}
}
|
Python3
import math
def checkPoint(radius, x, y, percent, startAngle):
endAngle = 360 / percent + startAngle
polarradius = math.sqrt(x * x + y * y)
Angle = math.atan(y / x)
if (Angle > = startAngle and Angle < = endAngle
and polarradius < radius):
print ( "Point (" , x, "," , y, ") "
"exist in the circle sector" )
else :
print ( "Point (" , x, "," , y, ") "
"does not exist in the circle sector" )
radius, x, y = 8 , 3 , 4
percent, startAngle = 12 , 0
checkPoint(radius, x, y, percent, startAngle)
|
C#
using System.IO;
using System;
class GFG {
static void checkPoint( int radius, int x, int y,
float percent, float startAngle)
{
float endAngle = 360 / percent + startAngle;
float polarradius =
( float )Math.Sqrt(x * x + y * y);
float Angle = ( float )Math.Atan(y / x);
if (Angle >= startAngle && Angle <= endAngle
&& polarradius < radius)
Console.Write( "Point ({0}, {1}) exist in "
+ "the circle sector" , x, y);
else
Console.Write( "Point ({0}, {1}) does not "
+ "exist in the circle sector" , x, y);
}
public static void Main()
{
int radius = 8, x = 3, y = 4;
float percent = 12, startAngle = 0;
checkPoint(radius, x, y, percent, startAngle);
}
}
|
Javascript
<script>
function checkPoint(radius, x, y, percent, startAngle)
{
let endAngle = 360 / percent + startAngle;
let polarradius = Math.sqrt(x * x + y * y);
let Angle = Math.atan(y / x);
if (Angle >= startAngle &&
Angle <= endAngle &&
polarradius < radius)
document.write( "Point" + "(" + x +
"," + y + ")" +
" exist in the circle sector\n" );
else
document.write( "Point" + "(" + x +
"," + y + ")" +
" exist in the circle sector\n" );
}
let radius = 8, x = 3, y = 4;
let percent = 12, startAngle = 0;
checkPoint(radius, x, y, percent, startAngle);
</script>
|
Output :
Point(3, 4) exists in the circle sector
Time complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
22 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...