{"id":23845,"date":"2021-10-05T20:28:41","date_gmt":"2021-10-05T14:58:41","guid":{"rendered":"https:\/\/python-programs.com\/?p=23845"},"modified":"2021-11-22T18:33:25","modified_gmt":"2021-11-22T13:03:25","slug":"python-program-for-area-of-a-polygon-with-given-n-ordered-vertices","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-area-of-a-polygon-with-given-n-ordered-vertices\/","title":{"rendered":"Python Program for Area of a Polygon with Given n Ordered Vertices"},"content":{"rendered":"
Given two lists which are the ordered coordinates of a polygon with n vertices, the task is to find the area of a given polygon.<\/p>\n
Ordered means that the coordinates are given either clockwise or anticlockwise from the first vertex to the last.<\/p>\n
Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the area of a given polygon for the given n ordered vertices in python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Given two lists which are the ordered coordinates of a polygon with n vertices, the task is to find the area of a given polygon. Ordered means that the coordinates are given either clockwise or anticlockwise from the first vertex to the last. Examples: Example1: Input: Given x-coordinates list = [1, 5, 1] Given y-coordinates …<\/p>\nGiven x-coordinates list = [1, 5, 1]\r\nGiven y-coordinates list = [6, 4, 3]<\/pre>\n
The Area of the given polygon for the given n ordered vertices :\r\n6<\/pre>\n
Given x-coordinates list = [2, 3, 3, 1]\r\nGiven y-coordinates list = [9, 2, 1, 2]<\/pre>\n
The Area of the given polygon for the given n ordered vertices :\r\n8<\/pre>\n
Program for Area of a Polygon with Given n Ordered Vertices in Python<\/h2>\n
\n
Method #1: Using Mathematical Formula (Static Input)<\/h3>\n
\n
# Give the x-coordinates list as static input and store it in a variable.\r\na = [1, 5, 1]\r\n# Give the y-coordinates list as static input and store it in another variable.\r\nb = [6, 4, 3]\r\n# Calculate the length of the given x-coordinates list and store it in another variable.\r\nlst_lengt = len(a)\r\n# Take a variable say polygn_area and initialize its value to 0.\r\npolygn_area = 0\r\n# Subtract 1 from the above x-coordinates list length and store it in another variable\r\n# say k .\r\nk = lst_lengt - 1\r\nprint(\"The Area of the given polygon for the given n ordered vertices :\")\r\n# Loop till the length of the given x-coordinates list using the for loop.\r\nfor itr in range(0, lst_lengt):\r\n # Calculate the area of the polygon using the above given mathematical formula and\r\n # add it to the above-initialized variable polygn_area.\r\n polygn_area += (a[k] + a[itr]) * (b[k] - b[itr])\r\n # Assign the value of the iterator to the k which is the previous vertex\r\n # to the iterator.\r\n k = itr\r\n# Divide the above polygn_area by 2, apply the abs() function to it which gives\r\n# the absolute value, and convert it to an integer using the int() function.\r\n# Store it in another variable.\r\nrslt = int(abs(polygn_area \/ 2.0))\r\n# Print the above result which is the area of the given polygon.\r\nprint(rslt)\r\n<\/pre>\n
The Area of the given polygon for the given n ordered vertices :\r\n6<\/pre>\n
Method #2: Using Mathematical Formula (User Input)<\/h3>\n
\n
# Give the x-coordinates list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\na = list(map(int, input( 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give the y-coordinates list as user input using list(),map(),input(),and split() functions.\r\n# Store it in another variable.\r\nb = list(map(int, input( 'Enter some random List Elements separated by spaces = ').split()))\r\n# Calculate the length of the given x-coordinates list and store it in another variable.\r\nlst_lengt = len(a)\r\n# Take a variable say polygn_area and initialize its value to 0.\r\npolygn_area = 0\r\n# Subtract 1 from the above x-coordinates list length and store it in another variable\r\n# say k .\r\nk = lst_lengt - 1\r\nprint(\"The Area of the given polygon for the given n ordered vertices :\")\r\n# Loop till the length of the given x-coordinates list using the for loop.\r\nfor itr in range(0, lst_lengt):\r\n # Calculate the area of the polygon using the above given mathematical formula and\r\n # add it to the above-initialized variable polygn_area.\r\n polygn_area += (a[k] + a[itr]) * (b[k] - b[itr])\r\n # Assign the value of the iterator to the k which is the previous vertex\r\n # to the iterator.\r\n k = itr\r\n# Divide the above polygn_area by 2, apply the abs() function to it which gives\r\n# the absolute value, and convert it to an integer using the int() function.\r\n# Store it in another variable.\r\nrslt = int(abs(polygn_area \/ 2.0))\r\n# Print the above result which is the area of the given polygon.\r\nprint(rslt)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 2 3 3 1\r\nEnter some random List Elements separated by spaces = 9 2 1 2\r\nThe Area of the given polygon for the given n ordered vertices :\r\n8<\/pre>\n