因此,请考虑以下图像:
这是一张350x350像素的png图片。
我写了如下
拉维尔5.7
确定什么是水,什么不是水的代码:
<?php
use Illuminate\Database\Seeder;
use App\Modules\Locations\Services\CreateMapService;
use ChristianEssl\LandmapGeneration\Struct\Color;
use App\Modules\Locations\Models\Location;
class SurfaceLocations extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$water = new Color(66, 129, 178);
$land = new Color(23, 132, 72);
$createImage = new CreateMapService($land, $water, 350, 350, 'random_map');
$createImage->generateMap('surface');
$contents = Storage::disk('maps')->get('surface.png');
$imageResource = imagecreatefromstring($contents);
$waterR = 112;
$waterG = 219;
$waterB = 255;
for ($x = 0; $x <= 349; $x++) {
for($y = 0; $y <= 349; $y++) {
$rgb = imagecolorat($imageResource, $x, $y);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
if ($r === $waterR && $g === $waterG && $b === $waterB) {
Location::create([
'x' => $x,
'y' => $y,
'is_water' => true
]);
} else {
Location::create([
'x' => $x,
'y' => $y,
'is_water' => false
]);
}
}
}
}
}
这是完美的,需要一点时间,但它的工作。它设置
is_water
当我们遇到图片的蓝色部分时。
我现在要做的是检查,不仅是我们在水上的像素,而且如果旁边的像素不是,随机检查(
rand(0, 100) >= 50
)如果我们设置一个
is_port
真的。
这里的想法是沿着“海岸”设置一个港口,但有时只能这样做。(以便海岸线的每个像素不是一个港口,而是沿着所述海岸可以有一个或多个港口。
我现在的逻辑可以修改来检查,但问题是如何修改?
我唯一能想到的是x和y的递增,以及它们的递减,当我们在水上时,检查这些值是否不是水,如果不是,检查我们是否通过了“检查”,如果是这样设置的
是端口
真的。
有什么想法吗?