{ $count = count($objects); $points = array(); $k = 0; //Find distances between all points for ($i = 0; $i < $count - 1; $i++) { for ($j = $i + 1; $j < $count; $j++) { $theta = $objects[$i]['y'] - $objects[$j]['y']; $dist = sin(deg2rad($objects[$i]['x'])) * sin(deg2rad($objects[$j]['x'])) + cos(deg2rad($objects[$i]['x'])) * cos(deg2rad($objects[$j]['x'])) * cos(deg2rad($theta)); $dist = acos($dist); $points[$k]['deg'] = rad2deg($dist); $points[$k]['name'] = $objects[$i]['name'] . '/' . $objects[$j]['name']; $k++; } } foreach ($points as $key => $row) { $deg[$key] = $row['deg']; $name[$key] = $row['name']; } //Sort distances based on ascendening array_multisort($deg, SORT_ASC, $name, SORT_ASC, $points); return explode('/', $points[0]['name']); } /********************* Example: *****************************/ $obj1 = array('name' => 'a', 'x' => 1, 'y' => 8); $obj2 = array('name' => 'b', 'x' => 1, 'y' => 2); $obj3 = array('name' => 'c', 'x' => 13, 'y' => 13); $obj4 = array('name' => 'd', 'x' => 15, 'y' => 14); $objects = array($obj1, $obj2, $obj3, $obj4); var_dump(findClosest($objects));
function sendAnswereIt($iter) { global $server; if (!file_exists("./answers/" . date('d-F') . ".json")) { return FALSE; } $html = browse("{$server}/AnswereIt.php"); if (strstr($html, "AICompletion.php")) { return FALSE; } $reshtml = str_get_html($html); if ($reshtml && is_object($reshtml) && isset($reshtml->nodes)) { $p = $reshtml->find('p[class="qutinsubfont"]'); $div = $reshtml->find('div[class="graphimg"]'); $grp = $div[0]->first_child()->src; $img = $p[0]->first_child()->src; } $a = time(); file_put_contents("tmp/{$a}.jpg", browse($img)); file_put_contents("grp/{$a}.png", browse($grp)); // graphic processing $str1 = processGraphic("grp/{$a}.png"); // distance processing $str2 = processDistance("tmp/{$a}.jpg"); // proceessing ends $str3 = $str1 + $str2; $ans = array(); $ans = json_decode(file_get_contents("./answers/" . date('d-F') . ".json"), true); $hayKeys = array_keys($ans); $str = findClosest($str3, $hayKeys); if (isset($ans["{$str}"])) { $answer = $ans["{$str}"]; $reshtml = str_get_html($html); $fieldList = array(); if ($reshtml && is_object($reshtml) && isset($reshtml->nodes)) { $input = $reshtml->find('input'); $loop = count($input); for ($i = 0; $i < $loop; $i++) { if (isset($input[$i]->attr["name"])) { $fieldList[$input[$i]->attr["name"]] = $input[$i]->attr["value"]; if ($input[$i]->attr["value"] == "Type your answer here") { if (isset($input[$i]->attr["style"])) { if ($input[$i]->attr["style"] == 'border:0 none;') { $fieldList[$input[$i]->attr["name"]] = $answer; } } } } } $fieldList["x"] = rand(10, 50); $fieldList["y"] = rand(1, 15); $html = browse("{$server}/AnswereIt.php", 1, $fieldList); if (strstr($html, "AnswereItGraph.php")) { echo "\nANSWEREIT.{$iter} answered {$str} with answer : {$answer}"; return TRUE; } } } else { echo "\nAnswer for this str not found : {$str} original str : {$str3}"; error_count(); return TRUE; } }
// array of numbers to check sort($numbers); // no shortest distance found, yet $shortest = -1; // loop through numbers to find the closest foreach ($numbers as $num) { // calculate the distance between the input num, // and the current num $lev = levenshtein($input, $num); // check for an exact match if ($lev == 0) { // closest num is this one (exact match) $closest = $num; $shortest = 0; // break out of the loop; we've found an exact match break; } // if this distance is less than the next found shortest // distance, OR if a next shortest num has not yet been found if ($lev <= $shortest || $shortest < 0) { // set the closest match, and shortest distance $closest = $num; $shortest = $lev; } } echo "Closest number is: " . $closest; } $hay = array_keys($hays); print_r($hay); echo findClosest($a, $hay); echo findClosest1($a, $hay);