php怎么用for循环做心形
2个回答
展开全部
<?php
for ($t = 0; $t < 360; $t++)
{
$y = 2 * cos($t) - cos(2 * $t); //笛卡尔心形曲线函数
$x = 2 * sin($t) - sin(2 * $t);
$x += 3;
$y += 3;
$x *= 70;
$y *= 70;
$x = round($x);
$y = round($y);
$str[] = $x;
$y = $y + 2 * (180 - $y);//图像上下翻转
$x = $y;
$str[] = $x;
}
$im = imagecreate(400, 400);//创建画布400*400
$black = imagecolorallocate($im, 0, 0, 0);
$red = imagecolorallocate($im, 255, 0, 0);//设置颜色
imagepolygon($im, $str, 360, $red);
imagestring($im, 5, 190, 190, "love", $red);//输出字符串
header('Content-type:image/gif');//通知浏览器输出的是gif图片
imagegif($im);//输出图片
imagedestroy($im);//销毁
?>
展开全部
<?php
// 打印心形上半部分
for ($i = 1; $i <= 10; $i++) {
for ($j = 1; $j <= 10; $j++) {
if (($i == 1 && ($j == 4 || $j == 7)) || ($i == 2 && ($j == 3 || $j == 8)) || ($i == 3 && ($j == 2 || $j == 9)) || ($i == 4 && ($j == 2 || $j == 9)) || ($i == 5 && ($j == 1 || $j == 10)) || ($i == 6 && ($j == 2 || $j == 9)) || ($i == 7 && ($j == 3 || $j == 8)) || ($i == 8 && ($j == 4 || $j == 7)) || ($i == 9 && ($j == 5 || $j == 6)) || ($i == 10 && ($j == 6)))) {
echo "*";
} else {
echo " ";
}
}
echo "<br>";
}
// 打印心形下半部分
for ($i = 1; $i <= 10; $i++) {
for ($j = 1; $j <= 10; $j++) {
if (($i == 1 && ($j == 6)) || ($i == 2 && ($j == 5 || $j == 7)) || ($i == 3 && ($j == 4 || $j == 8)) || ($i == 4 && ($j == 3 || $j == 9)) || ($i == 5 && ($j == 2 || $j == 10)) || ($i == 6 && ($j == 1 || $j == 9)) || ($i == 7 && ($j == 2 || $j == 8)) || ($i == 8 && ($j == 3 || $j == 7)) || ($i == 9 && ($j == 4 || $j == 6)) || ($i == 10 && ($j == 5)))) {
echo "*";
} else {
echo " ";
}
}
echo "<br>";
}
?>
// 打印心形上半部分
for ($i = 1; $i <= 10; $i++) {
for ($j = 1; $j <= 10; $j++) {
if (($i == 1 && ($j == 4 || $j == 7)) || ($i == 2 && ($j == 3 || $j == 8)) || ($i == 3 && ($j == 2 || $j == 9)) || ($i == 4 && ($j == 2 || $j == 9)) || ($i == 5 && ($j == 1 || $j == 10)) || ($i == 6 && ($j == 2 || $j == 9)) || ($i == 7 && ($j == 3 || $j == 8)) || ($i == 8 && ($j == 4 || $j == 7)) || ($i == 9 && ($j == 5 || $j == 6)) || ($i == 10 && ($j == 6)))) {
echo "*";
} else {
echo " ";
}
}
echo "<br>";
}
// 打印心形下半部分
for ($i = 1; $i <= 10; $i++) {
for ($j = 1; $j <= 10; $j++) {
if (($i == 1 && ($j == 6)) || ($i == 2 && ($j == 5 || $j == 7)) || ($i == 3 && ($j == 4 || $j == 8)) || ($i == 4 && ($j == 3 || $j == 9)) || ($i == 5 && ($j == 2 || $j == 10)) || ($i == 6 && ($j == 1 || $j == 9)) || ($i == 7 && ($j == 2 || $j == 8)) || ($i == 8 && ($j == 3 || $j == 7)) || ($i == 9 && ($j == 4 || $j == 6)) || ($i == 10 && ($j == 5)))) {
echo "*";
} else {
echo " ";
}
}
echo "<br>";
}
?>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询