Skip to content

Commit

Permalink
Reduced scope of variables left and right in `OtherExtensions.Cre…
Browse files Browse the repository at this point in the history
…atePolygonSet` (#313)

* Reduced scope of variables `left` and `right` in `OtherExtensions.CreatePolygonSet`

* Used `PointD` overloads in `ContainsPoint()`, had to add `explicit` operator overload to PointD

* Fixed formatting

---------

Co-authored-by: Lehonti Ramos <john@doe>
  • Loading branch information
Lehonti and Lehonti Ramos authored Aug 9, 2023
1 parent 39818b6 commit cb92af1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Pinta.Core/Classes/Point.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ public readonly double Magnitude ()
{
return new PointD (a.X + b.X, a.Y + b.Y);
}

public static explicit operator PointD (PointI p) => new PointD (p.X, p.Y);
}

public record struct Size
Expand Down
19 changes: 11 additions & 8 deletions Pinta.Core/Extensions/OtherExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,25 @@ public static IReadOnlyList<IReadOnlyList<PointI>> CreatePolygonSet (this BitMas
PointI last = new (start.X, start.Y + 1);
PointI curr = new (start.X, start.Y);
PointI next = curr;
PointI left = new ();
PointI right = new ();

// trace island outline
while (true) {
left.X = ((curr.X - last.X) + (curr.Y - last.Y) + 2) / 2 + curr.X - 1;
left.Y = ((curr.Y - last.Y) - (curr.X - last.X) + 2) / 2 + curr.Y - 1;

right.X = ((curr.X - last.X) - (curr.Y - last.Y) + 2) / 2 + curr.X - 1;
right.Y = ((curr.Y - last.Y) + (curr.X - last.X) + 2) / 2 + curr.Y - 1;
PointI left = new (
x: ((curr.X - last.X) + (curr.Y - last.Y) + 2) / 2 + curr.X - 1,
y: ((curr.Y - last.Y) - (curr.X - last.X) + 2) / 2 + curr.Y - 1
);

if (bounds.ContainsPoint (left.X, left.Y) && stencil[left]) {
PointI right = new (
x: ((curr.X - last.X) - (curr.Y - last.Y) + 2) / 2 + curr.X - 1,
y: ((curr.Y - last.Y) + (curr.X - last.X) + 2) / 2 + curr.Y - 1
);

if (bounds.ContainsPoint ((PointD) left) && stencil[left]) {
// go left
next.X += curr.Y - last.Y;
next.Y -= curr.X - last.X;
} else if (bounds.ContainsPoint (right.X, right.Y) && stencil[right]) {
} else if (bounds.ContainsPoint ((PointD) right) && stencil[right]) {
// go straight
next.X += curr.X - last.X;
next.Y += curr.Y - last.Y;
Expand Down

0 comments on commit cb92af1

Please sign in to comment.