最大化由给定矩形边上的点形成的三角形面积
给定一个矩形[(x1, y1), (x2, y2)]表示左下角和右上角的坐标,其边平行于坐标轴,其周边有N个点(每边至少一个) .任务是最大化由这些点形成的三角形的面积。
例子:
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.
方法:要通过给定矩形上的坐标找到最大面积三角形,请找到相距最远的每一边的坐标。因此,假设有四个边a、b、c、d ,其中a 和 c是矩形的长度, b、d 是宽度。现在最大面积将是
MAX(长度*(任一宽度上最远坐标之间的距离)/2,宽度*(任一长度上最远坐标之间的距离)/2)。
请按照以下步骤解决给定的问题。
- 计算长度 = abs(x2 – x1)和宽度 = abs(y2 – y1)
- 找到每边最远的坐标。
- 使用上述公式计算面积。
- 返回找到的区域。
下面是上述方法的实现。
C++
// C++ program for above approach
#include
#include
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
输出
18
时间复杂度: O(N),其中 N 是给定的坐标数。
辅助空间: O(1)