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.

46 lines
1.4 KiB

  1. using Microsoft.Xna.Framework;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace SemiColinGames {
  5. class Line {
  6. public static Point[] Rasterize(Point p1, Point p2) {
  7. return Line.Rasterize(p1.X, p1.Y, p2.X, p2.Y);
  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 Point[] Rasterize(int x1, int y1, int x2, int y2) {
  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 = 0; // Error value e_xy from the PDF.
  22. // The size of the output is the size of the longer dimension, plus one.
  23. int resultSize = Math.Max(dx, -dy) + 1;
  24. var result = new Point[resultSize];
  25. int i = 0;
  26. result[0] = new Point(x1, y1);
  27. while (x1 != x2 || y1 != y2) {
  28. i++;
  29. errorXY = 2 * error;
  30. if (errorXY >= dy) { // e_xy + e_x > 0
  31. error += dy;
  32. x1 += stepX;
  33. }
  34. if (errorXY <= dx) { // e_xy + e_y < 0
  35. error += dx;
  36. y1 += stepY;
  37. }
  38. result[i] = new Point(x1, y1);
  39. }
  40. return result;
  41. }
  42. }
  43. }