Skip to content
Browse files

optimized line drawing algorithm for horizontal an vertical lines

  • Loading branch information...
1 parent 8ae1c6d commit b4cfef398b4608b22a0c8c9fae90e03d6e405f74 @FROGGS FROGGS committed Sep 7, 2012
Showing with 24 additions and 15 deletions.
  1. +24 −15 lib/SDL/Surface.pm6
View
39 lib/SDL/Surface.pm6
@@ -75,27 +75,36 @@ class SDL::Surface {
_fill_rect( $!pointer, SDL::Rect.new( $x, $y, 1, 1 ).CArray, $color )
}
+ # http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
method draw_line ( Int $x0 is copy = 0, Int $y0 is copy = 0, Int $x1 is copy = 0, Int $y1 is copy = 0, Int $color = 0xFFFFFFFF ) {
my Int $dx = ($x1 - $x0).abs;
my Int $dy = ($y1 - $y0).abs;
my Int $sx = $x0 < $x1 ?? 1 !! -1;
my Int $sy = $y0 < $y1 ?? 1 !! -1;
my Int $err = $dx - $dy;
-
- while 1 {
- _fill_rect( $!pointer, SDL::Rect.new( $x0, $y0, 1, 1 ).CArray, $color );
- last if $x0 == $x1 && $y0 == $y1;
-
- my Int $e2 = 2 * $err;
-
- if $e2 > -$dy {
- $err = $err - $dy;
- $x0 = $x0 + $sx;
- }
-
- if $e2 < $dx {
- $err = $err + $dx;
- $y0 = $y0 + $sy;
+
+ if $dx == 0 {
+ _fill_rect( $!pointer, SDL::Rect.new( $x0, $y0, 1, $dy ).CArray, $color );
+ }
+ elsif $dy == 0 {
+ _fill_rect( $!pointer, SDL::Rect.new( $x0, $y0, $dx, 1 ).CArray, $color );
+ }
+ else {
+ while 1 {
+ _fill_rect( $!pointer, SDL::Rect.new( $x0, $y0, 1, 1 ).CArray, $color );
+ last if $x0 == $x1 && $y0 == $y1;
+
+ my Int $e2 = 2 * $err;
+
+ if $e2 > -$dy {
+ $err = $err - $dy;
+ $x0 = $x0 + $sx;
+ }
+
+ if $e2 < $dx {
+ $err = $err + $dx;
+ $y0 = $y0 + $sy;
+ }
}
}
}

0 comments on commit b4cfef3

Please sign in to comment.
Something went wrong with that request. Please try again.