示例#1
0
文件: 59.php 项目: bamper/TaskBook
<head>
    <title>59</title>
</head>

<?php 
/**
 * Using the Leng and Area functions from the tasks Proc56 and Proc58,
 * write a real-valued function Dist(xP, yP, xA, yA, xB, yB) that returns
 * the distance D(P, AB) between a point P and a line AB:
 * D(P, AB) = 2·SPAB/|AB|,
 * where SPAB is the area of the triangle PAB. Using this function, find the distance
 * between a point P and each of lines AB, AC, BC provided that coordinates of
 * points P, A, B, C are given.
 */
echo '<div style="display: none">';
include '58.php';
echo '</div>';
function Dist($xA, $yA, $xB, $yB, $xP, $yP)
{
    $result = Area($xA, $yA, $xB, $yB, $xP, $yP);
    $area = $result['triangle_area'];
    $a = $result['data_about_triangle']['length']['a'];
    $distance = 2 * $area / $a;
    return $distance;
}
$distance1 = Dist(2, 2, 2, 6, 6, 2);
$distance2 = Dist(0, 0, 0, 1, 1, 0);
echo $distance1 . '<br/>';
echo $distance2 . '<br/>';
示例#2
0
文件: 60.php 项目: bamper/TaskBook
function Alts($xA, $yA, $xB, $yB, $xC, $yC, &$hA, &$hB, &$hC)
{
    $hA = Dist($xA, $yA, $xB, $yB, $xC, $yC);
    $hB = Dist($xB, $yB, $xC, $yC, $xA, $yA);
    $hC = Dist($xA, $yA, $xC, $yC, $xB, $yB);
}
示例#3
0
文件: 68.php 项目: bamper/TaskBook
<?php

/**
 * Using the TPoint and TTriangle types and the Dist function (see Param64, Param65, Param67), write a procedure Alts
 * (T, h1, h2, h3) that evaluates the altitudes h1, h2, h3 drawn from the vertices T.A, T.B, T.C of a triangle T (T is
 * an input parameter of TTriangle type, h1, h2, h3 are output real-valued parameters). Using this procedure, evaluate
 * the altitudes of each of triangles ABC, ABD, ACD provided that the coordinates of points A, B, C, D are given.
 */
echo '<div style="display: none;">';
//
include 'helper.php';
include '67.php';
require_once '66.php';
require_once '64.php';
echo '</div >';
use TPoint\TPoint;
use ParamPhpJavaScrit\TTriangle\TTriangle;
$t1 = new TTriangle(2, 6, 7, 1, 2, 1);
$s = Area($t1);
$a = new TPoint($t1->getAx(), $t1->getAy());
$b = new TPoint($t1->getBx(), $t1->getBy());
$c = new TPoint($t1->getCx(), $t1->getCy());
$length1 = Leng2($a, $b);
$length2 = Leng2($a, $c);
$length3 = Leng2($b, $c);
echo Dist($s, $length1) . '<br/>';
echo Dist($s, $length2) . '<br/>';
echo Dist($s, $length3) . '<br/>';
示例#4
0
function Arrange($circle)
{
    global $debug;
    $nodcircle = array();
    $sortednod = array();
    $hubweight = array();
    $nbrnumber = array();
    if ($debug) {
        echo "<div class=\"textpad code txta\"><h3>ARRANGE</h3>\n";
    }
    foreach (array_keys($circle) as $node) {
        if (is_array($circle[$node]['alk'])) {
            $nbr = array_keys($circle[$node]['alk']);
            if (count($nbr) == 1) {
                # 1 neighbor
                $nodcircle[$node] = $nbr[0];
                if ($debug) {
                    echo "LEAF:{$node} -> {$nbr['0']}<br>";
                }
            } else {
                # Several neighbors
                if ($debug) {
                    echo "<p>HUB :{$node}<br>";
                }
                $nodcircle[$node] = $node;
                foreach ($nbr as $n) {
                    if (is_array($circle[$n]['alk'])) {
                        $hubweight[$node] += count(array_keys($circle[$n]['alk'])) > 1 ? 2 : 1;
                    }
                    if ($debug) {
                        echo "NBR :{$n}<br>";
                    }
                }
                if ($debug) {
                    echo "WGHT:{$hubweight[$node]}<br>";
                }
            }
        } else {
            $nodcircle[$node] = 0;
            if ($debug) {
                echo "UNL :{$node}<br>";
            }
        }
    }
    if ($debug) {
        echo "Align Hubs:\n";
    }
    arsort($hubweight);
    foreach ($hubweight as $curh => $cw) {
        if ($cw < 4) {
            if ($debug) {
                echo "HUB :{$curh} pos{$cw}<br>";
            }
            foreach ($hubweight as $nexth => $nw) {
                if (in_array($curh, array_keys($circle[$nexth]['alk'])) and $cw < $nw) {
                    if ($debug) {
                        echo "HLNK:{$curh} {$nexth} = {$nw}<br>";
                    }
                    if ($nodcircle[$curh] == $curh) {
                        # Only align hub, if not done before
                        $nodcircle[$curh] = $nexth . "0" . $curh;
                        # Hub will come in before the one it's aligned to
                        if ($debug) {
                            echo "HALI:{$nexth} to {$curh}<br>";
                        }
                    } else {
                        if ($debug) {
                            echo "HDON:{$nexth} is {$nodcircle[$nexth]}<br>";
                        }
                    }
                }
            }
        }
    }
    if ($debug) {
        echo "Arrange:\n";
    }
    asort($nodcircle);
    foreach ($nodcircle as $node => $nbr) {
        if (array_key_exists($node, $hubweight)) {
            $sortednod[$node] = $nbr . "2";
            # Hubs weight 2
            if ($debug) {
                echo "<p>HUB :{$nbr}<br>";
            }
        } else {
            $nbrnumber[$nbr]++;
            if ($nbrnumber[$nbr] % 2) {
                # Distribute LEAFs around HUBs
                $sortednod[$node] = $nodcircle[$nbr] . "1{$node}";
                if ($debug) {
                    echo "LEAF:{$node} = {$nbr} BELOW<br>";
                }
            } else {
                $sortednod[$node] = $nodcircle[$nbr] . "3{$node}";
                if ($debug) {
                    echo "LEAF:{$node} = {$nbr} ABOVE<br>";
                }
            }
        }
        if ($debug) {
            echo "SORT:{$sortednod[$node]}<br>";
        }
    }
    asort($sortednod);
    $sortedkeys = array_keys($sortednod);
    $csiz = count($sortedkeys);
    $iter = 0;
    if ($debug) {
        echo "<h3>Reposition nodes with 2 links crossing ({$csiz} total)</h3>\n";
    }
    do {
        $kpos = 0;
        $movednods = array();
        foreach ($sortedkeys as $k) {
            if ($debug) {
                echo "REPO{$iter}: {$k} ";
            }
            if (is_array($circle[$k]['alk'])) {
                # Any links?
                $nbr = array_keys($circle[$k]['alk']);
                $nnbr = count($nbr);
                if ($nnbr == 2 and !in_array($k, $movednods)) {
                    # We got 2 links?
                    $npos1 = array_search($nbr[0], $sortedkeys);
                    $npos2 = array_search($nbr[1], $sortedkeys);
                    $ndst1 = Dist($kpos, $npos1, $csiz);
                    $ndst2 = Dist($kpos, $npos2, $csiz);
                    $ktonb = $ndst1 + $ndst2;
                    $nbdst = Dist($npos1, $npos2, $csiz);
                    if ($debug) {
                        echo "pos{$kpos} connects to {$nbr['0']} on pos{$npos1} len{$ndst1} and {$nbr['1']} on pos{$npos2} len{$ndst2} (dist{$ktonb} vs nbrdist{$nbdst}) ";
                    }
                    # TODO add logic to detect crossing links or align and group nodes with 2 neighbors?
                    if ($ktonb > $nbdst + 1) {
                        # add 1 to avoid flapping
                        $mpos = $npos1 < $npos2 ? $npos1 : $npos2;
                        $nb1 = count(array_keys($circle[$nbr[0]]['alk']));
                        $nb2 = count(array_keys($circle[$nbr[1]]['alk']));
                        $mpos = $nb1 < $nb2 ? $npos1 : $npos2;
                        if ($debug) {
                            echo "{$nbr['0']} has {$nb1} links and {$nbr['1']} has {$nb2}, <span class=\"warn\">moving to {$mpos}</span>\n";
                        }
                        array_splice($sortedkeys, $kpos, 1);
                        # remove it
                        #if($nbdst > 0){
                        array_splice($sortedkeys, $mpos, 0, $k);
                        # and insert after nbr with less links
                        #}else{
                        #	array_splice($sortedkeys,$mpos,0,$k);		# or before if 0 crossing
                        #}
                        if ($debug) {
                            print_r($sortedkeys);
                        }
                        $movednods[] = $k;
                        break 1;
                    } else {
                        if ($debug) {
                            echo "stays\n";
                        }
                    }
                } else {
                    if ($debug) {
                        echo "has {$nnbr} neighbor" . ($nnbr == 1 ? '' : 's') . "\n";
                    }
                }
            } else {
                if ($debug) {
                    echo "no links\n";
                }
            }
            $kpos++;
        }
        if ($kpos == $csiz) {
            # Went through whole array lets end
            if ($debug) {
                echo "REPO:iter{$iter} reached pos{$kpos}, done!\n";
            }
            $iter = $csiz;
        }
        $iter++;
    } while ($iter < $csiz);
    if ($debug) {
        echo "</div>";
    }
    return $sortedkeys;
}
示例#5
0
文件: 67.php 项目: bamper/TaskBook
 * function Dist(P, A, B) that returns the distance D(P, AB) between a point P and a line AB:
 * D(P, AB) = 2·SPAB/|AB|,
 * where SPAB is the area of the triangle PAB (parameters P, A, B are input parameters of TPoint type). Using this
 * function, find the distance between a point P and each of lines AB, AC, BC provided that the coordinates of
 * points P, A, B, C are given.
 */
echo '<div style="display: none;">';
include 'helper.php';
//include '66.php';
require_once '66.php';
require_once '64.php';
echo '</div >';
use TPoint\TPoint;
use ParamPhpJavaScrit\TTriangle\TTriangle;
function Dist($S, $A)
{
    return 2 * $S / $A;
}
function Leng2($a, $b)
{
    $length = pow($a->getX() - $b->getX(), 2) + pow($a->getY() - $b->getY(), 2);
    $len = sqrt($length);
    return $len;
}
$t1 = new TTriangle(2, 6, 7, 1, 2, 1);
$s = Area($t1);
$a = new TPoint($t1->getAx(), $t1->getAy());
$b = new TPoint($t1->getBx(), $t1->getBy());
$length = Leng2($a, $b);
echo Dist($s, $length);