Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php draw line pixel

/**
  * Draw a line using Bresenham's line algorithm.
  *
  * @param resource $im
  *   The image resource.
  * @param int $x0
  *   The x part of the starting coordinate.
  * @param int $y0
  *   The y part of the starting coordinate.
  * @param int $x1
  *   The x part of the ending coordinate.
  * @param int $y1
  *   The y part of the ending coordinate.
  * @param int $color
  *   The color of the line, created from imagecolorallocate().
  */
function drawLine($im, $x0, $y0, $x1, $y1, $color) {
  if ($x0 == $x1 && $y0 == $y1) {
    // Start and finish are the same.
    imagesetpixel($im, $x0, $y0, $color);
    return;
  }

  $dx = $x1 - $x0;
  if ($dx < 0) {
    // x1 is lower than x0.
    $sx = -1;
  } else {
    // x1 is higher than x0.
    $sx = 1;
  }

  $dy = $y1 - $y0;
  if ($dy < 0) {
    // y1 is lower than y0.
    $sy = -1;
  } else {
    // y1 is higher than y0.
    $sy = 1;
  }

  if (abs($dy) < abs($dx)) {
    // Slope is going downwards.
    $slope = $dy / $dx;
    $pitch = $y0 - $slope * $x0;

    while ($x0 != $x1) {
      imagesetpixel($im, $x0, round($slope * $x0 + $pitch), $color);
      $x0 += $sx;
    }
  } else {
    // Slope is going upwards.
    $slope = $dx / $dy;
    $pitch = $x0 - $slope * $y0;

    while ($y0 != $y1) {
      imagesetpixel($im, round($slope * $y0 + $pitch), $y0, $color);
      $y0 += $sy;
    }
  }

  // Finish by adding the final pixel.
  imagesetpixel($im, $x1, $y1, $color);
}
Comment

PREVIOUS NEXT
Code Example
Php :: substr last 3 characters 
Php :: php get woocommerce attribute from database 
Php :: console_log in php 
Php :: laravel websockets 
Php :: create file in directory php 
Php :: strpos 
Php :: wordpress add submenu under custom post type 
Php :: magento 2 remove order 
Php :: phpmailer doesnt work on infinityfree 
Php :: how to use get php with index php with url 
Php :: php fpdf in phpmailer 
Php :: arry to string php 
Php :: drupal show php errors 
Php :: readable date in php 
Php :: octobercms mail 
Php :: laravel add many to many 
Php :: Best documentation tools for php 
Php :: Web servers supported by php 
Php :: laravel get last created id 
Php :: php add number to the existing name 
Php :: php array sort 
Php :: Csv To AssoT Php 
Php :: wp wordPress variables de session 
Php :: wordpress if page 
Php :: hint extension in visual studio code for laravel 
Php :: create model for existing table in laravel 
Php :: laravel stack script 
Php :: guzzle login example 
Php :: what is php 
Php :: php get variable name as a string 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =