static void Main(string[] args) { Application.EnableVisualStyles(); GraphicsForm form = new GraphicsForm(); form.Draw(); Application.Run(form); }
//This is quiet a monolithic function, but it gets the job done static void DrawGraph(int[] solutionsSet, int Max) { //Find Biggest number of solutions (Used for graphing dynamically) int biggest = 0; foreach(int sol in solutionsSet) { if (sol > biggest) biggest = sol; } Application.EnableVisualStyles(); GraphicsForm GFORM = new GraphicsForm(); GFORM.Title = "N-Queens"; int width = 640; int height = 480; int pad = 25; //Defaults to an internal size of 400 x 400 pixels, this can be overridden GFORM.CanvasWidth = width; GFORM.CanvasHeight = height; GFORM.CanvasWidthOffset = 6; // The canvas size is meant to fit inside the window, but at compile it changes slightly GFORM.CanvasHeightOffset = 29; // These offsets can tweak the width and height of the Form (not canvas) // I'm overriding the defaults in this case double X = 0; double Y = 0; double yScale = 0.95*(height - (2*pad))/Math.Log(biggest); double xSpace = (width-(2*pad))/(Max+2.0); double prev_x = -1; // Need to connect previous point to the current one double prev_y = -1; foreach(int sol in solutionsSet) { if(sol != 0) //Cant calculate Log(0) { Y = Math.Log(sol); } double cur_x = pad+(X+1) * (xSpace); double cur_y = (height-pad) -( Y * yScale); GFORM.AddPoint(cur_x ,cur_y, 2); X++; if(prev_x >= 0) { GFORM.AddLine(prev_x,prev_y, cur_x, cur_y,1); } prev_x = cur_x; prev_y =cur_y; } //Draw graph axes GFORM.AddLine(pad,pad,width-pad,pad,1.5); GFORM.AddLine(pad,pad,pad,height-pad,1.5); GFORM.AddLine(pad,height-pad,width-pad,height-pad,1.5); GFORM.AddLine(width-pad,pad,width-pad,height-pad,1.5); // Draw X scale (linear) for(int i =0; i < Max+2; i++) { GFORM.AddLine(pad+(i*xSpace),height-pad,pad+(i*xSpace),height-pad+5); } GFORM.AddText(width/3.0,height-15,"Queens"); // Draw Y scale (logarithmic) int m = 1; int scale =1; int count =0; double y = 0; while((y = Math.Log(m)) <= Math.Log(biggest)) { int l =5; if(count == 0) l = 10; GFORM.AddLine(pad-l,(height-pad)- (y*yScale),pad,(height-pad)-(y*yScale)); m+= scale; // To avoid drawing a million little lines, we bump up the scale every factor of 10 count++; if(count ==10) { count = 0; scale *= 10; } } GFORM.AddText(18,height/3.0,"Log(solutions)",90); // Tell the form to render GFORM.Draw(); IO.Write("Rendering..."); Application.Run(GFORM); // It could be possible to offload this to another thread. }