<?php
    
if (!empty($_SERVER['SCRIPT_FILENAME']) && basename($_SERVER['SCRIPT_FILENAME']) == 'rotassist-model.php')
        exit;

    require_once(
'rotassist-model-checks.php');

    function 
model_calculate($params)
    {
        
$sx $params['sx'];
        
$sy $params['sy'];

        if (!
$params['refpoint'])
        {
            
$sx /= 2;
            
$sy /= 2;
        }

        
$ax $params['ax'] - $sx;
        
$ay $params['ay'] - $sy;

        
$bx $params['bx'] - $sx;
        
$by $params['by'] - $sy;

        switch (
$params['orientation'])
        {
            case 
'h':
                
$horiz true;
                break;

            case 
'v':
                
$horiz false;
                break;

            default:
                
$dx = ($ax $bx) ? ($ax $bx) : ($bx $ax);
                
$dy = ($ay $by) ? ($ay $by) : ($by $ay);
                
$horiz $dx $dy;
                break;
        }

        
$af $horiz $ax $ay;
        
$bf $horiz $bx $by;

        
$cos sqrt(pow($af $bf2)) / sqrt(pow($ax $bx2) + pow($ay $by2));
        
$angle acos($cos);

        if ((
$ax $bx) ^ ($ay $by))
            
$angle = -$angle;

        
$deg $params['angle'] == 'deg';

        if (
$deg)
            
$angle 360 $angle / (atan(1));

        return 
"Kąt wynosi $angle ".($deg 'stopni' 'radianów');
    }

    function 
model($params)
    {
        
$err model_check($params);
        if (
$err)
            return 
$err;

        return 
model_calculate($params);
    }
?>