VOOZH about

URL: https://www.geeksforgeeks.org/dsa/maximize-area-of-triangle-formed-by-points-on-sides-of-given-rectangle/

⇱ Maximize area of triangle formed by points on sides of given rectangle - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Maximize area of triangle formed by points on sides of given rectangle

Last Updated : 11 Feb, 2022

Given a rectangle [(x1, y1), (x2, y2)] denoting the coordinates of bottom-left corner and top-right corner whose sides are parallel to coordinates axes and N points on its perimeter (at least one on each side). The task is to maximize the area of a triangle formed by these points.

Examples:

Input: rectangle[][]= {{0, 0}, {6, 6}}, 
coordinates[][] = {{0, 2}, {0, 3}, {0, 5}, {2, 0}, {3, 0}, {6, 0}, {6, 4}, {1, 6}, {6, 6}}
Output: 18
Explanation: Refer to the image below for explanation

👁 Image

Approach: For finding the maximum area triangle by coordinates on a given rectangle, find the coordinates on each side which are most distant apart. So, suppose there are four sides a, b, c, d where a and c being the length of the rectangle and b, d being the breadth. Now the maximum area will be 
MAX ( length * ( distance between farthest coordinates on either of breadth) / 2, breadth * ( distance between farthest coordinates on either of length) / 2 ). 
Follow the steps below to solve the given problem.

  • Calculate the length = abs(x2 - x1) and breadth = abs(y2 - y1)
  • Find the coordinates which are farthest from each other on each side.
  • Use the above-mentioned formula to calculate the area.
  • Return the area found.

Below is the implementation of the above approach.


Output
18

Time complexity: O(N), Where N is the number of coordinates given.
Auxiliary Space: O(1)

Comment