// Takes the mean (x,y) of each instruction block, // and then finds the distance between them using // a^2 + b^2 = c^2 public double Distance(InstructionBlock other) { double a = this.MeanX () - other.MeanX (); double b = this.MeanY () - other.MeanY (); double c = Math.Pow(a, 2) + Math.Pow(b, 2); return Math.Sqrt (c); }
// Accepts a string filename and loads the contents into instructionBlocks public Boolean LoadFile(string filename) { this.filename = filename; using (StreamReader sr = new StreamReader (filename)) { // The line in the file String line; InstructionBlock block = new InstructionBlock (); Instruction parent = null; int countLines = 0; while ((line = sr.ReadLine ()) != null) { ++countLines; Instruction i = StringToInstruction (line); // Base case: it's the beginning of an instruction group if (i.type.Equals ("G00") && i.z.HasValue && i.z > 0) { if (block.instructions.Count > 0) { instructionBlocks.Add (block); } block = new InstructionBlock (); parent = i; } // Set parent, if it's not null if (parent != null) i.addParent (i); // Finally, add the instruction to the block block.addInstruction (i); } // Add the last block, if it's not already added if (! (block == null || instructionBlocks.Contains(block))) { instructionBlocks.Add (block); } Console.WriteLine ("Read " + countLines + " lines."); return true; } return true; }
// Returns an ordered array of the instructions sorted by distance between blocks public ArrayList ShortestPath(InstructionBlock origin) { // Find the index of the origin block int? originIndex = instructionBlocks.IndexOf (origin); // Throw an error if it's not found if (originIndex < 0 || originIndex == null) { throw new ArgumentException ("You did not specify a valid origin"); } // Store the blocks that will be removed ArrayList tempBlocks = (ArrayList)instructionBlocks.Clone (); tempBlocks.Insert (0, tempBlocks[originIndex.Value]); tempBlocks.RemoveAt (originIndex.Value + 1); // This is where the ordered blocks will go ArrayList orderedBlocks = new ArrayList (); // Starts at the origin, goes through each block while (tempBlocks.Count > 1) { // Take out the origin, and then insert it into sorted blocks origin = (InstructionBlock)tempBlocks[0]; tempBlocks.RemoveAt (0); orderedBlocks.Add (origin); // Set the current shortest distance to infinity double distance = Double.PositiveInfinity; InstructionBlock shortest = null; // Examine each reamining neighbor ... if it's shorter, it's the new destination foreach (InstructionBlock destination in tempBlocks) { // If a closer block is found, it's the new destination if (origin.Distance (destination) < distance) { shortest = destination; } } // Remove the shortest block, add it to the front tempBlocks.Remove (shortest); tempBlocks.Insert (0, shortest); } // Add the final block with no neighbors orderedBlocks.Add (tempBlocks[0]); return orderedBlocks; }
public double NewDistance(InstructionBlock other) { double a = this.LastPoint ().x - other.FirstPoint ().x; double b = this.LastPoint ().y - other.FirstPoint ().y; double csquared = Math.Pow (a, 2) + Math.Pow (b, 2); return Math.Sqrt (csquared); }