我想出了这个代码片段,它将创建一个带有笛卡尔坐标的地图,并使用这些坐标在 SVG 窗口中绘制它们。希望这会对某人有所帮助。
该函数将地图的中心作为 $x, $y 并围绕该坐标绘制地图。
public static function xyMap( $x, $y, $width = 0, $height = 0, $show = array('X')) {
$minx = $x - ($width / 2);
$maxx = $x + ($width / 2);
if ($minx < 0) {
$minx = 0;
$maxx = $width;
} elseif ($maxx > Yii::app()->params['maxMapX']) {
$maxx = Yii::app()->params['maxMapX'];
$minx = Yii::app()->params['maxMapX'] - $width;
}
$miny = $y - ($height / 2);
$maxy = $y + ($height / 2);
if ($miny < 0) {
$miny = 0;
$maxy = $height;
} elseif ($maxy > Yii::app()->params['maxMapY']) {
$maxy = Yii::app()->params['maxMapY'];
$miny = Yii::app()->params['maxMapY'] - $height;
}
$x_xform = -1 * $minx;
$y_xform = $maxy;
$x_scale = 1;
$y_scale = -1;
echo "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"$width\" height=\"$height\" viewBox=\"0 0 $width $height\">\n";
echo "<g transform=\"translate($x_xform, $y_xform) scale($x_scale,$y_scale)\">\n";
echo "<rect x=\"$minx\" y=\"$miny\" width=\"$width\" height=\"$height\" stroke=\"black\" stroke-width=\"2\" fill=\"white\" />\n";
echo "<circle title=\"Center\" cx=\"$x\" cy=\"$y\" r=\"3\" fill=\"red\"/>\n";
echo "</g>\n</svg>\n";
}