Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $host = 'localhost';
- $db = 'fembd';
- $user = 'root';
- $password = 'root';
- try {
- $pdo = new PDO("mysql:host=$host; dbname=$db;", $user, $password);
- $pdo->exec("set names utf8");
- $pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
- } catch (PDOException $e) {
- print "Не удалось подключиться к базе данных! ".$e->getMessage();
- exit();
- }
- $sql = "SELECT elements.id, nodes.x, nodes.y
- FROM elements, nodes
- WHERE nodes.id = elements.n1
- OR nodes.id = elements.n2
- OR nodes.id = elements.n3";
- try {
- $result = $pdo->prepare($sql);
- } catch (PDOException $e) {
- print "Не удаётся соединиться с базой данных".$e->getMessage();
- exit();
- }
- try {
- $result->execute();
- }
- catch (PDOException $e) {
- if ($e->getCode() == 42000) {
- print "Не достаточно прав доступа для выполнения запроса:(".$e->getMessage();
- }
- else {
- print "Не удалось выполнить обращение к базе данных: ".$e->getMessage();
- }
- exit();
- }
- $rownum = $result->rowcount();
- if($rownum == 0){
- print "Вернулся пустой результат!";
- exit();
- }
- $elem_num = $rownum / 3;
- for ($i=0; $i<$elem_num; $i++){
- $node1 = $result->fetch();
- $node2 = $result->fetch();
- $node3 = $result->fetch();
- $centerx[$i] = ($node1[1] + $node2[1] + $node3[1]) / 3;
- $centery[$i] = ($node1[2] + $node2[2] + $node3[2]) / 3;
- }
- $sector_num = 5;
- $quadrant = array(0, 0, 0, 0, 0);
- for ($i = 0; $i < $elem_num; ++$i) {
- $x = $centerx[$i];
- $y = $centery[$i];
- if ($x > 0 && $y > 0)
- ++$quadrant[1];
- else if ($x < 0 && $y > 0)
- ++$quadrant[2];
- else if ($x < 0 && $y < 0)
- ++$quadrant[3];
- else if ($x > 0 && $y < 0)
- ++$quadrant[4];
- else
- ++$quadrant[0];
- }
- header("Content-type: image/png");
- $img = @imagecreate(900, 600)
- or die("Cannot Initialize new GD image stream");
- $white = imagecolorallocate($img, 255, 255, 255);
- $black = imagecolorallocate($img, 0, 0, 0);
- $red = 0;
- $green = 255;
- $blue = 25;
- $x_d = 300;
- $y_d = 300;
- $x_w = 600;
- $y_w = 100;
- $width = 300;
- $height = 300;
- $start_angle = 0;
- $end_angle = 0;
- /* $quadrant[0]++;
- $quadrant[3]--; */
- imagesetpixel($img, 300, 300, $black);
- for ($i = 0; $i < $sector_num; $i++){
- $diagram[$i] = imagecolorallocate($img, $red, $green, $blue);
- $end_angle += 360*($quadrant[$i]/$elem_num);
- imagefilledarc($img, $x_d, $y_d, $width, $height, $start_angle, $end_angle, $diagram[$i], IMG_ARC_PIE);
- if ($i > 0)
- imagestring($img, 5, $x_w, $y_w, "Quadrant $i", $diagram[$i]);
- else
- imagestring($img, 5, $x_w, $y_w, "Exception (border)", $diagram[$i]);
- $start_angle = $end_angle;
- $red += 50;
- $green -= 50;
- $blue *= 2;
- $y_w += 98;
- }
- imagepng($img);
- imagedestroy($img);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement