public static List <Vector2> bresenham(int x0, int y0, int x1, int y1) { List <Vector2> result = new List <Vector2>(); if (y0 == y1) { if (x0 > x1) { Additional_Math.swap(ref x0, ref x1); } for (int x = x0; x < x1; x++) { result.Add(new Vector2(x, y0)); } result.Add(new Vector2(x1, y1)); } else if (x0 == x1) { if (y0 > y1) { Additional_Math.swap(ref y0, ref y1); } for (int y = y0; y < y1; y++) { result.Add(new Vector2(x0, y)); } result.Add(new Vector2(x1, y1)); } else { bool steep = Math.Abs(y1 - y0) > Math.Abs(x1 - x0); if (steep) { Additional_Math.swap(ref x0, ref y0); Additional_Math.swap(ref x1, ref y1); } if (x0 > x1) { Additional_Math.swap(ref x0, ref x1); Additional_Math.swap(ref y0, ref y1); } int delta_x = x1 - x0; int delta_y = (int)Math.Abs(y1 - y0); int error = delta_x / 2; int ystep = y0 < y1 ? 1 : -1; int y = y0; for (int x = x0; x <= x1; x++) { result.Add(steep ? new Vector2(y, x) : new Vector2(x, y)); error -= delta_y; if (error < 0) { y += ystep; error += delta_x; } } } return(result); }
public static List <Vector2> bresenham_supercover(int x0, int y0, int x1, int y1) { Vector2 base_loc = new Vector2(x0, y0); List <Vector2> result = new List <Vector2>(); if (y0 == y1) { if (x0 > x1) { Additional_Math.swap(ref x0, ref x1); } for (int x = x0; x < x1; x++) { result.Add(new Vector2(x, y0)); } result.Add(new Vector2(x1, y1)); } else if (x0 == x1) { if (y0 > y1) { Additional_Math.swap(ref y0, ref y1); } for (int y = y0; y < y1; y++) { result.Add(new Vector2(x0, y)); } result.Add(new Vector2(x1, y1)); } else { bool steep = Math.Abs(y1 - y0) > Math.Abs(x1 - x0); if (steep) { Additional_Math.swap(ref x0, ref y0); Additional_Math.swap(ref x1, ref y1); } if (x0 > x1) { Additional_Math.swap(ref x0, ref x1); Additional_Math.swap(ref y0, ref y1); } int delta_x = (x1 - x0), delta_x2 = delta_x * 2; int delta_y = (int)Math.Abs(y1 - y0), delta_y2 = delta_y * 2; int error = delta_x; int error_prev = error; int ystep = y0 < y1 ? 1 : -1; int y = y0; result.Add(steep ? new Vector2(y, x0) : new Vector2(x0, y)); for (int x = x0 + 1; x <= x1; x++) { error += delta_y2; if (error > delta_x2) { y += ystep; error -= delta_x2; if (error + error_prev < delta_x2) { result.Add(steep ? new Vector2(y - ystep, x) : new Vector2(x, y - ystep)); } else if (error + error_prev > delta_x2) { result.Add(steep ? new Vector2(y, x - 1) : new Vector2(x - 1, y)); } else { result.Add(steep ? new Vector2(y, x - 1) : new Vector2(x - 1, y)); result.Add(steep ? new Vector2(y - ystep, x) : new Vector2(x, y - ystep)); } } result.Add(steep ? new Vector2(y, x) : new Vector2(x, y)); error_prev = error; } //result.Add(steep ? new Vector2(y, x1) : new Vector2(x1, y)); } if (result[0] != base_loc) { result.Reverse(); } return(result); }