function VennDiagram($names, $abc, $ab, $bc, $ca, $progress = 0) { $height = 300; $width = 400; $radius = 80; $shiftx = 55; $shifty = 45; $names = convert_accented_characters($names); $svg = '<div class="img-question text-center"> <svg width="' . $width . '" height="' . $height . '">'; // $svg .= '<rect width="'.$width.'" height="'.$height.'" fill="black" fill-opacity="0.2" />'; // Circles $svg .= DrawCircle($width / 2 - $shiftx, $height / 2 - $shifty, $radius); $svg .= DrawCircle($width / 2 + $shiftx, $height / 2 - $shifty, $radius); $svg .= DrawCircle($width / 2, $height / 2 + $shifty, $radius); // Labels $letters = array_map('str_split', $names); $svg .= DrawText($width / 2 - $shiftx * 2.3, $height / 2 - $shifty * 2.3, '$' . $letters[0][0] . '$', 13); $svg .= DrawText($width / 2 + $shiftx * 2.3, $height / 2 - $shifty * 2.3, '$' . $letters[1][0] . '$', 13); $svg .= DrawText($width / 2, $height / 2 + $shifty * 3.2, '$' . $letters[2][0] . '$', 13); $svg .= $progress >= 1 ? DrawText($width / 2, $height / 2 - 5, '$\\color{red}{' . $abc . '}$', 13) : ''; $svg .= $progress >= 2 ? DrawText($width / 2, $height / 2 - $shifty * 1.2, '$\\color{blue}{' . $ab . '}$', 13) : ''; $svg .= $progress >= 3 ? DrawText($width / 2 + $shiftx * 0.7, $height / 2 + 15, '$\\color{blue}{' . $bc . '}$', 13) : ''; $svg .= $progress >= 4 ? DrawText($width / 2 - $shiftx * 0.7, $height / 2 + 15, '$\\color{blue}{' . $ca . '}$', 13) : ''; $svg .= '</svg></div>'; return $svg; }
function PieChart($angle_deg = NULL) { $width = 400; $height = 260; $cx = $width / 2; $cy = $height / 2; $r = 100; $svg = '<div class="img-question text-center"> <svg width="' . $width . '" height="' . $height . '">'; // Show angle if ($angle_deg) { if ($angle_deg == 360) { $svg .= DrawCircle($cx, $cy, $r, $stroke = 'black', $strokewidth = 0, $fill = '#CCC'); } else { $svg .= DrawPieChart($cx, $cy, $r, 0, $angle_deg, $fill = '#CCC'); } list($x1, $y1) = polarToCartesian($cx, $cy, $r, 0); list($x2, $y2) = polarToCartesian($cx, $cy, $r, $angle_deg); $svg .= DrawLine($cx, $cy, $x1, $y1); $svg .= DrawLine($cx, $cy, $x2, $y2); } // Circle $svg .= DrawCircle($cx, $cy, $r); // Ticks for every 10° for ($i = 0; $i < 36; $i++) { $angle = $i * 10; list($x1, $y1) = polarToCartesian($cx, $cy, $r + 5, $angle); list($x2, $y2) = polarToCartesian($cx, $cy, $r - 5, $angle); $svg .= DrawLine($x1, $y1, $x2, $y2); } // Show hint degrees $svg .= DrawText($cx + $r + 10, $cy + 7, '0°', 15); $svg .= DrawText($cx + $r + 5, $cy - 13, '10°', 15); $svg .= DrawText($cx - 8, $cy - $r - 7, '90°', 15); $svg .= DrawText($cx - $r - 40, $cy + 7, '180°', 15); $svg .= DrawText($cx - 15, $cy + $r + 22, '270°', 15); $svg .= '</svg></div>'; return $svg; }
function DrawSineFunction($a, $b, $progress = 0) { $width = 600; $height = 300; $xunits_neg = -4; // Number of units on x-axis (unit length: pi/2) $xunits_pos = 4; $yunits_neg = -2; // Number of units on y-axis (unit length: 1/2) $yunits_pos = 2; $yunit_original = 1 / 2; $svg = '<div class="img-question text-center"> <svg width="' . $width . '" height="' . $height . '">'; // $svg .= '<rect width="'.$width.'" height="'.$height.'" fill="black" fill-opacity="0.2" />'; // Draw axes $padding1 = 10; // Padding left/bottom $padding2 = 30; // Padding right/top $xunit_length = ($width - $padding1 - $padding2) / ($xunits_pos - $xunits_neg); $yunit_length = ($height - $padding1 - $padding2) / ($yunits_pos - $yunits_neg); $originx = $padding1 + $xunit_length * abs($xunits_neg); $originy = $padding2 + $yunit_length * $yunits_pos; $svg .= DrawLine(0, $originy, $width, $originy); $svg .= DrawLine($originx, 0, $originx, $height); // Draw arrows $svg .= DrawLine($width, $originy, $width - 7, $originy - 7); $svg .= DrawLine($width, $originy, $width - 7, $originy + 7); $svg .= DrawLine($originx, 0, $originx - 7, 7); $svg .= DrawLine($originx, 0, $originx + 7, 7); // Draw units $xpos = $padding1; $xval = $xunits_neg * pi() / 2; while ($xpos <= $width - $padding2) { if ($xpos != $originx) { $svg .= DrawLine($xpos, $originy - 5, $xpos, $originy + 5); } $xtext = $this->Xvalue2Fraction($xval); $svg .= DrawText($xpos, $originy + 20, $xtext); $xpos += $xunit_length; $xval += pi() / 2; } $ypos = $padding2; $yval = $yunits_pos * $yunit_original; while ($ypos <= $height - $padding1) { if (round($ypos) != round($originy)) { $ytext = $this->Yvalue2Fraction($yval); $svg .= DrawLine($originx - 5, $ypos, $originx + 5, $ypos); $svg .= DrawText($originx + 20, $ypos - 5, $ytext); } $ypos += $yunit_length; $yval -= $yunit_original; } // Draw guides $svg .= $this->DrawGuides(1, $yunit_length, $yunit_original, $originy, $width); $svg .= $this->DrawGuides(-1, $yunit_length, $yunit_original, $originy, $width); // Draw function if ($progress == 0) { for ($i = 0; $i < $width; $i++) { $xval1 = ($i - $originx) / $xunit_length * pi() / 2; $xval2 = ($i + 1 - $originx) / $xunit_length * pi() / 2; $yval1 = -sin($xval1) * $yunit_length / $yunit_original + $originy; $yval2 = -sin($xval2) * $yunit_length / $yunit_original + $originy; $svg .= DrawLine($i, $yval1, $i + 1, $yval2, 'red', 2); } } elseif ($progress == 1) { for ($i = 0; $i < $width; $i++) { $xval1 = ($i - $originx) / $xunit_length * pi() / 2; $xval2 = ($i + 1 - $originx) / $xunit_length * pi() / 2; $yval1 = -sin($xval1) * $yunit_length / $yunit_original + $originy; $yval2 = -sin($xval2) * $yunit_length / $yunit_original + $originy; $svg .= $i % 3 == 0 ? DrawLine($i, $yval1, $i + 1, $yval2, 'blue', 1) : ''; } for ($i = 0; $i < $width; $i++) { $xval1 = ($i - $originx) / $xunit_length * pi() / 2; $xval2 = ($i + 1 - $originx) / $xunit_length * pi() / 2; $yval1 = -sin($a * $xval1) * $yunit_length / $yunit_original + $originy; $yval2 = -sin($a * $xval2) * $yunit_length / $yunit_original + $originy; $svg .= DrawLine($i, $yval1, $i + 1, $yval2, 'red', 2); } } else { for ($i = 0; $i < $width; $i++) { $xval1 = ($i - $originx) / $xunit_length * pi() / 2; $xval2 = ($i + 1 - $originx) / $xunit_length * pi() / 2; $yval1 = -sin($a * $xval1) * $yunit_length / $yunit_original + $originy; $yval2 = -sin($a * $xval2) * $yunit_length / $yunit_original + $originy; $svg .= DrawLine($i, $yval1, $i + 1, $yval2, 'blue', 2); } for ($i = 0; $i < $width; $i++) { $xval1 = ($i - $originx) / $xunit_length * pi() / 2; $yval1 = -sin($a * $xval1) * $yunit_length / $yunit_original + $originy; $yval2 = round(-$yunit_length / $yunit_original) + $originy; if (abs(sin($a * $xval1) - $b) < 1.0E-5) { $svg .= DrawCircle($i, $yval1, 5, 'red', 1, 'red'); if ($progress == 3 && $b != 0) { $svg .= '<g fill="none" stroke="red" stroke-width="2"><path stroke-dasharray="5,5" d="M' . $i . ' ' . $originy . ' l0 ' . round(-$yunit_length / $yunit_original) . '" /></g>'; // for some mysterious reasons this is not working... (produces the same, though): // $svg .= DrawPath($i, $originy, $i, $yval2, $color1='red', $width=2, $color2='none', $dasharray1=5, $dasharray2=5); } } } } $svg .= '</svg></div>'; return $svg; }
function Graph($graph, $step = NULL) { $width = 400; $height = 300; $centerX = $width / 2; $centerY = $height / 2; $radius = 100; $svg = '<div class="img-question text-center"> <svg width="' . $width . '" height="' . $height . '">'; // $svg .= '<rect width="'.$width.'" height="'.$height.'" fill="black" fill-opacity="0.2" />'; $size = count($graph); $angle0 = $size == 4 ? 45 : 90; // Edges for ($node1 = 0; $node1 < $size; $node1++) { if ($node1 < $step) { $color = $node1 == $step - 1 ? '#B155CF' : 'black'; foreach ($graph[$node1] as $node2) { if (!is_array($node2)) { $angle1 = $angle0 + $node1 * 360 / $size; $angle2 = $angle0 + $node2 * 360 / $size; list($x1, $y1) = polarToCartesian($centerX, $centerY, $radius, $angle1); list($x2, $y2) = polarToCartesian($centerX, $centerY, $radius, $angle2); $svg .= DrawLine($x1, $y1, $x2, $y2, $color, 2); } } } } // Nodes for ($node = 0; $node < $size; $node++) { $angle = $angle0 + $node * 360 / $size; if ($node < $step - 1) { $color = '#A1D490'; } elseif ($node == $step - 1) { $color = '#B155CF'; } else { $color = 'white'; } list($x, $y) = polarToCartesian($centerX, $centerY, $radius, $angle); $svg .= DrawCircle($x, $y, 10, 'black', $width = 2, $color); } $svg .= '</svg></div>'; return $svg; }
function Graph($min = NULL, $sec = NULL, $point = NULL) { $width = 400; $height = 350; $paddingX = 30; $paddingY = 80; $lines = 11; $unitX = ($width - 30 - $paddingX) / ($lines + 1); $unitY = ($height - 30 - $paddingY) / $lines; $secs = ['33', '00', '66']; $show = [323, 320, 315, 313]; $svg = '<div class="img-question text-center"> <svg width="' . $width . '" height="' . $height . '">'; // X axis $svg .= DrawLine(0, $height - $paddingY, $width, $height - $paddingY); $svg .= DrawLine($width, $height - $paddingY, $width - 5, $height - $paddingY + 5); $svg .= DrawLine($width, $height - $paddingY, $width - 5, $height - $paddingY - 5); $svg .= DrawText($width - 20, $height - $paddingY + 15, 'idő'); // Y axis $svg .= DrawLine($paddingX, 0, $paddingX, $height - 60); $svg .= DrawLine($paddingX, 0, $paddingX - 5, 5); $svg .= DrawLine($paddingX, 0, $paddingX + 5, 5); $svg .= DrawText($paddingX + 10, 15, 'pontszám'); for ($i = 0; $i < $lines + 1; $i++) { $x = $paddingX + ($lines - $i + 1) * $unitX; $y = $height - $paddingY - min($lines, $i + 1) * $unitY; $svg .= DrawPath($paddingX - 5, $y, $x, $y, 'black', 0.5, 'none', 5, 5); $svg .= DrawPath($x, $height - $paddingY + 5, $x, $y, 'black', 0.5, 'none', 5, 5); if ($i < $lines) { $svg .= DrawLine($x - $unitX, $y, $x, $y, 'black', 2); $svg .= DrawCircle($x - $unitX, $y, 3, 'black', 1, 'black'); $svg .= DrawCircle($x, $y, 3, 'black', 1, 'white'); } if (in_array(313 + $i, $show)) { $svg .= DrawText($paddingX - 25, $y + 4, 313 + $i); } elseif ($point !== NULL && $i < $lines) { $svg .= DrawText($paddingX - 25, $y + 4, 313 + $i, 10, 'blue'); } $text = '2 perc ' . strval(9 - floor(($i + 1) / 3)) . ',' . $secs[$i % 3] . ' mp'; $svg .= DrawText($x + 3, $height - $paddingY + 77, $text, 10, 'black', -90); } // Draw time if ($min !== NULL && $sec !== NULL) { $time = $min * 100 + $sec; $x1 = $paddingX + $unitX * ($lines + 1) * (400 - (933 - $time)) / 400; $y1 = $height - $paddingY; $svg .= DrawCircle($x1, $y1, 3, 'blue', 1, 'blue'); } if ($point !== NULL) { $x2 = $paddingX; $y2 = $height - $paddingY - ($point - 312) * $unitY; $svg .= DrawPath($x1, $y1, $x1, $y2, 'blue', 2, 'none', 5, 5); $svg .= DrawPath($x1, $y2, $x2, $y2, 'blue', 2, 'none', 5, 5); $svg .= DrawCircle($x2, $y2, 3, 'blue', 1, 'blue'); } $svg .= '</svg></div>'; return $svg; }
function AbsFunctionGraph($x1, $x2, $a, $b, $progress = 0) { $bottom = min(abs($x1 + $a), abs($x2 + $a), 0) + ($b < 0 ? $b : 0); $top = max(abs($x1 + $a), abs($x2 + $a)) + ($b > 0 ? $b : 0); $left = $x1; $right = $x2; $linesx = $top - $bottom + 3; $linesy = $right - $left + 3; $unit = 400 / $linesy < 40 ? 40 : 400 / $linesy; $width = $unit * $linesy; $height = $unit * $linesx; $svg = '<div class="img-question text-center"> <svg width="' . $width . '" height="' . $height . '">'; // Origo $Ox = (1.5 - $left) * $unit; $Oy = $height - (1.5 - $bottom) * $unit; if ($unit < 45) { $fontsize = 10; } elseif ($unit < 50) { $fontsize = 11; } else { $fontsize = 12; } // Guides for ($i = 0; $i < $linesy; $i++) { $x = (0.5 + $i) * $unit; $svg .= DrawLine($x, 0, $x, $height, '#F2F2F2'); $num = $i + $left - 1; if ($num == 0) { $svg .= DrawText($Ox + $unit / 3, $Oy + $unit / 2, '$0$', $fontsize); } else { $svg .= DrawLine($x, $Oy - 5, $x, $Oy + 5, 'black', 2); $svg .= DrawText($x, $Oy + $unit / 2, '$' . $num . '$', $fontsize); } } for ($i = 0; $i < $linesx; $i++) { $y = (0.5 + $i) * $unit; $svg .= DrawLine(0, $y, $width, $y, '#F2F2F2'); $num = $top - $i + 1; if ($num != 0) { $svg .= DrawLine($Ox + 5, $y, $Ox - 5, $y, 'black', 2); if ($num <= 9 && $num > 0) { $shift = $unit / 3; } elseif ($num > -10 || $num > 9) { $shift = $unit / 2; } else { $shift = $unit / 1.7; } $svg .= DrawText($Ox - $shift, $y + $unit / 5, '$' . $num . '$', $fontsize); } } // Axes $svg .= DrawVector($Ox, $height, $Ox, 0, 'black', 10, 2); $svg .= DrawVector(0, $Oy, $width, $Oy, 'black', 10, 2); $svg .= DrawText($width - $unit / 2, $Oy - $unit / 3, '$x$', $fontsize * 1.5); $svg .= DrawText($Ox + $unit / 2, $unit / 3, '$y$', $fontsize * 1.5); // Coordinates for |x| list($X1A, $Y1A) = $this->Coordinates2($x1, abs($x1), $height, $left, $bottom, $unit); list($X2A, $Y2A) = $this->Coordinates2(0, 0, $height, $left, $bottom, $unit); list($X3A, $Y3A) = $this->Coordinates2($x2, abs($x2), $height, $left, $bottom, $unit); // Coordinates for |x+a| list($X1B, $Y1B) = $this->Coordinates2($x1, abs($x1 + $a), $height, $left, $bottom, $unit); list($X2B, $Y2B) = $this->Coordinates2(-$a, 0, $height, $left, $bottom, $unit); list($X3B, $Y3B) = $this->Coordinates2($x2, abs($x2 + $a), $height, $left, $bottom, $unit); // Coordinates for |x+a|+b list($X1C, $Y1C) = $this->Coordinates2($x1, abs($x1 + $a) + $b, $height, $left, $bottom, $unit); list($X2C, $Y2C) = $this->Coordinates2(-$a, $b, $height, $left, $bottom, $unit); list($X3C, $Y3C) = $this->Coordinates2($x2, abs($x2 + $a) + $b, $height, $left, $bottom, $unit); if ($progress == 0) { // End points for |x| $svg .= DrawCircle($X1A, $Y1A, 3, 'red', 1, 'red'); $svg .= DrawCircle($X3A, $Y3A, 3, 'red', 1, 'red'); // Lines for |x| $svg .= DrawLine($X1A, $Y1A, $X2A, $Y2A, 'red', 2); $svg .= DrawLine($X2A, $Y2A, $X3A, $Y3A, 'red', 2); } elseif ($progress == 1) { // End points for |x| $svg .= DrawCircle($X1A, $Y1A, 3, 'blue', 1, 'blue'); $svg .= DrawCircle($X3A, $Y3A, 3, 'blue', 1, 'blue'); // Lines for |x| $svg .= DrawPath($X1A, $Y1A, $X2A, $Y2A, $color1 = 'blue', $width = 2, $color2 = 'none', $dasharray2asharray1 = 5, $dasharray2 = 5); $svg .= DrawPath($X2A, $Y2A, $X3A, $Y3A, $color1 = 'blue', $width = 2, $color2 = 'none', $dasharray2asharray1 = 5, $dasharray2 = 5); // Vector from old to new $svg .= DrawVector($X2A, $Y2A, $X2B, $Y2B, 'green', 10, 2, 30); // End points for |x+a| $svg .= DrawCircle($X1B, $Y1B, 3, 'red', 1, 'red'); $svg .= DrawCircle($X3B, $Y3B, 3, 'red', 1, 'red'); // Lines for |x+a| $svg .= DrawLine($X1B, $Y1B, $X2B, $Y2B, 'red', 2); $svg .= DrawLine($X2B, $Y2B, $X3B, $Y3B, 'red', 2); } elseif ($progress == 2) { // End points for |x+a| $svg .= DrawCircle($X1B, $Y1B, 3, 'blue', 1, 'blue'); $svg .= DrawCircle($X3B, $Y3B, 3, 'blue', 1, 'blue'); // Lines for |x+a| $svg .= DrawPath($X1B, $Y1B, $X2B, $Y2B, $color1 = 'blue', $width = 2, $color2 = 'none', $dasharray2asharray1 = 5, $dasharray2 = 5); $svg .= DrawPath($X2B, $Y2B, $X3B, $Y3B, $color1 = 'blue', $width = 2, $color2 = 'none', $dasharray2asharray1 = 5, $dasharray2 = 5); // Vector from old to new $svg .= DrawVector($X2B, $Y2B, $X2C, $Y2C, 'green', 10, 2, 30); // End points for |x+a|+b $svg .= DrawCircle($X1C, $Y1C, 3, 'red', 1, 'red'); $svg .= DrawCircle($X3C, $Y3C, 3, 'red', 1, 'red'); // Lines for |x+a|+b $svg .= DrawLine($X1C, $Y1C, $X2C, $Y2C, 'red', 2); $svg .= DrawLine($X2C, $Y2C, $X3C, $Y3C, 'red', 2); } else { // Lines between end points and y axis if (abs($x1 + $a) > abs($x2 + $a)) { if (abs($x1 + $a) + $b != 0) { list($X1D, $Y1D) = $this->Coordinates2(0, abs($x1 + $a) + $b, $height, $left, $bottom, $unit); $svg .= DrawPath($X1C, $Y1C, $X1D, $Y1D, $color1 = 'green', $width = 2, $color2 = 'none', $dasharray2asharray1 = 5, $dasharray2 = 5); } } else { if (abs($x2 + $a) + $b != 0) { list($X3D, $Y3D) = $this->Coordinates2(0, abs($x2 + $a) + $b, $height, $left, $bottom, $unit); $svg .= DrawPath($X3C, $Y3C, $X3D, $Y3D, $color1 = 'green', $width = 2, $color2 = 'none', $dasharray2asharray1 = 5, $dasharray2 = 5); } } if ($b != 0) { list($X2D, $Y2D) = $this->Coordinates2(0, $b, $height, $left, $bottom, $unit); $svg .= DrawPath($X2C, $Y2C, $X2D, $Y2D, $color1 = 'green', $width = 2, $color2 = 'none', $dasharray2asharray1 = 5, $dasharray2 = 5); } // End points for |x+a|+b $svg .= DrawCircle($X1C, $Y1C, 3, 'red', 1, 'red'); $svg .= DrawCircle($X3C, $Y3C, 3, 'red', 1, 'red'); // Lines for |x+a|+b $svg .= DrawLine($X1C, $Y1C, $X2C, $Y2C, 'red', 2); $svg .= DrawLine($X2C, $Y2C, $X3C, $Y3C, 'red', 2); } $svg .= '</svg></div>'; return $svg; }
function Rhombus($Cx, $Cy, $vx, $vy, $mult, $progress = 0) { // Calculate coordinates (nodes) list($Tx, $Ty, $Rx, $Ry, $Px, $Py, $Sx, $Sy) = $this->Coordinates1($Cx, $Cy, $vx, $vy, $mult); $bottom = min($Py, $Ry, $Sy, $Ty, 0); $top = max($Py, $Ry, $Sy, $Ty, 0); $left = min($Px, $Rx, $Sx, $Tx, 0); $right = max($Px, $Rx, $Sx, $Tx, 0); // print_r('bottom: '.$bottom.'<br />'); // print_r('top: '.$top.'<br />'); // print_r('left: '.$left.'<br />'); // print_r('right: '.$right.'<br />'); $linesx = $top - $bottom + 3; $linesy = $right - $left + 3; $unit = 400 / $linesy < 40 ? 40 : 400 / $linesy; $width = $unit * $linesy; $height = $unit * $linesx; // Calculate coordinates (labels) list($C2x, $C2y) = $this->Coordinates3($Rx, $Ry, $Tx, $Ty, 'center'); list($T2x, $T2y) = $this->Coordinates3($Cx, $Cy, $Tx, $Ty); list($R2x, $R2y) = $this->Coordinates3($Cx, $Cy, $Rx, $Ry); list($S2x, $S2y) = $this->Coordinates3($Cx, $Cy, $Sx, $Sy); list($P2x, $P2y) = $this->Coordinates3($Cx, $Cy, $Px, $Py); // Calculate coordinates on canvas (nodes) list($C3x, $C3y) = $this->Coordinates2($Cx, $Cy, $height, $left, $bottom, $unit); list($T3x, $T3y) = $this->Coordinates2($Tx, $Ty, $height, $left, $bottom, $unit); list($R3x, $R3y) = $this->Coordinates2($Rx, $Ry, $height, $left, $bottom, $unit); list($S3x, $S3y) = $this->Coordinates2($Sx, $Sy, $height, $left, $bottom, $unit); list($P3x, $P3y) = $this->Coordinates2($Px, $Py, $height, $left, $bottom, $unit); // Calculate coordinates on canvas (labels) list($C4x, $C4y) = $this->Coordinates2($C2x, $C2y, $height, $left, $bottom, $unit); list($T4x, $T4y) = $this->Coordinates2($T2x, $T2y, $height, $left, $bottom, $unit); list($R4x, $R4y) = $this->Coordinates2($R2x, $R2y, $height, $left, $bottom, $unit); list($S4x, $S4y) = $this->Coordinates2($S2x, $S2y, $height, $left, $bottom, $unit); list($P4x, $P4y) = $this->Coordinates2($P2x, $P2y, $height, $left, $bottom, $unit); // print_r('T('.$Tx.';'.$Ty.')<br />'); // print_r('R('.$Rx.';'.$Ry.')<br />'); // print_r('S('.$Sx.';'.$Sy.')<br />'); // print_r('P('.$Px.';'.$Py.')<br />'); $svg = '<div class="img-question text-center"> <svg width="' . $width . '" height="' . $height . '">'; // Origo $Ox = (1.5 - $left) * $unit; $Oy = $height - (1.5 - $bottom) * $unit; if ($unit < 45) { $fontsize = 10; } elseif ($unit < 50) { $fontsize = 11; } else { $fontsize = 12; } // Guides for ($i = 0; $i < $linesy; $i++) { $x = (0.5 + $i) * $unit; $svg .= DrawLine($x, 0, $x, $height, '#F2F2F2'); $num = $i + $left - 1; if ($num == 0) { $svg .= DrawText($Ox + $unit / 3, $Oy + $unit / 2, '$0$', $fontsize); } else { $svg .= DrawLine($x, $Oy - 5, $x, $Oy + 5, 'black', 2); $svg .= DrawText($x, $Oy + $unit / 2, '$' . $num . '$', $fontsize); } } for ($i = 0; $i < $linesx; $i++) { $y = (0.5 + $i) * $unit; $svg .= DrawLine(0, $y, $width, $y, '#F2F2F2'); $num = $top - $i + 1; if ($num != 0) { $svg .= DrawLine($Ox + 5, $y, $Ox - 5, $y, 'black', 2); if ($num <= 9 && $num > 0) { $shift = $unit / 3; } elseif ($num > -10 || $num > 9) { $shift = $unit / 2; } else { $shift = $unit / 1.7; } $svg .= DrawText($Ox - $shift, $y + $unit / 5, '$' . $num . '$', $fontsize); } } // Axes $svg .= DrawVector($Ox, $height, $Ox, 0, 'black', 10, 2); $svg .= DrawVector(0, $Oy, $width, $Oy, 'black', 10, 2); $svg .= DrawText($width - $unit / 2, $Oy - $unit / 3, '$x$', $fontsize * 1.5); $svg .= DrawText($Ox + $unit / 2, $unit / 3, '$y$', $fontsize * 1.5); if ($progress == 0) { if ($Cy != 0) { $svg .= DrawPath($Ox, $C3y, $C3x, $C3y, $color1 = 'red', $width = 2, $color2 = 'none', $dasharray2asharray1 = 5, $dasharray2 = 5); } if ($Cx != 0) { $svg .= DrawPath($C3x, $C3y, $C3x, $Oy, $color1 = 'red', $width = 2, $color2 = 'none', $dasharray1 = 5, $dasharray2 = 5); } // Draw point K $svg .= DrawCircle($C3x, $C3y, 3, 'red', 1, 'red'); $svg .= DrawText($C4x, $C4y, '$K$', 15); } elseif ($progress == 1) { if ($Ty != 0) { $svg .= DrawPath($Ox, $T3y, $T3x, $T3y, $color1 = 'red', $width = 2, $color2 = 'none', $dasharray1 = 5, $dasharray2 = 5); } if ($Tx != 0) { $svg .= DrawPath($T3x, $T3y, $T3x, $Oy, $color1 = 'red', $width = 2, $color2 = 'none', $dasharray1 = 5, $dasharray2 = 5); } // Draw point T $svg .= DrawCircle($T3x, $T3y, 3, 'red', 1, 'red'); $svg .= DrawText($T4x, $T4y, '$T$', 15); // Draw point K $svg .= DrawCircle($C3x, $C3y, 3, 'blue', 1, 'blue'); $svg .= DrawText($C4x, $C4y, '$K$', 15); } elseif ($progress == 2) { $svg .= DrawVector($C3x, $C3y, $T3x, $T3y, 'blue', 7, 2, 30); $svg .= DrawVector($C3x, $C3y, $R3x, $R3y, 'red', 7, 2, 30); // Draw point R $svg .= DrawText($R4x, $R4y, '$R$', 15); // Draw point K $svg .= DrawText($C4x, $C4y, '$K$', 15); // Draw point T $svg .= DrawText($T4x, $T4y, '$T$', 15); } elseif ($progress == 3) { $svg .= DrawVector($C3x, $C3y, $T3x, $T3y, 'blue', 7, 2, 30); $svg .= DrawVector($C3x, $C3y, $R3x, $R3y, 'blue', 7, 2, 30); $svg .= DrawVector($C3x, $C3y, $P3x, $P3y, 'red', 7, 2, 30); $svg .= DrawVector($C3x, $C3y, $S3x, $S3y, 'red', 7, 2, 30); // Draw point K $svg .= DrawText($C4x, $C4y, '$K$', 15); // Draw point T $svg .= DrawText($T4x, $T4y, '$T$', 15); // Draw point R $svg .= DrawText($R4x, $R4y, '$R$', 15); // Draw point S $svg .= DrawText($S4x, $S4y, '$S$', 15); // Draw point P $svg .= DrawText($P4x, $P4y, '$P$', 15); } else { $svg .= DrawLine($P3x, $P3y, $T3x, $T3y, 'red', 2); $svg .= DrawLine($S3x, $S3y, $R3x, $R3y, 'red', 2); $svg .= DrawLine($T3x, $T3y, $S3x, $S3y, 'red', 2); $svg .= DrawLine($R3x, $R3y, $P3x, $P3y, 'red', 2); if ($Py != $Ry && $Py != $Ty && $Py != 0) { $svg .= DrawPath($Ox, $P3y, $P3x, $P3y, $color1 = 'blue', $width = 2, $color2 = 'none', $dasharray1 = 5, $dasharray2 = 5); } if ($Px != $Rx && $Px != $Tx && $Px != 0) { $svg .= DrawPath($P3x, $P3y, $P3x, $Oy, $color1 = 'blue', $width = 2, $color2 = 'none', $dasharray1 = 5, $dasharray2 = 5); } if ($Ry != $Py && $Ry != $Sy && $Ry != 0) { $svg .= DrawPath($Ox, $R3y, $R3x, $R3y, $color1 = 'blue', $width = 2, $color2 = 'none', $dasharray1 = 5, $dasharray2 = 5); } if ($Rx != $Px && $Rx != $Sx && $Rx != 0) { $svg .= DrawPath($R3x, $R3y, $R3x, $Oy, $color1 = 'blue', $width = 2, $color2 = 'none', $dasharray1 = 5, $dasharray2 = 5); } if ($Sy != $Ry && $Sy != $Ty && $Sy != 0) { $svg .= DrawPath($Ox, $S3y, $S3x, $S3y, $color1 = 'blue', $width = 2, $color2 = 'none', $dasharray1 = 5, $dasharray2 = 5); } if ($Sx != $Rx && $Sx != $Tx && $Sx != 0) { $svg .= DrawPath($S3x, $S3y, $S3x, $Oy, $color1 = 'blue', $width = 2, $color2 = 'none', $dasharray1 = 5, $dasharray2 = 5); } if ($Ty != $Py && $Ty != $Sy && $Ty != 0) { $svg .= DrawPath($Ox, $T3y, $T3x, $T3y, $color1 = 'blue', $width = 2, $color2 = 'none', $dasharray1 = 5, $dasharray2 = 5); } if ($Tx != $Px && $Tx != $Sx && $Tx != 0) { $svg .= DrawPath($T3x, $T3y, $T3x, $Oy, $color1 = 'blue', $width = 2, $color2 = 'none', $dasharray1 = 5, $dasharray2 = 5); } // Draw point T $svg .= DrawText($T4x, $T4y, '$T$', 15); // Draw point R $svg .= DrawText($R4x, $R4y, '$R$', 15); // Draw point S $svg .= DrawText($S4x, $S4y, '$S$', 15); // Draw point P $svg .= DrawText($P4x, $P4y, '$P$', 15); } $svg .= '</svg></div>'; return $svg; }
function Graph($a, $b, $c, $x1, $x2, $relation, $progress = 0) { $padding1 = 20; // Padding left/bottom $padding2 = 20; // Padding right/top $x_avg = ($x1 + $x2) / 2; $x_min = min(0, floor($x_avg) - 2, $x1 - 1); $x_max = max(0, ceil($x_avg) + 2, $x2 + 1); $y_avg = $this->Equation_val($a, $b, $c, $x_avg); $diff = abs($y_avg) > 4 ? 1 : 2; $y_max = $this->Equation_val($a, $b, $c, $x1 - $diff); $y_min = $a > 0 ? floor($y_avg) : $y_max; $y_max = $a > 0 ? $y_max : ceil($y_avg); $height = 300; $width = 400; $unitx = ($width - $padding1 - $padding2) / ($x_max - $x_min); $unity = ($height - $padding1 - $padding2) / ($y_max - $y_min); $unit = max(30, min($unitx, $unity)); $width = $unit * ($x_max - $x_min) + $padding1 + $padding2; $height = $unit * ($y_max - $y_min) + $padding1 + $padding2; $originx = $padding1 + $unit * abs($x_min); $originy = $padding2 + $unit * $y_max; $svg = '<div class="img-question text-center"> <svg width="' . $width . '" height="' . $height . '">'; // $svg .= '<rect width="'.$width.'" height="'.$height.'" fill="black" fill-opacity="0.2" />'; // Draw guides $xpos = $padding1; while ($xpos <= $width - $padding2) { if ($xpos != $originx) { $svg .= DrawLine($xpos, 0, $xpos, $height, '#F2F2F2'); } $xpos += $unit; } $ypos = $padding2; while ($ypos <= $height - $padding1) { if ($ypos != $originy) { $svg .= DrawLine(0, $ypos, $width, $ypos, '#F2F2F2'); } $ypos += $unit; } // Draw axes $svg .= DrawLine(0, $originy, $width, $originy); $svg .= DrawLine($originx, 0, $originx, $height); // Draw arrows $svg .= DrawLine($width, $originy, $width - 7, $originy - 7); $svg .= DrawLine($width, $originy, $width - 7, $originy + 7); $svg .= DrawLine($originx, 0, $originx - 7, 7); $svg .= DrawLine($originx, 0, $originx + 7, 7); // Draw units $xpos = $padding1; $xval = $x_min; while ($xpos <= $width - $padding2) { if ($xpos != $originx) { $svg .= DrawLine($xpos, $originy - 5, $xpos, $originy + 5); $svg .= DrawText($xpos + 5, $originy + 17, $xval); } $xpos += $unit; $xval += 1; } $ypos = $padding2; $yval = $y_max; while ($ypos <= $height - $padding1) { if ($ypos != $originy) { $svg .= DrawLine($originx - 5, $ypos, $originx + 5, $ypos); } $svg .= DrawText($originx + 10, $ypos - 5, $yval); $ypos += $unit; $yval -= 1; } // Draw function for ($i = 0; $i < $width; $i++) { $xval1 = ($i - $originx) / $unit; $xval2 = ($i + 1 - $originx) / $unit; $yval1 = -$this->Equation_val($a, $b, $c, $xval1) * $unit + $originy; $yval2 = -$this->Equation_val($a, $b, $c, $xval2) * $unit + $originy; $color = $progress == 0 ? 'red' : 'blue'; $svg .= DrawLine($i, $yval1, $i + 1, $yval2, $color, 2); } if ($progress == 1) { // Calculate abscissas for zero points $x_zero1 = $x1 * $unit + $originx; $x_zero2 = $x2 * $unit + $originx; // Draw solution (segment) if ($a > 0 && in_array($relation, ['\\leq', '<']) || $a < 0 && in_array($relation, ['\\geq', '>'])) { $svg .= DrawLine($x_zero1, $originy, $x_zero2, $originy, $color = 'red', $width = 2); } else { $svg .= DrawLine($x_zero2, $originy, $width, $originy, $color = 'red', $width = 2); $svg .= DrawLine(0, $originy, $x_zero1, $originy, $color = 'red', $width = 2); } // Draw solution (end points) if ($x1 != $x2) { $color = in_array($relation, ['<', '>']) ? 'white' : 'red'; $svg .= DrawCircle($x_zero1, $originy, 4, 'red', 2, $color); $svg .= DrawCircle($x_zero2, $originy, 4, 'red', 2, $color); } } $svg .= '</svg></div>'; return $svg; }
function Graph($A, $B, $axis, $progress = 0) { list($C, $r, $P1, $P2) = $this->Solution($A, $B, $axis); $bottom = min(0, $C[1] - $r); $top = max(0, $C[1] + $r); $left = min(0, $C[0] - $r); $right = max(0, $C[0] + $r); // print_r('bottom: '.$bottom.'<br />'); // print_r('top: '.$top.'<br />'); // print_r('left: '.$left.'<br />'); // print_r('right: '.$right.'<br />'); $linesx = $top - $bottom + 3; $linesy = $right - $left + 3; $unit = 500 / $linesy; $width = $unit * $linesy; $height = $unit * $linesx; // print_r('width: '.$width.', height: '.$height.'<br />'); $svg = '<div class="img-question text-center"> <svg width="' . $width . '" height="' . $height . '">'; // Origo $OO = $this->CanvasCoordinates([0, 0], $height, $left, $bottom, $unit); if ($unit < 35) { $fontsize_axis = 0; // Hide text if coordinate system is too big $axis_width = 1; } elseif ($unit < 45) { $fontsize_axis = 10; $axis_width = 2; } elseif ($unit < 50) { $fontsize_axis = 11; $axis_width = 2; } else { $fontsize_axis = 12; $axis_width = 2; } $fontsize_label = round($fontsize_axis * 1.2); // Guides for ($i = 0; $i < $linesy; $i++) { $x = (0.5 + $i) * $unit; $svg .= DrawLine($x, 0, $x, $height, '#F2F2F2'); $num = $i + $left - 1; if ($num == 0) { $svg .= DrawText($OO[0] + $unit / 3, $OO[1] + $unit / 2, '$0$', $fontsize_axis); } else { $svg .= DrawLine($x, $OO[1] - 5, $x, $OO[1] + 5, 'black', $axis_width); $svg .= DrawText($x, $OO[1] + $unit / 2, '$' . $num . '$', $fontsize_axis); } } for ($i = 0; $i < $linesx; $i++) { $y = (0.5 + $i) * $unit; $svg .= DrawLine(0, $y, $width, $y, '#F2F2F2'); $num = $top - $i + 1; if ($num != 0) { $svg .= DrawLine($OO[0] + 5, $y, $OO[0] - 5, $y, 'black', $axis_width); if ($num <= 9 && $num > 0) { $shift = $unit / 3; } elseif ($num > -10 || $num > 9) { $shift = $unit / 2; } else { $shift = $unit / 1.7; } $svg .= DrawText($OO[0] - $shift, $y + $unit / 5, '$' . $num . '$', $fontsize_axis); } } // Axes $svg .= DrawVector($OO[0], $height, $OO[0], 0, 'black', 10, $axis_width); $svg .= DrawVector(0, $OO[1], $width, $OO[1], 'black', 10, $axis_width); $svg .= DrawText($width - $unit / 2, $OO[1] - $unit / 3, '$x$', $fontsize_label); $svg .= DrawText($OO[0] + $unit / 2, $unit / 3, '$y$', $fontsize_label); // Calculate canvas coordinates for nodes $CC = $this->CanvasCoordinates($C, $height, $left, $bottom, $unit); $AA = $this->CanvasCoordinates($A, $height, $left, $bottom, $unit); $BB = $this->CanvasCoordinates($B, $height, $left, $bottom, $unit); $PP1 = $this->CanvasCoordinates($P1, $height, $left, $bottom, $unit); $PP2 = $this->CanvasCoordinates($P2, $height, $left, $bottom, $unit); // print_r('C('.$CC[0].';'.$CC[1].')<br />'); // print_r('A('.$AA[0].';'.$AA[1].')<br />'); // print_r('B('.$BB[0].';'.$BB[1].')<br />'); // Calculate coordinates (labels) $Clabel = $this->LabelCoordinates($A, $B, 'center'); $Alabel = $this->LabelCoordinates($B, $A); $Blabel = $this->LabelCoordinates($A, $B); $P1label = $this->LabelCoordinates($C, $P1, 'minus'); $P2label = $this->LabelCoordinates($C, $P2, 'minus'); // Calculate canvas coordinates for labels $CClabel = $this->CanvasCoordinates($Clabel, $height, $left, $bottom, $unit); $AAlabel = $this->CanvasCoordinates($Alabel, $height, $left, $bottom, $unit); $BBlabel = $this->CanvasCoordinates($Blabel, $height, $left, $bottom, $unit); $PP1label = $this->CanvasCoordinates($P1label, $height, $left, $bottom, $unit); $PP2label = $this->CanvasCoordinates($P2label, $height, $left, $bottom, $unit); if ($progress == 1) { // Draw points $svg .= DrawCircle($AA[0], $AA[1], 3, 'red', 1, 'red'); $svg .= DrawCircle($BB[0], $BB[1], 3, 'red', 1, 'red'); // Draw labels $svg .= DrawText($AAlabel[0], $AAlabel[1], '$A$', $fontsize_label); $svg .= DrawText($BBlabel[0], $BBlabel[1], '$B$', $fontsize_label); } elseif ($progress == 2) { // AB $svg .= DrawLine($AA[0], $AA[1], $BB[0], $BB[1], 'blue', 2); // Draw points $svg .= DrawCircle($AA[0], $AA[1], 3, 'red', 1, 'red'); $svg .= DrawCircle($BB[0], $BB[1], 3, 'red', 1, 'red'); $svg .= DrawCircle($CC[0], $CC[1], 3, 'red', 1, 'red'); // Draw labels $svg .= DrawText($AAlabel[0], $AAlabel[1], '$A$', $fontsize_label); $svg .= DrawText($BBlabel[0], $BBlabel[1], '$B$', $fontsize_label); $svg .= DrawText($CClabel[0], $CClabel[1], '$C$', $fontsize_label); } elseif ($progress == 3) { // Circle $svg .= DrawCircle($CC[0], $CC[1], $r * $unit, 'blue', 2); // Draw points $svg .= DrawCircle($AA[0], $AA[1], 3, 'red', 1, 'red'); $svg .= DrawCircle($BB[0], $BB[1], 3, 'red', 1, 'red'); $svg .= DrawCircle($CC[0], $CC[1], 3, 'red', 1, 'red'); // Draw labels $svg .= DrawText($AAlabel[0], $AAlabel[1], '$A$', $fontsize_label); $svg .= DrawText($BBlabel[0], $BBlabel[1], '$B$', $fontsize_label); $svg .= DrawText($CClabel[0], $CClabel[1], '$C$', $fontsize_label); } elseif ($progress == 4) { // Axes if ($axis == 'x') { $svg .= DrawLine(0, $OO[1], $width, $OO[1], 'limegreen', 2); } else { $svg .= DrawLine($OO[0], $height, $OO[0], 0, 'limegreen', 2); } // Circle $svg .= DrawCircle($CC[0], $CC[1], $r * $unit, 'blue', 2); // Draw points $svg .= DrawCircle($PP1[0], $PP1[1], 3, 'red', 1, 'red'); $svg .= DrawCircle($PP2[0], $PP2[1], 3, 'red', 1, 'red'); // Draw labels $svg .= DrawText($PP1label[0], $PP1label[1], '$P_1$', $fontsize_label); $svg .= DrawText($PP2label[0], $PP2label[1], '$P_2$', $fontsize_label); } $svg .= '</svg></div>'; return $svg; }