Given n > 3, find number of diagonals in n sided convex polygon.
According to Wikipedia, In geometry, a diagonal is a line segment joining two vertices of a polygon or polyhedron, when those vertices are not on the same edge. Informally, any sloping line is called diagonal.
Input : 5 Output : 5
Explanation: Five possible diagonals are : AC, AD, BD, BE, CE
Since for an n-sided convex polygon, from each vertex, we can draw n-3 diagonals leaving two adjacent vertices and itself. Following this way for n-vertices, there will be n*(n-3) diagonals but then we will be calculating each diagonal twice so total number of diagonals become n*(n-3)/2
Here is code for above formula.
5 sided convex polygon have 5 diagonals
This article is contributed by Pratik Chhajer. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- How to check if a given point lies inside or outside a polygon?
- Convex Hull | Set 1 (Jarvis's Algorithm or Wrapping)
- Given n line segments, find if any two segments intersect
- Convex Hull | Set 2 (Graham Scan)
- Find if two rectangles overlap
- Minimum Cost Polygon Triangulation
- Maximum number of 2x2 squares that can be fit inside a right isosceles triangle
- Find Simple Closed Path for a given set of points
- Number of Integral Points between Two Points
- Area of a polygon with given n ordered vertices
- n'th Pentagonal Number
- Find all sides of a right angled triangle from given hypotenuse and area | Set 1
- Find perimeter of shapes formed with 1s in binary matrix
- Paper Cut into Minimum Number of Squares
- Find an Integer point on a line segment with given two ends