You are here

function pDraw::drawBezier in Visitors 7

Same name and namespace in other branches
  1. 7.2 pChart/class/pDraw.class.php \pDraw::drawBezier()
1 call to pDraw::drawBezier()
pDraw::drawSpline in pChart/class/pDraw.class.php

File

pChart/class/pDraw.class.php, line 635

Class

pDraw

Code

function drawBezier($X1, $Y1, $X2, $Y2, $Xv1, $Yv1, $Xv2, $Yv2, $Format = "") {
  $R = isset($Format["R"]) ? $Format["R"] : 0;
  $G = isset($Format["G"]) ? $Format["G"] : 0;
  $B = isset($Format["B"]) ? $Format["B"] : 0;
  $Alpha = isset($Format["Alpha"]) ? $Format["Alpha"] : 100;
  $ShowC = isset($Format["ShowControl"]) ? $Format["ShowControl"] : FALSE;
  $Segments = isset($Format["Segments"]) ? $Format["Segments"] : NULL;
  $Ticks = isset($Format["Ticks"]) ? $Format["Ticks"] : NULL;
  $NoDraw = isset($Format["NoDraw"]) ? $Format["NoDraw"] : FALSE;
  $PathOnly = isset($Format["PathOnly"]) ? $Format["PathOnly"] : FALSE;
  $Weight = isset($Format["Weight"]) ? $Format["Weight"] : NULL;
  $DrawArrow = isset($Format["DrawArrow"]) ? $Format["DrawArrow"] : FALSE;
  $ArrowSize = isset($Format["ArrowSize"]) ? $Format["ArrowSize"] : 10;
  $ArrowRatio = isset($Format["ArrowRatio"]) ? $Format["ArrowRatio"] : 0.5;
  $ArrowTwoHeads = isset($Format["ArrowTwoHeads"]) ? $Format["ArrowTwoHeads"] : FALSE;
  if ($Segments == NULL) {
    $Length = $this
      ->getLength($X1, $Y1, $X2, $Y2);
    $Precision = $Length * 125 / 1000;
  }
  else {
    $Precision = $Segments;
  }
  $P[0]["X"] = $X1;
  $P[0]["Y"] = $Y1;
  $P[1]["X"] = $Xv1;
  $P[1]["Y"] = $Yv1;
  $P[2]["X"] = $Xv2;
  $P[2]["Y"] = $Yv2;
  $P[3]["X"] = $X2;
  $P[3]["Y"] = $Y2;

  /* Compute the bezier points */
  $Q = "";
  $ID = 0;
  $Path = "";
  for ($i = 0; $i <= $Precision; $i = $i + 1) {
    $u = $i / $Precision;
    $C = "";
    $C[0] = (1 - $u) * (1 - $u) * (1 - $u);
    $C[1] = $u * 3 * (1 - $u) * (1 - $u);
    $C[2] = 3 * $u * $u * (1 - $u);
    $C[3] = $u * $u * $u;
    for ($j = 0; $j <= 3; $j++) {
      if (!isset($Q[$ID])) {
        $Q[$ID] = "";
      }
      if (!isset($Q[$ID]["X"])) {
        $Q[$ID]["X"] = 0;
      }
      if (!isset($Q[$ID]["Y"])) {
        $Q[$ID]["Y"] = 0;
      }
      $Q[$ID]["X"] = $Q[$ID]["X"] + $P[$j]["X"] * $C[$j];
      $Q[$ID]["Y"] = $Q[$ID]["Y"] + $P[$j]["Y"] * $C[$j];
    }
    $ID++;
  }
  $Q[$ID]["X"] = $X2;
  $Q[$ID]["Y"] = $Y2;
  if (!$NoDraw) {

    /* Display the control points */
    if ($ShowC && !$PathOnly) {
      $Xv1 = floor($Xv1);
      $Yv1 = floor($Yv1);
      $Xv2 = floor($Xv2);
      $Yv2 = floor($Yv2);
      $this
        ->drawLine($X1, $Y1, $X2, $Y2, array(
        "R" => 0,
        "G" => 0,
        "B" => 0,
        "Alpha" => 30,
      ));
      $MyMarkerSettings = array(
        "R" => 255,
        "G" => 0,
        "B" => 0,
        "BorderR" => 255,
        "BorderB" => 255,
        "BorderG" => 255,
        "Size" => 4,
      );
      $this
        ->drawRectangleMarker($Xv1, $Yv1, $MyMarkerSettings);
      $this
        ->drawText($Xv1 + 4, $Yv1, "v1");
      $MyMarkerSettings = array(
        "R" => 0,
        "G" => 0,
        "B" => 255,
        "BorderR" => 255,
        "BorderB" => 255,
        "BorderG" => 255,
        "Size" => 4,
      );
      $this
        ->drawRectangleMarker($Xv2, $Yv2, $MyMarkerSettings);
      $this
        ->drawText($Xv2 + 4, $Yv2, "v2");
    }

    /* Draw the bezier */
    $LastX = NULL;
    $LastY = NULL;
    $Cpt = NULL;
    $Mode = NULL;
    $ArrowS = NULL;
    foreach ($Q as $Key => $Point) {
      $X = $Point["X"];
      $Y = $Point["Y"];

      /* Get the first segment */
      if ($ArrowS == NULL && $LastX != NULL && $LastY != NULL) {
        $ArrowS["X2"] = $LastX;
        $ArrowS["Y2"] = $LastY;
        $ArrowS["X1"] = $X;
        $ArrowS["Y1"] = $Y;
      }
      if ($LastX != NULL && $LastY != NULL && !$PathOnly) {
        list($Cpt, $Mode) = $this
          ->drawLine($LastX, $LastY, $X, $Y, array(
          "R" => $R,
          "G" => $G,
          "B" => $B,
          "Alpha" => $Alpha,
          "Ticks" => $Ticks,
          "Cpt" => $Cpt,
          "Mode" => $Mode,
          "Weight" => $Weight,
        ));
      }

      /* Get the last segment */
      $ArrowE["X1"] = $LastX;
      $ArrowE["Y1"] = $LastY;
      $ArrowE["X2"] = $X;
      $ArrowE["Y2"] = $Y;
      $LastX = $X;
      $LastY = $Y;
    }
    if ($DrawArrow && !$PathOnly) {
      $ArrowSettings = array(
        "FillR" => $R,
        "FillG" => $G,
        "FillB" => $B,
        "Alpha" => $Alpha,
        "Size" => $ArrowSize,
        "Ratio" => $ArrowRatio,
      );
      if ($ArrowTwoHeads) {
        $this
          ->drawArrow($ArrowS["X1"], $ArrowS["Y1"], $ArrowS["X2"], $ArrowS["Y2"], $ArrowSettings);
      }
      $this
        ->drawArrow($ArrowE["X1"], $ArrowE["Y1"], $ArrowE["X2"], $ArrowE["Y2"], $ArrowSettings);
    }
  }
  return $Q;
}