PHP Classes

PHP Rectangle Class: Perform operations and draw rectangle shapes

Recommend this page to a friend!
  Info   View files Example   Screenshots Screenshots   View files View files (5)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-02-28 (7 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 43 All time: 10,652 This week: 107Up
Version License PHP version Categories
php-rectslib 1.0Custom (specified...5Algorithms, PHP 5, Graphics, Math
Description 

Author

This class can perform operations and draw rectangle shapes.

It can take the coordinates of rectangular areas on a two-dimensional plane and perform several operations.

Currently, it can perform operations like:

- Merging rectangles

- Unite rectangles

- Subtract rectangles

- Etc...

After the rectangle operations, the class can also render the resulting shapes as images.

Picture of Win Aung Cho
  Performance   Level  
Name: Win Aung Cho is available for providing paid consulting. Contact Win Aung Cho .
Classes: 11 packages by
Country: Myanmar Myanmar
Age: 59
All time rank: 25484 in Myanmar Myanmar
Week rank: 569 Up2 in Myanmar Myanmar Up
Innovation award
Innovation award
Nominee: 3x

Example

<?php
/******
 * RECTSLIB CLASS
 *
 * [RECTSLIB] is a utility for the group of rectangles.
 * Lib help you to construct the rectangles in various logical operations such as Union, Intersect abd Subtract.
 * This lib is free for the educational use as long as maintain this header together with this lib.
 * Author: Win Aung Cho
 * Contact winaungcho@gmail.com
 * version 1.0
 * Date: 17-02-2023
 *
 ******/

require_once ("rectslib.php");
$img_width = 640;
$img_height = 480;
 
$img = imagecreatetruecolor($img_width, $img_height);

$black = imagecolorallocate($img, 0, 0, 0);
$white = imagecolorallocate($img, 255, 255, 255);
$grey = imagecolorallocate($img, 128, 128, 128);
$red = imagecolorallocate($img, 255, 0, 0);
$green = imagecolorallocate($img, 0, 255, 0);
$blue = imagecolorallocate($img, 0, 200, 250);
$orange = imagecolorallocate($img, 255, 200, 0);
$brown = imagecolorallocate($img, 220, 110, 0);
$magenta = imagecolorallocate($img, 220, 0, 220);
$indego = imagecolorallocate($img, 220, 110, 110);


$rectslib = new RectsLib();
$A = $rectslib->p1p2(10, 10, 300, 200, "A", $green);
$B = $rectslib->p1p2(40, 30, 200, 300, "B", $red);
$C = $rectslib->p1p2(150, 40, 400, 150, "C", $orange);
$D = $rectslib->p1p2(140, 320, 200, 400, "D", $blue);
$E = $rectslib->p1p2(80, 160, 250, 250, "E", $brown);
$rects = [$A, $B, $C, $D, $E];
echo
$rectslib->showRects($rects);

$mode = 0;
if (isset(
$_GET["m"]))
   
$mode = $_GET["m"];
   
$rectslib->fillRects($img, $rects);
$all = $rectslib->merge($rects, $grey);
$fname = "rectsorigin.png";
echo
"<div style='font-size:30px;'>";
if (
$mode == 0){
    echo
"Original rectangles.";
    echo
"\t<a href='?m=1'>NEXT</a>";
}
if (
$mode == 1){
   
$rectslib->fillRects($img, $all);
   
$rectslib->drawRects($img, $all, $brown);
   
$fname = "rectsuniversal.png";
    echo
"Universal mesh for all rectangles.";
    echo
"\t<a href='?m=2'>NEXT</a>";
}
if (
$mode == 2){
   
$uni = $rectslib->union([$A, $B], [$C, $D]);
   
$rectslib->fillRects($img, $uni);
   
$rectslib->drawRects($img, $uni, $indego);
   
$edges = $rectslib->genEdges($uni);
   
$rectslib->drawEdges($img, $edges, $white);
   
$fname = "rectsunion.png";
    echo
"Union of [A B] & [C D]";
    echo
"\t<a href='?m=3'>NEXT</a>";
}

if (
$mode == 3){
   
$Intersect = $rectslib->intersect([$A, $B, $C, $D], [$B, $E]);
   
$rectslib->fillRects($img, $Intersect);
   
$rectslib->drawRects($img, $Intersect, $indego);
   
$edges = $rectslib->genEdges($Intersect);
   
$rectslib->drawEdges($img, $edges, $white);
   
$fname = "rectsintersect.png";
    echo
"Intersect of [A B C D] & [B E]";
    echo
"\t<a href='?m=4'>NEXT</a>";
}
if (
$mode == 4){
   
$subt = $rectslib->subtract([$A, $B, $C, $D], [$B, $E]);
   
$rectslib->fillRects($img, $subt);
   
$rectslib->drawRects($img, $subt, $indego);
   
$edges = $rectslib->genEdges($subt);
   
$rectslib->drawEdges($img, $edges, $white);
   
$fname = "rectssubtract.png";
    echo
"Subtract [B E] from [A B C D]";
}


imagePng($img, './images/'.$fname);
imagedestroy($img);
echo
"</div><br/>";
echo
"<img src='images/$fname?u=".time()."'/>";;
/*
A-GREEN-{10,10:300,200}
B-RED-{40,30:200,300}
C-ORANGE-{150,40:400,150}
D-BLUE-{140,320:200,400}
E-BROWN-{80,160:250,250}
*/
?>


Screenshots  
  • images/rectsintersect.png
  • images/rectsorigin.png
  • images/rectssubtract.png
  • images/rectsunion.png
  • images/rectsuniversal.png
  Files folder image Files  
File Role Description
Files folder imagesrc (2 files, 1 directory)
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imageimages (1 file)
  Accessible without login Plain text file example.php Example Example script
  Plain text file rectslib.php Class Class source

  Files folder image Files  /  src  /  images  
File Role Description
  Accessible without login Plain text file index.php Aux. Auxiliary script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:43
This week:0
All time:10,652
This week:107Up