Skip to content

Commit 7b1f92d

Browse files
committed
Fix polygon aliasing
1 parent 9c1a4de commit 7b1f92d

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

src/Circle.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ public function __construct(LatLng $center, string $strokeColor, int $strokeWeig
5353
$this->center = $center;
5454
$this->edge = $center;
5555
$this->strokeColor = \str_replace('#', '', $strokeColor);
56-
$this->strokeWeight = $strokeWeight;
57-
$this->fillColor = $fillColor;
56+
$this->strokeWeight = $strokeWeight > 0 ? $strokeWeight : 0;
57+
$this->fillColor = \str_replace('#', '', $fillColor);
5858
}
5959

6060
/**
@@ -100,7 +100,10 @@ public function draw(Image $image, MapData $mapData): Circle
100100

101101
$dImage = Image::newCanvas($image->getWidth(), $image->getHeight());
102102

103-
$dImage->drawCircle($center->getX(), $center->getY(), $length * 2, $this->strokeColor);
103+
if ($this->strokeWeight > 0) {
104+
$dImage->drawCircle($center->getX(), $center->getY(), $length * 2, $this->strokeColor);
105+
}
106+
104107
$dImage->drawCircle($center->getX(), $center->getY(), ($length - $this->strokeWeight) * 2, $this->fillColor);
105108

106109
$image->pasteOn($dImage, 0, 0);

src/Polygon.php

+25-22
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ class Polygon implements Draw
4242
public function __construct(string $strokeColor, int $strokeWeight, string $fillColor)
4343
{
4444
$this->strokeColor = \str_replace('#', '', $strokeColor);
45-
$this->strokeWeight = $strokeWeight;
46-
$this->fillColor = $fillColor;
45+
$this->strokeWeight = $strokeWeight > 0 ? $strokeWeight : 0;
46+
$this->fillColor = \str_replace('#', '', $fillColor);
4747
}
4848

4949
/**
@@ -78,31 +78,34 @@ function (LatLng $p) use ($mapData) {
7878
$this->points
7979
);
8080

81-
$dImage = Image::newCanvas($image->getWidth(), $image->getHeight());
82-
83-
$dImage->drawPolygon(
84-
\array_reduce(
85-
$cPoints,
86-
function (array $acc, XY $p) {
87-
$acc[] = $p->getX();
88-
$acc[] = $p->getY();
89-
return $acc;
90-
},
91-
[]
92-
),
93-
$this->fillColor
81+
$image->pasteOn(
82+
Image::newCanvas($image->getWidth(), $image->getHeight())
83+
->drawPolygon(
84+
\array_reduce(
85+
$cPoints,
86+
function (array $acc, XY $p) {
87+
$acc[] = $p->getX();
88+
$acc[] = $p->getY();
89+
return $acc;
90+
},
91+
[]
92+
),
93+
$this->fillColor
94+
),
95+
0,
96+
0
9497
);
9598

96-
foreach ($cPoints as $k => $point) {
97-
$pK = $k - 1;
98-
if (!isset($cPoints[$pK])) {
99-
$pK = \count($cPoints) - 1;
99+
if ($this->strokeWeight > 0) {
100+
foreach ($cPoints as $k => $point) {
101+
$pK = $k - 1;
102+
if (!isset($cPoints[$pK])) {
103+
$pK = \count($cPoints) - 1;
104+
}
105+
$image->drawLine($cPoints[$pK]->getX(), $cPoints[$pK]->getY(), $point->getX(), $point->getY(), $this->strokeWeight, $this->strokeColor);
100106
}
101-
$dImage->drawLine($cPoints[$pK]->getX(), $cPoints[$pK]->getY(), $point->getX(), $point->getY(), $this->strokeWeight, $this->strokeColor);
102107
}
103108

104-
$image->pasteOn($dImage, 0, 0);
105-
106109
return $this;
107110
}
108111
}

src/samples/resources/sample1.png

5.93 KB
Loading

0 commit comments

Comments
 (0)