Advertisement
Guest User

FloodFill?

a guest
Aug 9th, 2012
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.05 KB | None | 0 0
  1. public class Region
  2.         {
  3.             //points assigned to this region
  4.             public List<ulong> AssignedPoints = new List<ulong>();
  5.  
  6.             //decode a long into it's original x and y values, shift to right 32 to get x, AND with 32 1's in binary to get y
  7.             public static uint[] Decode_Point(ulong EncodedPoint)
  8.             {
  9.                 uint orig_x = (uint)(EncodedPoint >> 32);
  10.                 uint orig_y = (uint)(EncodedPoint & 4294967295);
  11.                 return new uint[] { orig_x, orig_y };
  12.             }
  13.  
  14.             //so x and y are 32 bits, so we can combine them into a ulong of 64 bits
  15.             public static ulong Encode_Point(uint x, uint y)
  16.             {
  17.                 ulong EncodedXY = 0 | (ulong)x;
  18.                 EncodedXY = EncodedXY << 32;
  19.                 EncodedXY = EncodedXY | (ulong)y;
  20.                 return EncodedXY;
  21.             }
  22.  
  23.             //print out the region
  24.             public void print(int XLimit, int YLimit, string name)
  25.             {
  26.                 //sort the assigned points
  27.                 AssignedPoints.Sort();
  28.  
  29.                 StreamWriter SR = new StreamWriter(new FileStream(name, FileMode.Create));
  30.  
  31.                 for (uint y = 0; y < YLimit; y++)
  32.                 {
  33.                     for (uint x = 0; x < XLimit; x++)
  34.                     {
  35.                         ulong point = Encode_Point(x, y);
  36.                         if (AssignedPoints.Contains(point))
  37.                         {
  38.                             SR.Write(".");
  39.                         }
  40.                         else SR.Write("#");
  41.                     }
  42.                     SR.WriteLine();
  43.                 }
  44.                 SR.Close();
  45.             }
  46.  
  47.             //find the floors adjacent to this region
  48.             public void Find_Adjacent(ulong RootPoint, List<ulong> PointsList)
  49.             {
  50.  
  51.                
  52.                 //decode x and y values of root point
  53.                 uint[] RootXY = Decode_Point(RootPoint);
  54.                 uint RootX = RootXY[0];
  55.                 uint RootY = RootXY[1];
  56.  
  57.                 //get point to the right of this one
  58.                 ulong RightPointEncoded = Encode_Point(RootX + 1, RootY);
  59.                 //get the point to the left of this one
  60.                 ulong LeftPointEncoded = Encode_Point(RootX - 1, RootY);
  61.                 //get the point above this one
  62.                 ulong UpPointEncoded = Encode_Point(RootX, RootY - 1);
  63.                 //get the point below this one
  64.                 ulong DownPointEncoded = Encode_Point(RootX, RootY + 1);
  65.  
  66.  
  67.                 //if the point to the right of this one exists
  68.                 if (PointsList.Contains(RightPointEncoded))
  69.                 {
  70.                     PointsList.RemoveAt(PointsList.BinarySearch(RightPointEncoded));
  71.                     AssignedPoints.Add(RightPointEncoded);
  72.                     Find_Adjacent(RightPointEncoded, PointsList);
  73.                 }
  74.  
  75.                 //if the point to the left of this one exists
  76.                 if (PointsList.Contains(LeftPointEncoded))
  77.                 {
  78.                     PointsList.RemoveAt(PointsList.BinarySearch(LeftPointEncoded));
  79.                     AssignedPoints.Add(LeftPointEncoded);
  80.                     Find_Adjacent(LeftPointEncoded, PointsList);
  81.  
  82.                 }
  83.  
  84.                 //if the point above this one exists
  85.                 if (PointsList.Contains(UpPointEncoded))
  86.                 {
  87.                     PointsList.RemoveAt(PointsList.BinarySearch(UpPointEncoded));
  88.                     AssignedPoints.Add(UpPointEncoded);
  89.                     Find_Adjacent(UpPointEncoded, PointsList);
  90.  
  91.                 }
  92.  
  93.                 //if the point below this one exists
  94.                 if (PointsList.Contains(DownPointEncoded))
  95.                 {
  96.                     PointsList.RemoveAt(PointsList.BinarySearch(DownPointEncoded));
  97.                     AssignedPoints.Add(DownPointEncoded);
  98.                     Find_Adjacent(DownPointEncoded, PointsList);
  99.                 }
  100.             }
  101.  
  102.         }
  103.  
  104.  
  105.         private void button2_Click(object sender, EventArgs e)
  106.         {
  107.             //encoded locations of floors
  108.             List<ulong> GoodPoints = new List<ulong>();
  109.  
  110.             //x and y limits for printing
  111.             int XLimit = 0;
  112.             int YLimit = 0;
  113.  
  114.             //read in the list of good points
  115.             uint Y_Position = 0;
  116.             foreach (string Line in File.ReadLines("Generated.txt"))
  117.             {
  118.                 uint X_Position = 0;
  119.                 foreach (char ch in Line)
  120.                 {
  121.                     if (ch == '.') GoodPoints.Add(Region.Encode_Point(X_Position, Y_Position));
  122.                     X_Position++;
  123.                 }
  124.                 Y_Position++;
  125.                 XLimit = (int)X_Position;
  126.  
  127.                 uint[] xy = Region.Decode_Point(Region.Encode_Point(X_Position, Y_Position));
  128.  
  129.             }
  130.             YLimit = (int)Y_Position;
  131.  
  132.             //sort the list of good points
  133.             GoodPoints.Sort();
  134.  
  135.             //create List of regions
  136.             List<Region> RegionList = new List<Region>();
  137.  
  138.             while (GoodPoints.Count != 0)
  139.             {
  140.                 //assign the region with the root point
  141.                 Region NewRegion = new Region();
  142.                 //remove the root point from the good list
  143.                 ulong Currentpoint = GoodPoints[0];
  144.                 GoodPoints.RemoveAt(0);
  145.                 //find adjacent points
  146.                 NewRegion.Find_Adjacent(Currentpoint, GoodPoints);
  147.                 //add in the current point
  148.                 NewRegion.AssignedPoints.Add(Currentpoint);
  149.  
  150.                 foreach (ulong point in NewRegion.AssignedPoints)
  151.                 {
  152.                     uint[] p = Region.Decode_Point(point);
  153.                 }
  154.                 RegionList.Add(NewRegion);
  155.             }
  156.  
  157.             //print out the regions
  158.             for (int i = 0; i < RegionList.Count; i++)
  159.             {
  160.                 RegionList[i].print(XLimit, YLimit, i.ToString());
  161.             }
  162.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement