Circle iterator now compensates for the matrix size and won't overflow.

This commit is contained in:
Zed A. Shaw 2024-12-25 01:15:33 -05:00
parent 35f2defc11
commit 857cd2f910
5 changed files with 25 additions and 23 deletions

View file

@ -180,21 +180,24 @@ namespace matrix {
}
circle::circle(Point center, int radius) :
circle::circle(Matrix &mat, Point center, int radius) :
center(center), radius(radius)
{
top = max(center.y - radius, size_t(0));
bottom = center.y + radius;
width = matrix::width(mat);
height = matrix::height(mat);
top = max(int(center.y - radius), 0);
bottom = min(int(center.y + radius), height);
y = top;
}
bool circle::next() {
y++;
if(y <= bottom) {
if(y < bottom) {
dy = y - center.y;
dx = floor(sqrt(radius * radius - dy * dy));
left = center.x - dx;
right = center.x + dx;
left = max(0, int(center.x - dx));
right = min(width, int(center.x + dx));
return true;
} else {
return false;