Angle between 3 given vertices in a n-sided regular polygon
Last Updated :
13 Mar, 2022
Given a n-sided regular polygon and three vertices a1 , a2 and a3 , the task is to find the angle suspended at vertex a1 by vertex a2 and vertex a3.
Examples:
Input: n = 6, a1 = 1, a2 = 2, a3 = 4
Output: 90
Input: n = 5, a1 = 1, a2 = 2, a3 = 5
Output: 36
Approach:
The angle subtended by an edge on the center of n sided regular polygon is 360/n .
The angle subtended by vertices separated by k edges becomes (360*k)/n .
The chord between the vertices subtends an angle with half the value of the angle subtended at the center at the third vertex which is a point on the circumference on the circumcircle.
Let the angle obtained in this manner be a = (180*x)/n where k is number of edges between i and k.
Similarly for the opposite vertex we get the angle to be b = (180*y)/n where l is number of edges between j and k.
The angle between the three vertices thus equals 180-a-b .
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
double calculate_angle(int n, int i, int j, int k)
{
int x, y;
if (i < j)
x = j - i;
else
x = j + n - i;
if (j < k)
y = k - j;
else
y = k + n - j;
double ang1 = (180 * x) / n;
double ang2 = (180 * y) / n;
double ans = 180 - ang1 - ang2;
return ans;
}
int main()
{
int n = 5;
int a1 = 1;
int a2 = 2;
int a3 = 5;
cout << calculate_angle(n, a1, a2, a3);
return 0;
}
Java
class GFG
{
static double calculate_angle(int n, int i,
int j, int k)
{
int x, y;
if (i < j)
x = j - i;
else
x = j + n - i;
if (j < k)
y = k - j;
else
y = k + n - j;
double ang1 = (180 * x) / n;
double ang2 = (180 * y) / n;
double ans = 180 - ang1 - ang2;
return ans;
}
public static void main (String[] args)
{
int n = 5;
int a1 = 1;
int a2 = 2;
int a3 = 5;
System.out.println((int)calculate_angle(n, a1, a2, a3));
}
}
Python3
def calculate_angle(n, i, j, k):
x, y = 0, 0
if (i < j):
x = j - i
else:
x = j + n - i
if (j < k):
y = k - j
else:
y = k + n - j
ang1 = (180 * x) // n
ang2 = (180 * y) // n
ans = 180 - ang1 - ang2
return ans
n = 5
a1 = 1
a2 = 2
a3 = 5
print(calculate_angle(n, a1, a2, a3))
C#
using System;
class GFG
{
static double calculate_angle(int n, int i,
int j, int k)
{
int x, y;
if (i < j)
x = j - i;
else
x = j + n - i;
if (j < k)
y = k - j;
else
y = k + n - j;
double ang1 = (180 * x) / n;
double ang2 = (180 * y) / n;
double ans = 180 - ang1 - ang2;
return ans;
}
public static void Main ()
{
int n = 5;
int a1 = 1;
int a2 = 2;
int a3 = 5;
Console.WriteLine((int)calculate_angle(n, a1, a2, a3));
}
}
Javascript
<script>
function calculate_angle(n , i, j , k)
{
var x, y;
if (i < j)
x = j - i;
else
x = j + n - i;
if (j < k)
y = k - j;
else
y = k + n - j;
var ang1 = (180 * x) / n;
var ang2 = (180 * y) / n;
var ans = 180 - ang1 - ang2;
return ans;
}
var n = 5;
var a1 = 1;
var a2 = 2;
var a3 = 5;
document.write(parseInt(calculate_angle(n, a1, a2, a3)));
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Add Your Comment
Please Login to comment...