
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Largest Triangle Area in Python
Suppose we have a list of points on a plane. We have to find the area of the largest triangle that can be formed by any 3 of the points.
So, if the input is like [[0,0],[0,1],[1,0],[0,2],[2,0]], then the output will be 2
To solve this, we will follow these steps −
- res := 0
- N := size of points list
- for i in range 0 to N - 2, do
- for j in range i + 1 to N - 1, do
- for k in range i + 2 to N, do
- (x1, y1) := points[i],
- (x2, y2) := points[j],
- (x3, y3) := points[k]
- res := maximum of res, 0.5 * |x1 *(y2 - y3) + x2 *(y3 - y1) + x3 *(y1 - y2)
- for k in range i + 2 to N, do
- for j in range i + 1 to N - 1, do
- return res
Let us see the following implementation to get better understanding −
Example
class Solution: def largestTriangleArea(self, points): res = 0 N = len(points) for i in range(N - 2): for j in range(i + 1, N - 1): for k in range(i + 2, N): (x1, y1), (x2, y2), (x3, y3) = points[i],points[j],points[k] res = max(res, 0.5 * abs(x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2))) return res ob = Solution() print(ob.largestTriangleArea([[0,0],[0,1],[1,0],[0,2],[2,0]]))
Input
[[0,0],[0,1],[1,0],[0,2],[2,0]]
Output
2.0
- Related Articles
- Largest Perimeter Triangle in Python
- Area of the Largest Triangle inscribed in a Hexagon in C++
- Area of the largest triangle that can be inscribed within a rectangle?
- Program to find largest perimeter triangle using Python
- C++ program to find the Area of the Largest Triangle inscribed in a Hexagon?
- Area of largest triangle that can be inscribed within a rectangle in C Program?
- Program to find largest rectangle area under histogram in python
- Program to find area of largest island in a matrix in Python
- Program to find area of largest submatrix by column rearrangements in Python
- Program to find area of largest square of 1s in a given matrix in python
- How to Calculate the Area of a Triangle using Python?
- Check if right triangle possible from given area and hypotenuse in Python
- Area of Reuleaux Triangle?
- Largest Number in Python
- Largest Gap in Python

Advertisements