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.

45 lines
1.4 KiB

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