Skip to content

Commit

Permalink
UPD moved square iterators to Geom
Browse files Browse the repository at this point in the history
  • Loading branch information
deepnight committed Oct 17, 2024
1 parent f64b134 commit a529104
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 69 deletions.
69 changes: 0 additions & 69 deletions src/dn/Bresenham.hx
Original file line number Diff line number Diff line change
Expand Up @@ -367,21 +367,6 @@ class Bresenham {
}


/** Iterate a filled square. For non-even sizes, the square is aligned to the left/top. **/
public static function iterateCenteredSquareAlignLow(centerX:Int, centerY:Int, wid:Int, hei:Int, cb:Int->Int->Void) {
for(y in 0...hei)
for(x in 0...wid)
cb( Std.int(centerX-wid*0.5) + x, Std.int(centerY-hei*0.5) + y );
}

/** Iterate a filled square. For non-even sizes, the square is aligned to the right/bottom. **/
public static function iterateCenteredSquareAlignHigh(centerX:Int, centerY:Int, wid:Int, hei:Int, cb:Int->Int->Void) {
for(y in 0...hei)
for(x in 0...wid)
cb( Std.int(centerX-wid*0.5 + 0.5) + x, Std.int(centerY-hei*0.5 + 0.5) + y );
}


/**
* A helper function to iterate over each tile in a line from `(x0, y0)` to `(x1, y1)` while
* allowing diagonal traversal.
Expand Down Expand Up @@ -643,60 +628,6 @@ class Bresenham {
);
}

// Centered square (low-aligned)
for(w in [1,2,3,4,7])
for(h in [1,2,3,4,7]) {
var n = 0;
var left = 9999;
var top = 9999;
var right = -9999;
var bottom = -9999;

CiAssert.noException(
"Bresenham square (low-aligned)",
iterateCenteredSquareAlignLow(0,0,w,h, function(x,y) {
left = M.imin(left, x);
right = M.imax(right, x);
top = M.imin(top, y);
bottom = M.imax(bottom, y);
n++;
})
);

CiAssert.equals( left, Std.int(-w*0.5) );
CiAssert.equals( right, Std.int(-w*0.5) + w - 1 );
CiAssert.equals( top, Std.int(-h*0.5) );
CiAssert.equals( bottom, Std.int(-h*0.5) + h - 1 );
CiAssert.equals( n, w*h );
}

// Centered square (high-aligned)
for(w in [1,2,3,4,7])
for(h in [1,2,3,4,7]) {
var n = 0;
var left = 9999;
var top = 9999;
var right = -9999;
var bottom = -9999;

CiAssert.noException(
"Bresenham square (high-aligned)",
iterateCenteredSquareAlignHigh(0,0,w,h, function(x,y) {
left = M.imin(left, x);
right = M.imax(right, x);
top = M.imin(top, y);
bottom = M.imax(bottom, y);
n++;
})
);

CiAssert.equals( left, Std.int(-w*0.5 + 0.5) );
CiAssert.equals( right, Std.int(-w*0.5 + 0.5) + w - 1 );
CiAssert.equals( top, Std.int(-h*0.5 + 0.5) );
CiAssert.equals( bottom, Std.int(-h*0.5 + 0.5) + h - 1 );
CiAssert.equals( n, w*h );
}

// Circles / discs
CiAssert.equals( Bresenham.getCircle(0,0, 0).length, 1 );
CiAssert.equals( Bresenham.getCircle(0,0, 1).length, 4 );
Expand Down
73 changes: 73 additions & 0 deletions src/dn/Geom.hx
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,23 @@ class Geom {
}


/** Iterate a filled square. For non-even sizes, the square is aligned to the left/top. **/
public static function iterateCenteredSquareAlignLow(centerX:Int, centerY:Int, wid:Int, hei:Int, cb:Int->Int->Void) {
for(y in 0...hei)
for(x in 0...wid)
cb( Std.int(centerX-wid*0.5) + x, Std.int(centerY-hei*0.5) + y );
}

/** Iterate a filled square. For non-even sizes, the square is aligned to the right/bottom. **/
public static function iterateCenteredSquareAlignHigh(centerX:Int, centerY:Int, wid:Int, hei:Int, cb:Int->Int->Void) {
for(y in 0...hei)
for(x in 0...wid)
cb( Std.int(centerX-wid*0.5 + 0.5) + x, Std.int(centerY-hei*0.5 + 0.5) + y );
}




#if deepnightLibsTests
public static function test() {
// Point vs Circle
Expand Down Expand Up @@ -158,6 +175,62 @@ class Geom {

// Rect vs Circle
// TODO


// Centered square (low-aligned)
for(w in [1,2,3,4,7])
for(h in [1,2,3,4,7]) {
var n = 0;
var left = 9999;
var top = 9999;
var right = -9999;
var bottom = -9999;

CiAssert.noException(
"Bresenham square (low-aligned)",
iterateCenteredSquareAlignLow(0,0,w,h, function(x,y) {
left = M.imin(left, x);
right = M.imax(right, x);
top = M.imin(top, y);
bottom = M.imax(bottom, y);
n++;
})
);

CiAssert.equals( left, Std.int(-w*0.5) );
CiAssert.equals( right, Std.int(-w*0.5) + w - 1 );
CiAssert.equals( top, Std.int(-h*0.5) );
CiAssert.equals( bottom, Std.int(-h*0.5) + h - 1 );
CiAssert.equals( n, w*h );
}

// Centered square (high-aligned)
for(w in [1,2,3,4,7])
for(h in [1,2,3,4,7]) {
var n = 0;
var left = 9999;
var top = 9999;
var right = -9999;
var bottom = -9999;

CiAssert.noException(
"Bresenham square (high-aligned)",
iterateCenteredSquareAlignHigh(0,0,w,h, function(x,y) {
left = M.imin(left, x);
right = M.imax(right, x);
top = M.imin(top, y);
bottom = M.imax(bottom, y);
n++;
})
);

CiAssert.equals( left, Std.int(-w*0.5 + 0.5) );
CiAssert.equals( right, Std.int(-w*0.5 + 0.5) + w - 1 );
CiAssert.equals( top, Std.int(-h*0.5 + 0.5) );
CiAssert.equals( bottom, Std.int(-h*0.5 + 0.5) + h - 1 );
CiAssert.equals( n, w*h );
}

}
#end
}

0 comments on commit a529104

Please sign in to comment.