- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have outer points of a polygon in clockwise order. We have to check these points are forming a convex polygon or not. A polygon is said to be concave if any one of its interior angle is greater than 180°.

From this diagram it is clear that for each three consecutive points the interior angle is not more than 180° except CDE.

So, if the input is like points = [(3,4), (4,7),(7,8),(8,4),(12,3),(10,1),(5,2)], then the output will be True.

To solve this, we will follow these steps −

- n := size of points
- for i in range 0 to size of points, do
- p1 := points[i-2] when i > 1, otherwise points[n-2]
- p2 := points[i-2] when i > 0, otherwise points[n-1]
- p3 := points[i]
- if angle between points (p1, p2, p3) > 180, then
- return True

- return False

Let us see the following implementation to get better understanding −

import math def get_angle(a, b, c): angle = math.degrees(math.atan2(c[1]-b[1], c[0]-b[0]) - math.atan2(a[1]-b[1], a[0]-b[0])) return angle + 360 if angle < 0 else angle def solve(points): n = len(points) for i in range(len(points)): p1 = points[i-2] p2 = points[i-1] p3 = points[i] if get_angle(p1, p2, p3) > 180: return True return False points = [(3,4), (4,7),(7,8),(8,4),(12,3),(10,1),(5,2)] print(solve(points))

[(3,4), (4,7),(7,8),(8,4),(12,3),(10,1),(5,2)]

True

- Related Questions & Answers
- Program to check points are forming convex hull or not in Python
- Program to check linked list items are forming palindrome or not in Python
- Program to check whether domain and range are forming function or not in Python
- Program to check heap is forming max heap or not in Python
- Program to check given point in inside or boundary of given polygon or not in python
- Program to check whether parentheses are balanced or not in Python
- Program to check whether list of points form a straight line or not in Python
- Program to check programmers convention arrangements are correct or not in Python
- Program to check three consecutive odds are present or not in Python
- Program to check whether two sentences are similar or not in Python
- Program to check whether elements frequencies are even or not in Python
- Program to check given push pop sequences are proper or not in python
- Program to check whether two string arrays are equivalent or not in Python
- Program to check strings are rotation of each other or not in Python
- Program to check all listed delivery operations are valid or not in Python

Advertisements