-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate-pattern.php
175 lines (127 loc) · 4 KB
/
template-pattern.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
abstract class Shape {
protected $_points;
abstract public function setPoints();
abstract public function getShape();
public function Draw() {
$this->setPoints();
$image = $this->getShape();
imagepng($image, 'shapeimage.png');
imagedestroy($image);
}
}
class Triangle extends Shape {
public function setPoints() {
$this->_points = array(50, 70, 100);
;
}
public function getShape() {
$c = $this->_points[2]; // base
$b = $this->_points[1]; // sides
$a = $this->_points[0];
// calculate angle, cosine rule
$alpha = acos((pow($b, 2) + pow($c, 2) - pow($a, 2)) / (2 * $b * $c));
// pythag to calc height and y distance
$height = abs(sin($alpha)) * $b;
$width = abs(cos($alpha)) * $b;
$x = 0; // start point
$y = 100;
$points = array(
$x, $y, // start
$x + $c, $y, // base
$x + $width, $y - $height // apex
);
// draw
$image = imagecreatetruecolor(100, 100);
// sets background to red
$white = ImageColorAllocate($image, 255, 255, 255);
ImageFillToBorder($image, 0, 0, $white, $white);
$blue = imagecolorallocate($image, 0, 0, 255);
imagefilledpolygon($image, $points, 3, $blue);
return $image;
}
}
class Rectangle extends Shape {
public function setPoints() {
$this->_points = array(4, 4, 195, 195);
}
public function getShape() {
$x1 = $this->_points[0];
$y1 = $this->_points[1];
$x2 = $this->_points[2];
$y2 = $this->_points[3];
// Create a 55x30 image
$image = imagecreatetruecolor(200, 200);
$white = imagecolorallocate($image, 255, 255, 255);
// Draw a white rectangle
imagefilledrectangle($image, $x1, $y1, $x2, $y2, $white);
return $image;
}
}
class Ellipse extends Shape {
public function setPoints() {
$this->_points = array(100, 100, 100, 100);
}
public function getShape() {
$cx = $this->_points[0];
$cy = $this->_points[1];
$width = $this->_points[2];
$height = $this->_points[3];
// Create a 55x30 image
$image = imagecreatetruecolor(200, 200);
$white = imagecolorallocate($image, 255, 255, 255);
// choose a color for the ellipse
$ellipseColor = imagecolorallocate($image, 0, 0, 255);
// draw the white ellipse
imagefilledellipse($image, $cx, $cy, $width, $height, $ellipseColor);
return $image;
}
}
class Polygon extends Shape {
public function setPoints() {
$this->_points = array(
40, 50, // Point 1 (x, y)
20, 240, // Point 2 (x, y)
60, 60, // Point 3 (x, y)
240, 20, // Point 4 (x, y)
50, 40, // Point 5 (x, y)
10, 10 // Point 6 (x, y)
);
}
public function getShape() {
// create image
$image = imagecreatetruecolor(250, 250);
// allocate colors
$bg = imagecolorallocate($image, 0, 0, 0);
$blue = imagecolorallocate($image, 0, 0, 255);
// fill the background
imagefilledrectangle($image, 0, 0, 249, 249, $bg);
// draw a polygon
imagefilledpolygon($image, $this->_points, 6, $blue);
return $image;
}
}
//$ellipse = new Ellipse;
//$ellipse->Draw();
//$rectangle = new Rectangle;
//$rectangle->Draw();
//$polygon = new Polygon;
//$polygon->Draw();
//$triangle = new Triangle;
//$triangle->Draw();
if ($_POST && $_POST['shape']) {
$shape_name = $_POST['shape'];
$shape = new $shape_name;
$shape->Draw();
}
?>
<form method="POST">
<select name="shape">
<option>Ellipse</option>
<option>Rectangle</option>
<option>Polygon</option>
<option>Triangle</option>
</select>
<input type="submit" value="Draw" name="Draw" />
<img src="shapeimage.png" alt="Shape Image"/>
</form>