我正在尝试查找将(x,y,z)转换为(x,y)所需的数学运算,以便可以使用php创建框。所有点将在网格上,并且没有旋转。将3D对象转换为2D空间
我已经尝试了这种解决方案https://stackoverflow.com/a/25955134/379249这让我的存在方式的一部分,但我需要弄清楚的多边形
。例如点,我在下面红色显示一个大箱子。
我需要能够给它要么宽度,长度,高度的尺寸和起点或我可以给它的所有四个点构成每个面上。
我需要翻译那些使它看起来3D,这是我绊倒了。
一旦红色框被创建,我也需要能够使用相同的功能,如绿色,蓝色或橙色创建其他框。
我可以创建代码,但我坚持数学来做翻译。
任何想法?
谢谢!
编辑 **
感谢@ user2464424我能得到它非常接近。以下是我的代码。
和我生成的图像。
<?php
function RotatePoint($sin,$cos,$x,$y) {
return array($x*$cos - $y*$sin, $y*$cos + $x*$sin);
}
$im = imagecreatetruecolor(200, 200);
$white = imagecolorallocate($im, 255, 255, 255);
imagefilledrectangle($im, 0, 0, 200, 200, $white);
$brown = imagecolorallocate($im, 120, 53, 31);
$green = imagecolorallocate($im, 23, 255, 65);
$blue = imagecolorallocate($im, 31, 23, 255);
$orange = imagecolorallocate($im, 255, 185, 23);
function draw_box(&$image, $color, $start, $width, $length, $height)
{
$camx = 80;
$camy = 240;
$camz = 40;
$yaw = 10;
$pitch = 20;
$sy = sin(-$yaw); $cy = cos(-$yaw); $sp = sin(-$pitch); $cp = cos(-$pitch);
$start_x = $start[0];
$start_y = $start[1];
$start_z = $start[2];
// Draw 6 faces
for ($i = 0; $i < 6; $i++)
{
switch ($i)
{
case 0:
$face = array(
array($start_x, $start_y, $start_z),
array($start_x+$width, $start_y, $start_z),
array($start_x+$width, $start_y, $start_z+$height),
array($start_x, $start_y, $start_z+$height)
);
break;
case 1:
$face = array(
array($start_x, $start_y+$length, $start_z),
array($start_x+$width, $start_y+$length, $start_z),
array($start_x+$width, $start_y+$length, $start_z+$height),
array($start_x, $start_y+$length, $start_z+$height)
);
break;
case 2:
$face = array(
array($start_x, $start_y, $start_z),
array($start_x, $start_y+$length, $start_z),
array($start_x, $start_y+$length, $start_z+$height),
array($start_x, $start_y, $start_z+$height)
);
break;
case 3:
$face = array(
array($start_x+$width, $start_y, $start_z),
array($start_x+$width, $start_y+$length, $start_z),
array($start_x+$width, $start_y+$length, $start_z+$height),
array($start_x+$width, $start_y, $start_z+$height)
);
break;
case 4:
$face = array(
array($start_x, $start_y, $start_z+$height),
array($start_x+$width, $start_y, $start_z+$height),
array($start_x+$width, $start_y+$length, $start_z+$height),
array($start_x, $start_y+$length, $start_z+$height)
);
break;
case 5:
$face = array(
array($start_x, $start_y, $start_z),
array($start_x+$width, $start_y, $start_z),
array($start_x+$width, $start_y+$length, $start_z),
array($start_x, $start_y+$length, $start_z)
);
break;
}
$polygon = array();
foreach ($face as $point)
{
$x = $point[0] - $camx;
$y = $point[1] - $camy;
$z = $point[2] - $camz;
$rot = RotatePoint($sy,$cy,$x,$y);
$x = $rot[0];
$y = $rot[1];
$rot = RotatePoint($sp,$cp,$z,$y);
$z = $rot[0];
$y = $rot[1];
$polygon[] = $x;
$polygon[] = $z;
}
imagepolygon($image, $polygon, 4, $color);
}
}
draw_box($im, $brown, array(0, 0, 0), 80, 80, 80);
draw_box($im, $green, array(0, 0, 0), 40, 40, 40);
draw_box($im, $blue, array(0, 0, 40), 30, 20, 10);
draw_box($im, $orange, array(60, 0, 0), 15, 40, 80);
imagepng($im, './image.png');
imagedestroy($im);
?>
<img src="image.png">
一对夫妇的问题。
1:我如何从底部获取轴的原点,现在它在右侧倒过来。
2:说我总是想在400x400的图像上生成它,我如何确保它不会脱落或能够放大?
3:如果容器是40x40x40或10x100x100,我如何调整原点,使其始终位于图像的底部,并且盒子的比例尺适合?
编辑 **
下面是最终代码的链接: https://gist.github.com/rzfarrell/3a9e5046dcfd6bd2d2f4bfa1a34b21ef
我认为你想要实现的是正射投影,这是正确的吗? – user2464424
有* bazillions *的方式投影3D到2D。如果您还没有,请查看:https://en.wikipedia.org/wiki/Graphical_projection。此外,翻译很容易(只需做x'= x + delta等)。轮换更难:https://en.wikipedia.org/wiki/Rotation_(mathematics) –
@ user2464424是正射投影是我正在寻找。 – bones