-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
33 lines (31 loc) · 1.11 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//利用叉乘求三角形面积,已知三个点A[x1, y1], B[x2, y2], C[x3, y3]。那面积就是 S=(x1*y2 - x1*y3 + x2*y3 - x2*y1 + x3*y1 - x3*y2) * 0.5
using System;
namespace LargestTriangleArea
{
class Program
{
static void Main(string[] args)
{
int[][] points = new int[3][]
{
new int[]{1,0},
new int[]{0,0},
new int[]{0,1},
};
Console.WriteLine(LargestTriangleArea(points));
}
static double LargestTriangleArea(int[][] points)
{
double maxArea = 0;
for (int i = 0; i < points.Length; i++)
for (int j = i + 1; j < points.Length; j++)
for (int k = j + 1; k < points.Length; k++)
maxArea = Math.Max(maxArea, CalcArea(points[i], points[j], points[k]));
return maxArea;
}
static double CalcArea(int[] a, int[] b, int[] c)
{
return Math.Abs(a[0] * b[1] - a[0] * c[1] + b[0] * c[1] - b[0] * a[1] + c[0] * a[1] - c[0] * b[1]) * 0.5;
}
}
}