Skip to content
This repository was archived by the owner on Jul 26, 2025. It is now read-only.

Commit 27eadb1

Browse files
committed
fix: fix bug with transparent circle fill
1 parent 692b155 commit 27eadb1

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

src/draw/drawCircleOnImage.ts

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,25 +73,51 @@ export function drawCircleOnImage(
7373
if (radius === 1) {
7474
setBlendedVisiblePixel(newImage, center.column, center.row, fill);
7575
}
76+
//Starting points for the top and bottom row of the circle.
77+
let prevBotRow = center.row + radius;
78+
let prevTopRow = center.row - radius;
79+
let index = 0;
7680
circle(center.column, center.row, radius, (column: number, row: number) => {
7781
setBlendedVisiblePixel(newImage, column, row, color);
78-
79-
//todo: fill is not optimal we can fill symmetrically
80-
if (column - 1 > center.column) {
82+
// Filling the first line of the circle.
83+
if (index === 0) {
8184
newImage.drawLine(
8285
{ row, column: column - 1 },
83-
{ row, column: center.column },
86+
{
87+
row,
88+
column: center.column - (column - center.column - 1),
89+
},
8490
{ strokeColor: fill, out: newImage },
8591
);
86-
} else if (column + 1 < center.column) {
92+
}
93+
// The bresenham algorithm is drawing the circle in 4 parts. We are filling the top and bottom part of the circle. Therefore we check whether the point belongs to top or bottom part through indexes. Index must be 1 or 3 to fill the circle.
94+
// Filling bottom half of the circle.
95+
if ((index - 1) % 4 === 0 && prevBotRow !== row) {
8796
newImage.drawLine(
8897
{ row, column: column + 1 },
89-
{ row, column: center.column },
98+
{
99+
row,
100+
column: center.column - (column - center.column + 1),
101+
},
90102
{ strokeColor: fill, out: newImage },
91103
);
104+
105+
prevBotRow = row;
106+
// Filling top half of the circle.
107+
} else if ((index - 3) % 4 === 0 && prevTopRow !== row) {
108+
newImage.drawLine(
109+
{ row, column: column - 1 },
110+
{
111+
row,
112+
column: center.column - (column - center.column - 1),
113+
},
114+
{ strokeColor: fill, out: newImage },
115+
);
116+
prevTopRow = row;
92117
}
118+
119+
index++;
93120
});
94121
}
95-
96122
return newImage;
97123
}

0 commit comments

Comments
 (0)