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++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Function that checks whether given angle
// can be created using any 3 sides
double calculate_angle(int n, int i, int j, int k)
{
// Initialize x and y
int x, y;
// Calculate the number of vertices
// between i and j, j and k
if (i < j)
x = j - i;
else
x = j + n - i;
if (j < k)
y = k - j;
else
y = k + n - j;
// Calculate the angle subtended
// at the circumference
double ang1 = (180 * x) / n;
double ang2 = (180 * y) / n;
// Angle subtended at j can be found
// using the fact that the sum of angles
// of a triangle is equal to 180 degrees
double ans = 180 - ang1 - ang2;
return ans;
}
// Driver code
int main()
{
int n = 5;
int a1 = 1;
int a2 = 2;
int a3 = 5;
cout << calculate_angle(n, a1, a2, a3);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function that checks whether given angle
// can be created using any 3 sides
static double calculate_angle(int n, int i,
int j, int k)
{
// Initialize x and y
int x, y;
// Calculate the number of vertices
// between i and j, j and k
if (i < j)
x = j - i;
else
x = j + n - i;
if (j < k)
y = k - j;
else
y = k + n - j;
// Calculate the angle subtended
// at the circumference
double ang1 = (180 * x) / n;
double ang2 = (180 * y) / n;
// Angle subtended at j can be found
// using the fact that the sum of angles
// of a triangle is equal to 180 degrees
double ans = 180 - ang1 - ang2;
return ans;
}
// Driver code
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));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 implementation of the approach
# Function that checks whether given angle
# can be created using any 3 sides
def calculate_angle(n, i, j, k):
# Initialize x and y
x, y = 0, 0
# Calculate the number of vertices
# between i and j, j and k
if (i < j):
x = j - i
else:
x = j + n - i
if (j < k):
y = k - j
else:
y = k + n - j
# Calculate the angle subtended
# at the circumference
ang1 = (180 * x) // n
ang2 = (180 * y) // n
# Angle subtended at j can be found
# using the fact that the sum of angles
# of a triangle is equal to 180 degrees
ans = 180 - ang1 - ang2
return ans
# Driver code
n = 5
a1 = 1
a2 = 2
a3 = 5
print(calculate_angle(n, a1, a2, a3))
# This code is contributed by Mohit Kumar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function that checks whether given angle
// can be created using any 3 sides
static double calculate_angle(int n, int i,
int j, int k)
{
// Initialize x and y
int x, y;
// Calculate the number of vertices
// between i and j, j and k
if (i < j)
x = j - i;
else
x = j + n - i;
if (j < k)
y = k - j;
else
y = k + n - j;
// Calculate the angle subtended
// at the circumference
double ang1 = (180 * x) / n;
double ang2 = (180 * y) / n;
// Angle subtended at j can be found
// using the fact that the sum of angles
// of a triangle is equal to 180 degrees
double ans = 180 - ang1 - ang2;
return ans;
}
// Driver code
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));
}
}
// This code is contributed by ihritik
JavaScript
<script>
// JavaScript implementation of the approach
// Function that checks whether given angle
// can be created using any 3 sides
function calculate_angle(n , i, j , k)
{
// Initialize x and y
var x, y;
// Calculate the number of vertices
// between i and j, j and k
if (i < j)
x = j - i;
else
x = j + n - i;
if (j < k)
y = k - j;
else
y = k + n - j;
// Calculate the angle subtended
// at the circumference
var ang1 = (180 * x) / n;
var ang2 = (180 * y) / n;
// Angle subtended at j can be found
// using the fact that the sum of angles
// of a triangle is equal to 180 degrees
var ans = 180 - ang1 - ang2;
return ans;
}
// Driver code
var n = 5;
var a1 = 1;
var a2 = 2;
var a3 = 5;
document.write(parseInt(calculate_angle(n, a1, a2, a3)));
// This code is contributed by 29AjayKumar
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Number of occurrences of a given angle formed using 3 vertices of a n-sided regular polygon Given an n-sided regular polygon and an angle ?, the task is to find number of occurrences of angle ( Ai, Aj, Ak ) = ? ( i < j < k) in a regular n-gon (regular polygon with n vertices) with vertices marked as A1, A2, ..., An.Examples: Input: n = 4, ang = 90 Output: 4 Input: n = 6, ang = 50 Out
6 min read
Find the angle of Rotational Symmetry of an N-sided regular polygon Given an integer N which is the number of sides of a regular polygon. The task is to find the smallest angle of rotation such that the generated regular polygons have a similar position and dimensions, i.e. the new rotated polygon is in symmetry with the initial one. A shape is said to have a rotati
4 min read
Apothem of a n-sided regular polygon Given here the side length a of a regular n-sided polygon, the task is to find the length of its Apothem. Apothem is the line drawn from the center of the polygon that is perpendicular to one of its sides. Examples: Input a = 9, n = 6 Output: 7.79424 Input: a = 8, n = 7 Output: 8.30609 Approach: In
4 min read
Program to find the Interior and Exterior Angle of a Regular Polygon Given the number of sides n of a regular polygon. The task is to find out the Interior angle and Exterior angle of the polygon.Examples: Input : n = 6 Output : Interior angle: 120 Exterior angle: 60 Input : n = 10 Output: Interior angle: 144 Exterior angle: 36 Interior angle: The angle between two a
4 min read
Minimum side of square embedded in Regular polygon with N sides Given an even number N which represents the number of sides of a regular polygon with N vertices, the task is to find the square of the minimum size such that given Polygon can completely embed in the square. A Polygon is a convex figure and has equal sides and equal angles. All sides have length 1.
8 min read