1
- using System . Collections . Generic ;
1
+ using System . Collections ;
2
2
3
3
namespace QRCoder
4
4
{
@@ -11,15 +11,19 @@ private static partial class ModulePlacer
11
11
/// </summary>
12
12
public struct BlockedModules
13
13
{
14
- private readonly List < Rectangle > _blockedModules ;
14
+ private readonly BitArray [ ] _blockedModules ;
15
15
16
16
/// <summary>
17
17
/// Initializes a new instance of the <see cref="BlockedModules"/> struct with a specified capacity.
18
18
/// </summary>
19
19
/// <param name="capacity">The initial capacity of the blocked modules list.</param>
20
- public BlockedModules ( int capacity )
20
+ public BlockedModules ( int size )
21
21
{
22
- _blockedModules = new List < Rectangle > ( capacity ) ;
22
+ _blockedModules = new BitArray [ size ] ;
23
+ for ( int i = 0 ; i < size ; i ++ )
24
+ {
25
+ _blockedModules [ i ] = new BitArray ( size ) ;
26
+ }
23
27
}
24
28
25
29
/// <summary>
@@ -29,7 +33,7 @@ public BlockedModules(int capacity)
29
33
/// <param name="y">The y-coordinate of the module.</param>
30
34
public void Add ( int x , int y )
31
35
{
32
- _blockedModules . Add ( new Rectangle ( x , y , 1 , 1 ) ) ;
36
+ _blockedModules [ y ] [ x ] = true ;
33
37
}
34
38
35
39
/// <summary>
@@ -38,7 +42,13 @@ public void Add(int x, int y)
38
42
/// <param name="rect">The rectangle that defines the blocked module.</param>
39
43
public void Add ( Rectangle rect )
40
44
{
41
- _blockedModules . Add ( rect ) ;
45
+ for ( int y = rect . Y ; y < rect . Y + rect . Height ; y ++ )
46
+ {
47
+ for ( int x = rect . X ; x < rect . X + rect . Width ; x ++ )
48
+ {
49
+ _blockedModules [ y ] [ x ] = true ;
50
+ }
51
+ }
42
52
}
43
53
44
54
/// <summary>
@@ -49,7 +59,7 @@ public void Add(Rectangle rect)
49
59
/// <returns><c>true</c> if the coordinates are blocked; otherwise, <c>false</c>.</returns>
50
60
public bool IsBlocked ( int x , int y )
51
61
{
52
- return IsBlocked ( new Rectangle ( x , y , 1 , 1 ) ) ;
62
+ return _blockedModules [ y ] [ x ] ;
53
63
}
54
64
55
65
/// <summary>
@@ -59,12 +69,13 @@ public bool IsBlocked(int x, int y)
59
69
/// <returns><c>true</c> if the rectangle is blocked; otherwise, <c>false</c>.</returns>
60
70
public bool IsBlocked ( Rectangle r1 )
61
71
{
62
- // Iterate through the list of blocked modules to check for any intersection.
63
- foreach ( var r2 in _blockedModules )
72
+ for ( int y = r1 . Y ; y < r1 . Y + r1 . Height ; y ++ )
64
73
{
65
- // Check if any part of the rectangles overlap.
66
- if ( r2 . X < r1 . X + r1 . Width && r1 . X < r2 . X + r2 . Width && r2 . Y < r1 . Y + r1 . Height && r1 . Y < r2 . Y + r2 . Height )
67
- return true ;
74
+ for ( int x = r1 . X ; x < r1 . X + r1 . Width ; x ++ )
75
+ {
76
+ if ( _blockedModules [ y ] [ x ] )
77
+ return true ;
78
+ }
68
79
}
69
80
return false ;
70
81
}
0 commit comments