D77 812. Largest Triangle Area
Topic Link
Topic analysis
Given a set of coordinates, the composition can return the largest area of a triangle.
Thinking
It can only be set for the cycle. Obtained using a triangular area seeking area formula.
The final code
<?php
class Solution {
/**
* @param Integer[][] $points
* @return Float
*/
function largestTriangleArea($points) {
$pointAmount = count($points);
$max = -1;
for($i=0;$i<=$pointAmount;$i++){
for($j=$i+1;$j<$pointAmount;$j++){
for($k=$j+1;$k<$pointAmount;$k++){
$p1 = $points[$i];
$p2 = $points[$j];
$p3 = $points[$k];
$area = abs($p1[0]*$p2[1]+$p2[0]*$p3[1]+$p3[0]*$p1[1]-$p1[0]*$p3[1]-$p2[0]*$p1[1]-$p3[0]*$p2[1])/2;
if($area>$max){
$max = $area;
}
}
}
}
return $max;
}
}
复制代码
If you find this article useful, welcomed with love of power assistance.