public static void RenderArray(PaintEventArgs e, ArrayList list) { Graphics g = e.Graphics; for (int i = 0; i < list.Count; i++) { CustomRect temp = (CustomRect)list[i]; g.FillRectangle(temp.brush, temp.x, temp.y, temp.width, temp.height); } }
private static int Partition(ref int[] array, int left, int right, PaintEventArgs e) { Graphics g = e.Graphics; int x = array[right]; int i = (left - 1); for (int j = left; j <= right - 1; ++j) { g.Clear(Color.White); if (array[j] <= x) { ++i; Swap(ref array[i], ref array[j]); //ANIMATION PART g.Clear(Color.White); //Set elements in rectangle array CustomRect current2 = (CustomRect)rectangles[i]; CustomRect next2 = (CustomRect)rectangles[j]; //rectangles[j] = rectangles[j+1]; rectangles[i] = new CustomRect(current2.x, current2.y, current2.width, next2.height, red); rectangles[j] = new CustomRect(next2.x, next2.y, next2.width, current2.height, red); RenderArray(e, rectangles); ResetColor((CustomRect)rectangles[i], (CustomRect)rectangles[j]); RenderArray(e, rectangles); } } Swap(ref array[i + 1], ref array[right]); //ANIMATION PART //Set elements in rectangle array CustomRect current = (CustomRect)rectangles[i + 1]; CustomRect next = (CustomRect)rectangles[right]; //rectangles[j] = rectangles[j+1]; rectangles[i + 1] = new CustomRect(current.x, current.y, current.width, next.height, red); rectangles[right] = new CustomRect(next.x, next.y, next.width, current.height, red); RenderArray(e, rectangles); ResetColor((CustomRect)rectangles[i + 1], (CustomRect)rectangles[right]); RenderArray(e, rectangles); return(i + 1); }
//Animation for BubbleSort public static void BubbleSortAnimation(object sender, PaintEventArgs e, int[] array) { SolidBrush brush = new SolidBrush(Color.Black); Graphics g = e.Graphics; //Let's copy the array so that the original one can still be accessed and doesn't get modified. int[] sorted = array; int temp = 0; g.Clear(Color.White); RenderArray(e, rectangles); for (int i = 0; i < sorted.Length; i++) { for (int j = 0; j < sorted.Length - 1; j++) { g.Clear(Color.White); if (sorted[j] > sorted[j + 1]) { temp = sorted[j + 1]; sorted[j + 1] = sorted[j]; sorted[j] = temp; //ANIMATION PART //Set elements in rectangle array CustomRect current = (CustomRect)rectangles[j]; CustomRect next = (CustomRect)rectangles[j + 1]; //rectangles[j] = rectangles[j+1]; rectangles[j] = new CustomRect(current.x, current.y, current.width, next.height, red); rectangles[j + 1] = new CustomRect(next.x, next.y, next.width, current.height, red); RenderArray(e, rectangles); ResetColor((CustomRect)rectangles[j], (CustomRect)rectangles[j + 1]); RenderArray(e, rectangles); } } } g.Clear(Color.White); RenderArray(e, rectangles); }
internal static void InsertionSortAnimation(object sender, PaintEventArgs e, int[] array) { var p = sender as Panel; Graphics g = e.Graphics; int j; for (int i = 1; i < array.Length; i++) { int value = array[i]; for (j = i - 1; j >= 0 && array[j] > value; j--) { int temp = array[j + 1]; array[j + 1] = array[j]; array[j] = temp; //ANIMATION //Set elements in rectangle array CustomRect current = (CustomRect)rectangles[j]; CustomRect next = (CustomRect)rectangles[j + 1]; rectangles[j] = new CustomRect(current.x, current.y, current.width, next.height, red); rectangles[j + 1] = new CustomRect(next.x, next.y, next.width, current.height, red); g.Clear(Color.White); RenderArray(e, rectangles); ResetColor((CustomRect)rectangles[j], (CustomRect)rectangles[j + 1]); RenderArray(e, rectangles); } array[j + 1] = value; g.Clear(Color.White); RenderArray(e, rectangles); } }
//THIS IS VERY UGLY BUT APPARENTLY NECESSARY public static void ResetColor(CustomRect customRect1, CustomRect customRect2) { customRect1.setBrush(black); customRect2.setBrush(black); }