A stealth-based 2D platformer where you don't have to kill anyone unless you want to. https://www.semicolin.games
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.3 KiB

  1. using Microsoft.Xna.Framework;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace SemiColinGames {
  5. public static class Line {
  6. public static void Rasterize(Point p1, Point p2, IList<Point> result) {
  7. Line.Rasterize(p1.X, p1.Y, p2.X, p2.Y, result);
  8. }
  9. // Rasterizes a line using Bresenham's line-drawing algorithm.
  10. //
  11. // References:
  12. // https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
  13. // http://members.chello.at/~easyfilter/bresenham.html
  14. // http://members.chello.at/~easyfilter/Bresenham.pdf (section 1.6)
  15. public static void Rasterize(int x1, int y1, int x2, int y2, IList<Point> result) {
  16. int dx = Math.Abs(x2 - x1);
  17. int stepX = x1 < x2 ? 1 : -1;
  18. int dy = -Math.Abs(y2 - y1);
  19. int stepY = y1 < y2 ? 1 : -1;
  20. int error = dx + dy;
  21. int errorXY; // Error value e_xy from the PDF.
  22. int i = 0;
  23. result.Clear();
  24. result.Add(new Point(x1, y1));
  25. while (x1 != x2 || y1 != y2) {
  26. i++;
  27. errorXY = 2 * error;
  28. if (errorXY >= dy) { // e_xy + e_x > 0
  29. error += dy;
  30. x1 += stepX;
  31. }
  32. if (errorXY <= dx) { // e_xy + e_y < 0
  33. error += dx;
  34. y1 += stepY;
  35. }
  36. result.Add(new Point(x1, y1));
  37. }
  38. }
  39. }
  40. }