1
|
|
|
<?php |
|
|
|
|
2
|
|
|
use nstdio\svg\container\G; |
3
|
|
|
use nstdio\svg\container\SVG; |
4
|
|
|
use nstdio\svg\shape\Circle; |
5
|
|
|
use nstdio\svg\shape\Path; |
6
|
|
|
use nstdio\svg\shape\Rect; |
7
|
|
|
use nstdio\svg\text\Text; |
8
|
|
|
use nstdio\svg\util\Bezier; |
9
|
|
|
|
10
|
|
|
require_once __DIR__ . '/../vendor/autoload.php'; |
11
|
|
|
$p0x = 90; |
12
|
|
|
$p0y = 200; |
13
|
|
|
|
14
|
|
|
$p1x = 90; |
15
|
|
|
$p1y = 380; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
$p2x = 450; |
19
|
|
|
$p2y = 200; |
20
|
|
|
|
21
|
|
|
$p3x = 450; |
22
|
|
|
$p3y = 380; |
23
|
|
|
|
24
|
|
|
drawCubic(820, 239, 302, 127, 411, 237, 548, 327); |
25
|
|
|
drawCubic(208, 120, 634, 421, 728, 276, 342, 69); |
26
|
|
|
|
27
|
|
|
for ($i = 0; $i < 5; $i++) { |
28
|
|
|
drawCubic( |
29
|
|
|
mt_rand(0, 980), |
30
|
|
|
mt_rand(0, 480), |
31
|
|
|
mt_rand(0, 980), |
32
|
|
|
mt_rand(0, 480), |
33
|
|
|
mt_rand(0, 980), |
34
|
|
|
mt_rand(0, 480), |
35
|
|
|
mt_rand(0, 980), |
36
|
|
|
mt_rand(0, 480) |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
function drawCubic($p0x, $p0y, $p1x, $p1y, $p2x, $p2y, $p3x, $p3y) |
41
|
|
|
{ |
42
|
|
|
$svg = new SVG(1000, 600); |
43
|
|
|
$svg->getElement()->removeAttribute('viewBox'); |
44
|
|
|
|
45
|
|
|
$controlPts = new Path($svg, $p0x, $p0y); |
46
|
|
|
$controlPts->lineTo($p1x, $p1y) |
47
|
|
|
->lineTo($p2x, $p2y) |
48
|
|
|
->lineTo($p3x, $p3y) |
49
|
|
|
->apply(['fill' => 'none', 'stroke' => 'lightgray']); |
50
|
|
|
|
51
|
|
|
(new Path($svg, $p0x, $p0y)) |
52
|
|
|
->curveTo($p1x, $p1y, $p2x, $p2y, $p3x, $p3y) |
53
|
|
|
->apply(['fill' => 'none', 'stroke' => 'red']); |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
$fz = (new G($svg))->apply(['font-size' => '10px']); |
57
|
|
|
|
58
|
|
|
$blackGroup = (new G($fz))->apply(['fill' => 'black']); |
59
|
|
|
|
60
|
|
|
addCtrlPt($blackGroup, $p0x, $p0y, -20, 15, 'P0'); |
61
|
|
|
addCtrlPt($blackGroup, $p1x, $p1y, -20, -10, 'P1'); |
62
|
|
|
addCtrlPt($blackGroup, $p2x, $p2y, -20, -10, 'P2'); |
63
|
|
|
addCtrlPt($blackGroup, $p3x, $p3y, -20, 15, 'P3'); |
64
|
|
|
|
65
|
|
|
drawBBox($svg, $p0x, $p0y, $p1x, $p1y, $p2x, $p2y, $p3x, $p3y); |
66
|
|
|
|
67
|
|
|
(new Rect($svg, 498, 998, 1, 1))->apply(['fill' => 'none', 'stroke' => 'blue', 'stroke-width' => '1']); |
68
|
|
|
|
69
|
|
|
echo $svg; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
function addCtrlPt($parent, $x, $y, $dx, $dy, $ptName) |
73
|
|
|
{ |
74
|
|
|
new Circle($parent, $x, $y, 3); |
75
|
|
|
$p0Text = new Text($parent, "$ptName($x, $y)"); |
76
|
|
|
$p0Text->apply(['x' => $x + $dx, 'y' => $y + $dy]); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
function drawBBox($parent, $p0x, $p0y, $p1x, $p1y, $p2x, $p2y, $p3x, $p3y) |
80
|
|
|
{ |
81
|
|
|
$box = Bezier::cubicBBox($p0x, $p0y, $p1x, $p1y, $p2x, $p2y, $p3x, $p3y); |
82
|
|
|
(new Rect($parent, $box['height'], $box['width'], $box['x'], $box['y'])) |
83
|
|
|
->apply([ |
84
|
|
|
'stroke' => 'gray', |
85
|
|
|
'stroke-dasharray' => '8 8', |
86
|
|
|
'stroke-linecap' => 'round', |
87
|
|
|
'fill' => 'blue', |
88
|
|
|
'fill-opacity' => 0.1, |
89
|
|
|
]); |
90
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.