Find three vertices in an N-sided regular polygon such that the angle subtended at a vertex by two other vertices is closest to A
Last Updated :
09 Jul, 2021
Given an N-sided regular convex polygon and an angle A in degrees, the task is to find any 3 vertices i, j, and k, such that the angle subtended at vertex j by vertex i and k is closest to A.
Note: Each vertex is numbered from 1 to N in an anticlockwise direction starting from any vertex.
Examples:
Input: N = 3, A = 15
Output: 2 1 3
Explanation:
The given polygon is a triangle, therefore the minimum angle that can be subtended on any vertex is equal to 30 which is closest to A and includes all the vertices of the polygon.
Input: N = 4, A = 90
Output: 2 1 4
Approach: The given problem can be solved based on the following observations:
- The vertices of a regular polygon lie on a circle.
- The central angle of n sided regular polygon is 360/N.
- The angle subtended on the center of the circle by vertices separated by X edges is equals to (360*X)/N.
- By the theorem, the angle subtended on the circum-circle by an arc is half of the angle subtended at the center by the same arc. Therefore, the angle subtended on a vertex by an arc formed by X edges would be equal to (180*X)/N.
- As the polygon is regular therefore the same angle can be subtended by taking any 2 vertices of edge difference equal to X.
Follow the steps below to solve the problem:
- Initialize two variables, say ans as 0 and minElement as INT_MAX to store the count of edges which subtends the angle closest to A and to store the closest angle respectively.
- Iterate over the range [1, N – 2] using the variable i and perform the following steps:
- Find the angle subtended on circum-circle by an arc formed by i edges using the above formula and store it in a variable, angle.
- Check if the absolute value of (A – angle) is less than the absolute value of (minElement – A), then update the value of i to ans and the value of angle to minElement.
- After completing the above steps, print the vertices {2, 1, 2 + ans} as the resultant vertices.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXN 1000000
void closestsAngle( int N, int A)
{
double mi = INT_MAX;
int ans = 0;
for ( int i = 1; i < N - 1; i++) {
double angle = 180.0 * i / N;
if ( fabs (angle - A) < fabs (mi - A)) {
mi = angle;
ans = i;
}
}
cout << 2 << ' ' << 1 << ' '
<< 2 + ans;
}
int main()
{
int N = 3, A = 15;
closestsAngle(N, A);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void closestsAngle( int N, int A)
{
double mi = Integer.MAX_VALUE;
int ans = 0 ;
for ( int i = 1 ; i < N - 1 ; i++) {
double angle = 180.0 * i / N;
if (Math.abs(angle - A) < Math.abs(mi - A)) {
mi = angle;
ans = i;
}
}
System.out.print( 2 + " " + 1 + " " + ( 2 + ans));
}
public static void main(String[] args)
{
int N = 3 , A = 15 ;
closestsAngle(N, A);
}
}
|
Python3
import math
import sys
def closestsAngle(N, A):
mi = sys.maxsize
ans = 0
for i in range (N ):
angle = 180.0 * i / N
if (math.fabs(angle - A) < math.fabs(mi - A)):
mi = angle
i + = 1
ans = i
print ( 2 , 1 , 2 + ans)
if __name__ = = '__main__' :
N = 3
A = 15
closestsAngle(N, A)
|
C#
using System;
class GFG{
static void closestsAngle( int N, int A)
{
double mi = Int32.MaxValue;
int ans = 0;
for ( int i = 1; i < N - 1; i++)
{
double angle = 180.0 * i / N;
if (Math.Abs(angle - A) <
Math.Abs(mi - A))
{
mi = angle;
ans = i;
}
}
Console.Write(2 + " " + 1 +
" " + (2 + ans));
}
public static void Main()
{
int N = 3, A = 15;
closestsAngle(N, A);
}
}
|
Javascript
<script>
function closestsAngle(N, A) {
let mi = Number.MAX_VALUE;
let ans = 0;
for (let i = 1; i < N - 1; i++) {
let angle = 180.0 * i / N;
if (Math.abs(angle - A) < Math.abs(mi - A)) {
mi = angle;
ans = i;
}
}
document.write(2 + ' ' + 1 + ' '
+ parseInt(2 + ans));
}
let N = 3, A = 15;
closestsAngle(N, A);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...