@@ -10,122 +10,122 @@ namespace BenMakesGames.PlayPlayMini.GraphicsExtensions;
1010/// </summary>
1111public static class LineExtensions
1212{
13- /// <summary>
14- /// Draws a horizontal line from x1 to x2 at height y.
15- /// </summary>
1613 /// <param name="graphics">The graphics manager instance.</param>
17- /// <param name="x1">Starting x coordinate.</param>
18- /// <param name="x2">Ending x coordinate.</param>
19- /// <param name="y">Y coordinate of the line.</param>
20- /// <param name="color">Color of the line.</param>
21- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
22- public static void DrawRow ( this GraphicsManager graphics , int x1 , int x2 , int y , Color color )
23- => graphics . DrawFilledRectangle ( x1 , y , x2 - x1 + 1 , 1 , color ) ;
24-
25- /// <summary>
26- /// Draws a vertical line from y1 to y2 at position x.
27- /// </summary>
28- /// <param name="graphics">The graphics manager instance.</param>
29- /// <param name="x">X coordinate of the line.</param>
30- /// <param name="y1">Starting y coordinate.</param>
31- /// <param name="y2">Ending y coordinate.</param>
32- /// <param name="color">Color of the line.</param>
33- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
34- public static void DrawColumn ( this GraphicsManager graphics , int x , int y1 , int y2 , Color color )
35- => graphics . DrawFilledRectangle ( x , y1 , 1 , y2 - y1 + 1 , color ) ;
36-
37- /// <summary>
38- /// Draws a line between two Vector2 points.
39- /// </summary>
40- /// <param name="graphics">The graphics manager instance.</param>
41- /// <param name="start">Starting point of the line.</param>
42- /// <param name="end">Ending point of the line.</param>
43- /// <param name="color">Color of the line.</param>
44- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
45- public static void DrawLine ( this GraphicsManager graphics , Vector2 start , Vector2 end , Color color )
46- => graphics . DrawLine ( ( int ) start . X , ( int ) start . Y , ( int ) end . X , ( int ) end . Y , color ) ;
47-
48- /// <summary>
49- /// Draws a line between two points using a modified Bresenham's line algorithm.
50- /// </summary>
51- /// <remarks>
52- /// This implementation minimizes MonoGame Draw calls by drawing line segments.
53- /// </remarks>
54- /// <param name="graphics">The graphics manager instance.</param>
55- /// <param name="x1">Starting x coordinate.</param>
56- /// <param name="y1">Starting y coordinate.</param>
57- /// <param name="x2">Ending x coordinate.</param>
58- /// <param name="y2">Ending y coordinate.</param>
59- /// <param name="color">Color of the line.</param>
60- public static void DrawLine ( this GraphicsManager graphics , int x1 , int y1 , int x2 , int y2 , Color color )
14+ extension ( GraphicsManager graphics )
6115 {
62- var w = x2 - x1 ;
16+ /// <summary>
17+ /// Draws a horizontal line from x1 to x2 at height y.
18+ /// </summary>
19+ /// <param name="x1">Starting x coordinate.</param>
20+ /// <param name="x2">Ending x coordinate.</param>
21+ /// <param name="y">Y coordinate of the line.</param>
22+ /// <param name="color">Color of the line.</param>
23+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
24+ public void DrawRow ( int x1 , int x2 , int y , Color color )
25+ => graphics . DrawFilledRectangle ( x1 , y , x2 - x1 + 1 , 1 , color ) ;
6326
64- if ( w == 0 )
65- {
66- graphics . DrawColumn ( x1 , y1 , y2 , color ) ;
67- return ;
68- }
27+ /// <summary>
28+ /// Draws a vertical line from y1 to y2 at position x.
29+ /// </summary>
30+ /// <param name="x">X coordinate of the line.</param>
31+ /// <param name="y1">Starting y coordinate.</param>
32+ /// <param name="y2">Ending y coordinate.</param>
33+ /// <param name="color">Color of the line.</param>
34+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
35+ public void DrawColumn ( int x , int y1 , int y2 , Color color )
36+ => graphics . DrawFilledRectangle ( x , y1 , 1 , y2 - y1 + 1 , color ) ;
6937
70- var h = y2 - y1 ;
38+ /// <summary>
39+ /// Draws a line between two Vector2 points.
40+ /// </summary>
41+ /// <param name="start">Starting point of the line.</param>
42+ /// <param name="end">Ending point of the line.</param>
43+ /// <param name="color">Color of the line.</param>
44+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
45+ public void DrawLine ( Vector2 start , Vector2 end , Color color )
46+ => graphics . DrawLine ( ( int ) start . X , ( int ) start . Y , ( int ) end . X , ( int ) end . Y , color ) ;
7147
72- if ( h == 0 )
48+ /// <summary>
49+ /// Draws a line between two points using a modified Bresenham's line algorithm.
50+ /// </summary>
51+ /// <remarks>
52+ /// This implementation minimizes MonoGame Draw calls by drawing line segments.
53+ /// </remarks>
54+ /// <param name="x1">Starting x coordinate.</param>
55+ /// <param name="y1">Starting y coordinate.</param>
56+ /// <param name="x2">Ending x coordinate.</param>
57+ /// <param name="y2">Ending y coordinate.</param>
58+ /// <param name="color">Color of the line.</param>
59+ public void DrawLine ( int x1 , int y1 , int x2 , int y2 , Color color )
7360 {
74- graphics . DrawRow ( x1 , x2 , y1 , color ) ;
75- return ;
76- }
61+ var w = x2 - x1 ;
7762
78- var dy2 = 0 ;
79- var dx1 = w < 0 ? - 1 : 1 ;
80- var dy1 = h < 0 ? - 1 : 1 ;
81- var dx2 = w < 0 ? - 1 : 1 ;
82- var longest = Math . Abs ( w ) ;
83- var shortest = Math . Abs ( h ) ;
84- if ( ! ( longest > shortest ) )
85- {
86- ( shortest , longest ) = ( longest , shortest ) ;
87- dy2 = h < 0 ? - 1 : 1 ;
88- dx2 = 0 ;
89- }
63+ if ( w == 0 )
64+ {
65+ graphics . DrawColumn ( x1 , y1 , y2 , color ) ;
66+ return ;
67+ }
9068
91- var numerator = longest >> 1 ;
92- var oldX = x1 ;
93- var oldY = y1 ;
69+ var h = y2 - y1 ;
9470
95- for ( var i = 0 ; i <= longest ; i ++ )
96- {
97- numerator += shortest ;
98- if ( ! ( numerator < longest ) )
71+ if ( h == 0 )
9972 {
100- numerator -= longest ;
101- x1 += dx1 ;
102- y1 += dy1 ;
73+ graphics . DrawRow ( x1 , x2 , y1 , color ) ;
74+ return ;
75+ }
10376
104- if ( ( x1 != oldX && y1 != oldY ) || i == longest )
105- {
106- var rectX = Math . Min ( oldX , x1 - dx1 ) ;
107- var rectY = Math . Min ( oldY , y1 - dy1 ) ;
108- var rectW = Math . Abs ( x1 - dx1 - oldX ) + 1 ;
109- var rectH = Math . Abs ( y1 - dy1 - oldY ) + 1 ;
110- graphics . DrawFilledRectangle ( rectX , rectY , rectW , rectH , color ) ;
111- oldX = x1 ;
112- oldY = y1 ;
113- }
77+ var dy2 = 0 ;
78+ var dx1 = w < 0 ? - 1 : 1 ;
79+ var dy1 = h < 0 ? - 1 : 1 ;
80+ var dx2 = w < 0 ? - 1 : 1 ;
81+ var longest = Math . Abs ( w ) ;
82+ var shortest = Math . Abs ( h ) ;
83+ if ( ! ( longest > shortest ) )
84+ {
85+ ( shortest , longest ) = ( longest , shortest ) ;
86+ dy2 = h < 0 ? - 1 : 1 ;
87+ dx2 = 0 ;
11488 }
115- else
89+
90+ var numerator = longest >> 1 ;
91+ var oldX = x1 ;
92+ var oldY = y1 ;
93+
94+ for ( var i = 0 ; i <= longest ; i ++ )
11695 {
117- x1 += dx2 ;
118- y1 += dy2 ;
96+ numerator += shortest ;
97+ if ( ! ( numerator < longest ) )
98+ {
99+ numerator -= longest ;
100+ x1 += dx1 ;
101+ y1 += dy1 ;
119102
120- if ( ( x1 != oldX && y1 != oldY ) || i == longest )
103+ if ( ( x1 != oldX && y1 != oldY ) || i == longest )
104+ {
105+ var rectX = Math . Min ( oldX , x1 - dx1 ) ;
106+ var rectY = Math . Min ( oldY , y1 - dy1 ) ;
107+ var rectW = Math . Abs ( x1 - dx1 - oldX ) + 1 ;
108+ var rectH = Math . Abs ( y1 - dy1 - oldY ) + 1 ;
109+ graphics . DrawFilledRectangle ( rectX , rectY , rectW , rectH , color ) ;
110+ oldX = x1 ;
111+ oldY = y1 ;
112+ }
113+ }
114+ else
121115 {
122- var rectX = Math . Min ( oldX , x1 - dx2 ) ;
123- var rectY = Math . Min ( oldY , y1 - dy2 ) ;
124- var rectW = Math . Abs ( x1 - dx2 - oldX ) + 1 ;
125- var rectH = Math . Abs ( y1 - dy2 - oldY ) + 1 ;
126- graphics . DrawFilledRectangle ( rectX , rectY , rectW , rectH , color ) ;
127- oldX = x1 ;
128- oldY = y1 ;
116+ x1 += dx2 ;
117+ y1 += dy2 ;
118+
119+ if ( ( x1 != oldX && y1 != oldY ) || i == longest )
120+ {
121+ var rectX = Math . Min ( oldX , x1 - dx2 ) ;
122+ var rectY = Math . Min ( oldY , y1 - dy2 ) ;
123+ var rectW = Math . Abs ( x1 - dx2 - oldX ) + 1 ;
124+ var rectH = Math . Abs ( y1 - dy2 - oldY ) + 1 ;
125+ graphics . DrawFilledRectangle ( rectX , rectY , rectW , rectH , color ) ;
126+ oldX = x1 ;
127+ oldY = y1 ;
128+ }
129129 }
130130 }
131131 }
0 commit comments