Open In App

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

Last Updated : 11 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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

 

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.

C++




// C++ program for above approach
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
 
// To find the maximum area of triangle
void maxTriangleArea(int rectangle[2][2],
                     int coordinates[][2],
                     int numberOfCoordinates)
{
 
    int l1min = INT_MAX, l2min = INT_MAX,
        l1max = INT_MIN, l2max = INT_MIN,
        b1min = INT_MAX, b1max = INT_MIN,
        b2min = INT_MAX, b2max = INT_MIN;
 
    int l1Ycoordinate = rectangle[0][1];
    int l2Ycoordinate = rectangle[1][1];
 
    int b1Xcoordinate = rectangle[0][0];
    int b2Xcoordinate = rectangle[1][0];
 
    // Always consider side parallel
    // to x-axis as length and
    // side parallel to y-axis as breadth
    for (int i = 0; i < numberOfCoordinates;
         i++) {
        coordinates[i][1];
 
        // coordinate on l1
        if (coordinates[i][1] == l1Ycoordinate) {
            l1min = min(l1min,
                        coordinates[i][0]);
            l1max = max(l1max,
                        coordinates[i][0]);
        }
 
        // Coordinate on l2
        if (coordinates[i][1] == l2Ycoordinate) {
            l2min = min(l2min,
                        coordinates[i][0]);
            l2max = max(l2max,
                        coordinates[i][0]);
        }
 
        // Coordinate on b1
        if (coordinates[i][0] == b1Xcoordinate) {
            b1min = min(b1min,
                        coordinates[i][1]);
            b1max = max(b1max,
                        coordinates[i][1]);
        }
 
        // Coordinate on b2
        if (coordinates[i][0] == b2Xcoordinate) {
            b2min = min(b2min,
                        coordinates[i][1]);
            b2max = max(b2max,
                        coordinates[i][1]);
        }
    }
 
    // Find maximum possible distance
    // on length
    int maxOfLength = max(abs(l1max - l1min),
                          abs(l2max - l2min));
 
    // Find maximum possible distance
    // on breadth
    int maxofBreadth = max(abs(b1max - b1min),
                           abs(b2max - b2min));
 
    // Calculate result base * height / 2
    float result
        = max((maxofBreadth
               * (abs(rectangle[0][0]
                      - rectangle[1][0]))),
              (maxOfLength
               * (abs(rectangle[0][1]
                      - rectangle[1][1]))))
          / 2.0;
 
    // Print the result
    cout << result;
}
 
// Driver Code
int main()
{
    // Rectangle with x1, y1 and x2, y2
    int rectangle[2][2] = { { 0, 0 },
                            { 6, 6 } };
 
    // Coordinates on sides of given rectangle
    int coordinates[9][2]
        = { { 0, 2 }, { 0, 3 }, { 0, 5 },
            { 2, 0 }, { 3, 0 }, { 6, 0 },
            { 6, 4 }, { 1, 6 }, { 6, 6 } };
 
    int numberOfCoordinates
        = sizeof(coordinates) / sizeof(coordinates[0]);
 
    maxTriangleArea(rectangle, coordinates,
                    numberOfCoordinates);
    return 0;
}


Java




// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // To find the maximum area of triangle
  static void maxTriangleArea(int[ ][ ] rectangle,
                              int[ ][ ] coordinates,
                              int numberOfCoordinates)
  {
 
    int l1min = Integer.MAX_VALUE, l2min = Integer.MAX_VALUE,
    l1max = Integer.MIN_VALUE, l2max = Integer.MIN_VALUE,
    b1min = Integer.MAX_VALUE, b1max = Integer.MIN_VALUE,
    b2min = Integer.MAX_VALUE, b2max = Integer.MIN_VALUE;
 
    int l1Ycoordinate = rectangle[0][1];
    int l2Ycoordinate = rectangle[1][1];
 
    int b1Xcoordinate = rectangle[0][0];
    int b2Xcoordinate = rectangle[1][0];
 
    // Always consider side parallel
    // to x-axis as length and
    // side parallel to y-axis as breadth
    for (int i = 0; i < numberOfCoordinates; i++) {
 
      // coordinate on l1
      if (coordinates[i][1] == l1Ycoordinate) {
        l1min = Math.min(l1min,
                         coordinates[i][0]);
        l1max = Math.max(l1max,
                         coordinates[i][0]);
      }
 
      // Coordinate on l2
      if (coordinates[i][1] == l2Ycoordinate) {
        l2min = Math.min(l2min,
                         coordinates[i][0]);
        l2max = Math.max(l2max,
                         coordinates[i][0]);
      }
 
      // Coordinate on b1
      if (coordinates[i][0] == b1Xcoordinate) {
        b1min = Math.min(b1min,
                         coordinates[i][1]);
        b1max = Math.max(b1max,
                         coordinates[i][1]);
      }
 
      // Coordinate on b2
      if (coordinates[i][0] == b2Xcoordinate) {
        b2min = Math.min(b2min,
                         coordinates[i][1]);
        b2max = Math.max(b2max,
                         coordinates[i][1]);
      }
    }
 
    // Find maximum possible distance
    // on length
    int maxOfLength = Math.max(Math.abs(l1max - l1min),
                               Math.abs(l2max - l2min));
 
    // Find maximum possible distance
    // on breadth
    int maxofBreadth = Math.max(Math.abs(b1max - b1min),
                                Math.abs(b2max - b2min));
 
    // Calculate result base * height / 2
    int result
      = Math.max((maxofBreadth
                  * (Math.abs(rectangle[0][0]
                              - rectangle[1][0]))),
                 (maxOfLength
                  * (Math.abs(rectangle[0][1]
                              - rectangle[1][1]))))
      / 2;
 
    // Print the result
    System.out.print(result);
  }
 
  // Driver Code
  public static void main (String[] args)
  {
     
    // Rectangle with x1, y1 and x2, y2
    int[ ][ ] rectangle = { { 0, 0 },
                           { 6, 6 } };
 
    // Coordinates on sides of given rectangle
    int[ ][ ] coordinates
      = { { 0, 2 }, { 0, 3 }, { 0, 5 },
         { 2, 0 }, { 3, 0 }, { 6, 0 },
         { 6, 4 }, { 1, 6 }, { 6, 6 } };
 
    int numberOfCoordinates
      = coordinates.length;
 
    maxTriangleArea(rectangle, coordinates,
                    numberOfCoordinates);
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3




# Python code for the above approach
 
# To find the maximum area of triangle
def maxTriangleArea(rectangle, coordinates, numberOfCoordinates):
 
    l1min = 10 ** 9
    l2min = 10 ** 9
    l1max = 10 ** -9
    l2max = 10 ** -9
    b1min = 10 ** 9
    b1max = 10 ** -9
    b2min = 10 ** 9
    b2max = 10 ** -9
 
    l1Ycoordinate = rectangle[0][1];
    l2Ycoordinate = rectangle[1][1];
 
    b1Xcoordinate = rectangle[0][0];
    b2Xcoordinate = rectangle[1][0];
 
    # Always consider side parallel
    # to x-axis as length and
    # side parallel to y-axis as breadth
    for i in range(numberOfCoordinates):
        coordinates[i][1];
 
        # coordinate on l1
        if (coordinates[i][1] == l1Ycoordinate) :
            l1min = min(l1min, coordinates[i][0]);
            l1max = max(l1max, coordinates[i][0]);
         
 
        # Coordinate on l2
        if (coordinates[i][1] == l2Ycoordinate):
            l2min = min(l2min, coordinates[i][0]);
            l2max = max(l2max, coordinates[i][0]);
 
 
        # Coordinate on b1
        if (coordinates[i][0] == b1Xcoordinate):
            b1min = min(b1min, coordinates[i][1]);
            b1max = max(b1max, coordinates[i][1]);
         
 
        # Coordinate on b2
        if (coordinates[i][0] == b2Xcoordinate):
            b2min = min(b2min, coordinates[i][1]);
            b2max = max(b2max, coordinates[i][1]);
         
 
    # Find maximum possible distance
    # on length
    maxOfLength = max(abs(l1max - l1min), abs(l2max - l2min));
 
    # Find maximum possible distance
    # on breadth
    maxofBreadth = max(abs(b1max - b1min), abs(b2max - b2min));
 
    # Calculate result base * height / 2
    result = max((maxofBreadth * (abs(rectangle[0][0] - rectangle[1][0]))),
            (maxOfLength * (abs(rectangle[0][1] - rectangle[1][1])))) / 2.0;
 
    # Print the result
    print(int(result));
 
 
# Driver Code
 
# Rectangle with x1, y1 and x2, y2
rectangle = [[0, 0],[6, 6]];
 
# Coordinates on sides of given rectangle
coordinates = [[0, 2], [0, 3], [0, 5],
    [2, 0], [3, 0], [6, 0],
    [6, 4], [1, 6], [6, 6]];
 
numberOfCoordinates = len(coordinates)
 
maxTriangleArea(rectangle, coordinates, numberOfCoordinates);
 
# This code is contributed by gfgking


C#




// C# program for the above approach
using System;
class GFG {
 
  // To find the maximum area of triangle
  static void maxTriangleArea(int[,] rectangle,
                              int[,] coordinates,
                              int numberOfCoordinates)
  {
 
    int l1min = Int32.MaxValue, l2min = Int32.MaxValue,
    l1max = Int32.MinValue, l2max = Int32.MinValue,
    b1min = Int32.MaxValue, b1max = Int32.MinValue,
    b2min = Int32.MaxValue, b2max = Int32.MinValue;
 
    int l1Ycoordinate = rectangle[0,1];
    int l2Ycoordinate = rectangle[1,1];
 
    int b1Xcoordinate = rectangle[0,0];
    int b2Xcoordinate = rectangle[1,0];
 
    // Always consider side parallel
    // to x-axis as length and
    // side parallel to y-axis as breadth
    for (int i = 0; i < numberOfCoordinates; i++) {
 
      // coordinate on l1
      if (coordinates[i,1] == l1Ycoordinate) {
        l1min = Math.Min(l1min,
                         coordinates[i,0]);
        l1max = Math.Max(l1max,
                         coordinates[i,0]);
      }
 
      // Coordinate on l2
      if (coordinates[i,1] == l2Ycoordinate) {
        l2min = Math.Min(l2min,
                         coordinates[i,0]);
        l2max = Math.Max(l2max,
                         coordinates[i,0]);
      }
 
      // Coordinate on b1
      if (coordinates[i,0] == b1Xcoordinate) {
        b1min = Math.Min(b1min,
                         coordinates[i,1]);
        b1max = Math.Max(b1max,
                         coordinates[i,1]);
      }
 
      // Coordinate on b2
      if (coordinates[i,0] == b2Xcoordinate) {
        b2min = Math.Min(b2min,
                         coordinates[i,1]);
        b2max = Math.Max(b2max,
                         coordinates[i,1]);
      }
    }
 
    // Find maximum possible distance
    // on length
    int maxOfLength = Math.Max(Math.Abs(l1max - l1min),
                               Math.Abs(l2max - l2min));
 
    // Find maximum possible distance
    // on breadth
    int maxofBreadth = Math.Max(Math.Abs(b1max - b1min),
                                Math.Abs(b2max - b2min));
 
    // Calculate result base * height / 2
    int result
      = Math.Max((maxofBreadth
                  * (Math.Abs(rectangle[0,0]
                              - rectangle[1,0]))),
                 (maxOfLength
                  * (Math.Abs(rectangle[0,1]
                              - rectangle[1,1]))))
      / 2;
 
    // Print the result
    Console.Write(result);
  }
 
  // Driver Code
  public static void Main ()
  {
 
    // Rectangle with x1, y1 and x2, y2
    int[,] rectangle = { { 0, 0 },
                        { 6, 6 } };
 
    // Coordinates on sides of given rectangle
    int[,] coordinates
      = { { 0, 2 }, { 0, 3 }, { 0, 5 },
         { 2, 0 }, { 3, 0 }, { 6, 0 },
         { 6, 4 }, { 1, 6 }, { 6, 6 } };
 
    int numberOfCoordinates
      = coordinates.GetLength(0);
 
    maxTriangleArea(rectangle, coordinates,
                    numberOfCoordinates);
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript




<script>
       // JavaScript code for the above approach
 
 
       // To find the maximum area of triangle
       function maxTriangleArea(rectangle,
           coordinates,
           numberOfCoordinates) {
 
           let l1min = Number.MAX_VALUE, l2min = Number.MAX_VALUE,
               l1max = Number.MIN_VALUE, l2max = Number.MIN_VALUE,
               b1min = Number.MAX_VALUE, b1max = Number.MIN_VALUE,
               b2min = Number.MAX_VALUE, b2max = Number.MIN_VALUE;
 
           let l1Ycoordinate = rectangle[0][1];
           let l2Ycoordinate = rectangle[1][1];
 
           let b1Xcoordinate = rectangle[0][0];
           let b2Xcoordinate = rectangle[1][0];
 
           // Always consider side parallel
           // to x-axis as length and
           // side parallel to y-axis as breadth
           for (let i = 0; i < numberOfCoordinates;
               i++) {
               coordinates[i][1];
 
               // coordinate on l1
               if (coordinates[i][1] == l1Ycoordinate) {
                   l1min = Math.min(l1min,
                       coordinates[i][0]);
                   l1max = Math.max(l1max,
                       coordinates[i][0]);
               }
 
               // Coordinate on l2
               if (coordinates[i][1] == l2Ycoordinate) {
                   l2min = Math.min(l2min,
                       coordinates[i][0]);
                   l2max = Math.max(l2max,
                       coordinates[i][0]);
               }
 
               // Coordinate on b1
               if (coordinates[i][0] == b1Xcoordinate) {
                   b1min = Math.min(b1min,
                       coordinates[i][1]);
                   b1max = Math.max(b1max,
                       coordinates[i][1]);
               }
 
               // Coordinate on b2
               if (coordinates[i][0] == b2Xcoordinate) {
                   b2min = Math.min(b2min,
                       coordinates[i][1]);
                   b2max = Math.max(b2max,
                       coordinates[i][1]);
               }
           }
 
           // Find maximum possible distance
           // on length
           let maxOfLength = Math.max(Math.abs(l1max - l1min),
               Math.abs(l2max - l2min));
 
           // Find maximum possible distance
           // on breadth
           let maxofBreadth = Math.max(Math.abs(b1max - b1min),
               Math.abs(b2max - b2min));
 
           // Calculate result base * height / 2
           let result
               = Math.max((maxofBreadth
                   * (Math.abs(rectangle[0][0]
                       - rectangle[1][0]))),
                   (maxOfLength
                       * (Math.abs(rectangle[0][1]
                           - rectangle[1][1]))))
               / 2.0;
 
           // Print the result
           document.write(result);
       }
 
       // Driver Code
 
       // Rectangle with x1, y1 and x2, y2
       let rectangle = [[0, 0],
       [6, 6]];
 
       // Coordinates on sides of given rectangle
       let coordinates
           = [[0, 2], [0, 3], [0, 5],
           [2, 0], [3, 0], [6, 0],
           [6, 4], [1, 6], [6, 6]];
 
       let numberOfCoordinates
           = coordinates.length;
 
       maxTriangleArea(rectangle, coordinates,
           numberOfCoordinates);
 
      // This code is contributed by Potta Lokesh
   </script>


Output

18

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



Similar Reads

Calculate ratio of area of a triangle inscribed in an Ellipse and the triangle formed by corresponding points on auxiliary circle
Given two integers A and B representing the lengths of semi-major and semi-minor axes of an ellipse, the task is to calculate the ratio of any triangle inscribed in the ellipse and that of the triangle formed by the corresponding points on its auxiliary circle. Examples: Input: A = 1, B = 2Output: 2Explanation: Ratio = B / A = 2 / 1 = 2 Input: A =
4 min read
Maximum size of subset of given array such that a triangle can be formed by any three integers as the sides of the triangle
Given an array arr[] consisting of N integers, the task is to find the size of the largest subset of the array such that a triangle can be formed from any of the three integers of the subset as the sides of a triangle. Examples: Input: arr[] = {1, 4, 7, 4}Output: 3Explanation: A possible subsets that follow the given conditions are {1, 4, 4} and {4
7 min read
Area of the largest rectangle formed by lines parallel to X and Y axis from given set of points
Given an array arr[] consisting of N pair of integers representing coordinates of N points, the task is to find the area of the largest rectangle formed by straight lines drawn parallel to X and Y-axis from a given set of points. Examples: Input: arr[] = {{0, 0}, {1, 1}}Output: 1Explanation: The area of the largest rectangle is 1 formed by the coor
6 min read
Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
Given N-sided polygon we need to find the total number of triangles formed by joining the vertices of the given polygon with exactly two sides being common and no side being common.Examples: Input : N = 6 Output : 6 2 The image below is of a triangle forming inside a Hexagon by joining vertices as shown above. The triangle formed has two sides (AB
5 min read
Queries to count points lying on or inside an isosceles Triangle with given length of equal sides
Given an array arr[][] of dimension N * 2 representing the co-ordinates of N points and an array Q[] consisting of M integers, the task for every element in the Q[i] is to find the number of points lying inside or on the right-angled isosceles triangle formed on the positive co-ordinate axis with two equal sides of length Q[i]in each query. Example
7 min read
Maximum area rectangle by picking four sides from array
Given an array of n positive integers that represent lengths. Find out the maximum possible area whose four sides are picked from the given array. Note that a rectangle can only be formed if there are two pairs of equal values in the given array. Examples: Input : arr[] = {2, 1, 2, 5, 4, 4} Output : 8 Explanation : Dimension will be 4 * 2 Input : a
10 min read
Find area of triangle if two vectors of two adjacent sides are given
Given two vectors in form of (xi+yj+zk) of two adjacent sides of a triangle. The task is to find out the area of a triangle.Examples: Input: x1 = -2, y1 = 0, z1 = -5 x2 = 1, y2 = -2, z2 = -1 Output: Area = 6.422616289332565Input: x1 = -2, y1 = 1, z1 = 5 x2 = 1, y2 = 3, z2 = -1 Output: Area = 8.860022573334675 Approach: Suppose we have two vectors a
5 min read
Find all sides of a right angled triangle from given hypotenuse and area | Set 1
Given hypotenuse and area of a right angle triangle, get its base and height and if any triangle with given hypotenuse and area is not possible, print not possible.Examples: Input : hypotenuse = 5, area = 6 Output : base = 3, height = 4 Input : hypotenuse = 5, area = 7 Output : No triangle possible with above specification. Recommended PracticeArea
9 min read
Maximum area of a Rectangle that can be circumscribed about a given Rectangle of size LxW
Given a rectangle of dimensions L and W. The task is to find the maximum area of a rectangle that can be circumscribed about a given rectangle with dimensions L and W. Examples: Input: L = 10, W = 10Output: 200 Input: L = 18, W = 12Output: 450 Approach: Let below is the given rectangle EFGH of dimensions L and W. We have to find the area of rectang
4 min read
Maximize Perimeter of Quadrilateral formed by choosing sides from given Array
Given an array arr of size N where each element represents the length of a side, the task is to find the maximum perimeter quadrilateral that can be created using the sides in the given array. If no quadrilateral can be formed print -1. Examples: Input: arr[ ] = {3, 1, 2, 4, 2, 1}Output: 11Explanation: A quadrilateral of perimeter 4 + 3 + 2 + 2 = 1
8 min read