Skip to content

Instantly share code, notes, and snippets.

@andrelashley
Created February 21, 2012 22:18
Show Gist options
  • Save andrelashley/1879412 to your computer and use it in GitHub Desktop.
Save andrelashley/1879412 to your computer and use it in GitHub Desktop.
Circle class
<?php
/*
Lab 7
circle.php
Andre Lashley (andre.lashley@gmail.com)
February 20, 2012
4 hours estimated completion time
4 hours actual completion time
This program draws 6 different shapes.
Visit http://deepblue.cs.camosun.bc.ca/~cst020/comp170/lab7/main.html to use it.
This program uses source code found at http://ca.php.net/manual/en/function.imagecolorallocate.php in the comments
to convert hex values passed into the function to RGB values.
*/
class Circle{
// create the properties of a circle and the canvas its drawn on
private $shapeSize;
private $location;
private $canvasHsize;
private $canvasVsize;
private $canvas;
private $fgcolor;
private $bgcolor;
// write a constructor to take the size of the shape, its location, the horizontal size of the canvas,
// and its vertical size
function __construct($inCanvasHsize, $inCanvasVsize, $inFgcolor, $inBgcolor, $inLocation, $inSize){
$this->shapeSize = $inSize;
$this->location = $inLocation;
$this->canvasHsize = $inCanvasHsize;
$this->canvasVsize = $inCanvasVsize;
$this->fgcolor = $inFgcolor;
$this->bgcolor = $inBgcolor;
// Output the header telling the browser this is a png image.
header ('Content-type: image/png');
// create the empty canvas.
$this->canvas = imagecreatetruecolor($this->canvasHsize, $this->canvasVsize) or die('Cannot Initialize new GD image stream');
// convert the foreground and background colors to their associated hex values
$this->bgcolor = $this->hex2rgb($this->bgcolor);
$this->fgcolor = $this->hex2rgb($this->fgcolor);
/* Define the foreground and background colours and fill the image with the background colour.
The foreground colour is passed as a parameter, the background colour is white */
$this->fgcolor = imagecolorallocate($this->canvas, $this->fgcolor[0], $this->fgcolor[1], $this->fgcolor[2]);
$this->bgcolor = imagecolorallocate($this->canvas, $this->bgcolor[0], $this->bgcolor[1], $this->bgcolor[2]);
imagefill($this->canvas, 0, 0, $this->bgcolor);
}
// make the shape bigger by some size
public function bigger(){
$this->shapeSize += 10;
}
// make the shape smaller by some size
public function smaller(){
$this->shapeSize -= 10;
}
// make the shape move up by some amount
public function moveUp(){
$this->location[1] += 15;
}
// make the shape move down by some amount
public function moveDown(){
$this->location[1] -= 15;
}
// make the shape move left by some amount
public function moveLeft(){
$this->location[0] -= 15;
}
// move the shape right by some amount
public function moveRight(){
$this->location[0] += 15;
}
// Draw a circle based on the location passed into the function.
public function draw(){
imageellipse($this->canvas, $this->location[0], $this->canvasHsize - $this->location[1], $this->shapeSize, $this->shapeSize, $this->fgcolor);
}
// display what we've drawn on the canvas
public function display(){
/* Write the image to standard out - in the case of the web server to the network to the browser
and free up any memory used by the image */
imagepng($this->canvas);
imagedestroy($this->canvas);
}
// create a helper method to convert hex values to RGB
private function hex2rgb($rgb){
/**
* Convert color from hex in XXXXXX (eg. FFFFFF, 000000, FF0000) to array(R, G, B)
* of integers (0-255).
*
* name: rgb2array
* author: Yetty
* @param $color hex in XXXXXX (eg. FFFFFF, 000000, FF0000)
* @return string; array(R, G, B) of integers (0-255)
*/
return array(
base_convert(substr($rgb, 0, 2), 16, 10),
base_convert(substr($rgb, 2, 2), 16, 10),
base_convert(substr($rgb, 4, 2), 16, 10),
);
}
}
// instantiate a circle object
$circle = new Circle(300, 300, "FFFFFF", "000000", array(75, 55), 50);
$circle->moveUp();
$circle->moveUp();
$circle->draw();
$circle->smaller();
$circle->smaller();
$circle->moveRight();
$circle->draw();
$circle->display();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment