算法(计算多边形面积 (3边形-6边形) )

<?php
class Area_S{

    private $num_len = 3;
    private $args = [];
    private $deg = [];
    public $res = [];

    public function __construct($args = [],$deg=[]){

        if($args)
            $this->args = $args;
        
        $this->num_len = count($this->args);
        if($this->num_len > 6){
            $this->res = $this->return_data("","error Missing parameter ,Out of range",false);
            return false;
        }
        if($this->num_len -3 > count($deg)){
            $this->res = $this->return_data("","error Missing parameter",false);
            return false;
        }
        $this->deg = $deg;
        $this->run();
    }
    // run
    private function run(){
        // 两边加个角
        if($this->num_len == 2 && count($this->deg) == 1){
            $this->args[2] = $this->deg[array_keys($this->deg)[0]];
            $this->args[2] = $this->get_dui_line_s($this->args);
            $s = $this->area_s_3($this->args);
            // 三边
        }else if($this->num_len == 3){
            
            $s = $this->area_s_3($this->args);
            
            if($s == false){
               return false; 
            }
            
        }else{
            // 只能到6 再多就没法算了
            $s = 0;
            foreach($this->deg as $k => $v){
                $new_arr =[];
                $k_arr =explode("-",$k);
                $k_arr[0] -= 1;
                $k_arr[1] -= 1;
                if(!in_array($k_arr[0],array_keys($this->args))){
                    $this->res = $this->return_data("","Error, angle parameter error",false);
                    return ;
                }
                if(!in_array($k_arr[1],array_keys($this->args))){
                    $this->res = $this->return_data("","Error, angle parameter error",false);
                    return ;
                }
                array_push($new_arr , $this->args[$k_arr[0]]);
                array_push($new_arr , $this->args[$k_arr[1]]);
                array_push($new_arr , $v);
                $dui_line = $this->get_dui_line_s($new_arr);
                $new_arr[2] = $dui_line;
                $new_s = $this->area_s_3($new_arr);
                if(!$new_s){
                    return false;
                }
                $s += $new_s;
                unset($this->args[$k_arr[0]]);
                $this->args[$k_arr[1]] = $dui_line;
            }
            if(count($this->args) == 3){
                $new_s = $this->area_s_3($this->args);
                if(!$new_s){
                    return false;
                }
                $s += $new_s;
            }

        }
        $this->res = $this->return_data($s,"success",true); 
        return ;

    }
    // 返回 
    private function return_data($data,$msg,$type){
        
        if($type){
            return json_encode(['data'=>$data,"code"=>200,"msg"=>$msg]);
        }else{
           
            return json_encode(['data'=>$data,"code"=>400,"msg"=>$msg]);
        }

    }

    // 求面积
    private function area_s_3($arr){
        $arr = array_values($arr);
        // shi fo shi yi ge zheng cheng shu ju 
        $bool_type =$this->is_triangle($arr);
  
        if($bool_type == false){
            $this->res = $this->return_data("","data is error 1",false);
            return false;
        }
        $l = array_sum($arr)/2; // ban 
        if(count($arr) !== 3){
            $this->res = $this->return_data("","data is error 2",false);
            return false;
        }
        $s = sqrt($l * ($l-$arr[0])*($l-$arr[1])*($l-$arr[2]));
        return $s;
    }
    //判断是否是个三角形
    private function is_triangle($arr){
        if($arr[0]+$arr[1] < $arr[2]){
            return false;
        }else if($arr[1]+$arr[2] < $arr[0]){
            return false;
        }else if($arr[2]+$arr[0] < $arr[1]){
            return false;
        }else{
            return true;
        }
    }
    // 获取对角线
    private function get_dui_line_s($arr_deg){
        // liang tiao xian + yi ge jiao
        $arr_deg[2] = str_replace("deg","",$arr_deg[2]);
        
        $dui_line_len = sqrt($arr_deg[0]*$arr_deg[0] + $arr_deg[1]*$arr_deg[1] - 2*$arr_deg[0] *$arr_deg[1]*cos(deg2rad($arr_deg[2])));

        return $dui_line_len;

    }

}
$area = new Area_S([3,4],['1-2'=>'90deg']);

var_dump($area->res);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值