Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Region
- {
- //points assigned to this region
- public List<ulong> AssignedPoints = new List<ulong>();
- //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
- public static uint[] Decode_Point(ulong EncodedPoint)
- {
- uint orig_x = (uint)(EncodedPoint >> 32);
- uint orig_y = (uint)(EncodedPoint & 4294967295);
- return new uint[] { orig_x, orig_y };
- }
- //so x and y are 32 bits, so we can combine them into a ulong of 64 bits
- public static ulong Encode_Point(uint x, uint y)
- {
- ulong EncodedXY = 0 | (ulong)x;
- EncodedXY = EncodedXY << 32;
- EncodedXY = EncodedXY | (ulong)y;
- return EncodedXY;
- }
- //print out the region
- public void print(int XLimit, int YLimit, string name)
- {
- //sort the assigned points
- AssignedPoints.Sort();
- StreamWriter SR = new StreamWriter(new FileStream(name, FileMode.Create));
- for (uint y = 0; y < YLimit; y++)
- {
- for (uint x = 0; x < XLimit; x++)
- {
- ulong point = Encode_Point(x, y);
- if (AssignedPoints.Contains(point))
- {
- SR.Write(".");
- }
- else SR.Write("#");
- }
- SR.WriteLine();
- }
- SR.Close();
- }
- //find the floors adjacent to this region
- public void Find_Adjacent(ulong RootPoint, List<ulong> PointsList)
- {
- //decode x and y values of root point
- uint[] RootXY = Decode_Point(RootPoint);
- uint RootX = RootXY[0];
- uint RootY = RootXY[1];
- //get point to the right of this one
- ulong RightPointEncoded = Encode_Point(RootX + 1, RootY);
- //get the point to the left of this one
- ulong LeftPointEncoded = Encode_Point(RootX - 1, RootY);
- //get the point above this one
- ulong UpPointEncoded = Encode_Point(RootX, RootY - 1);
- //get the point below this one
- ulong DownPointEncoded = Encode_Point(RootX, RootY + 1);
- //if the point to the right of this one exists
- if (PointsList.Contains(RightPointEncoded))
- {
- PointsList.RemoveAt(PointsList.BinarySearch(RightPointEncoded));
- AssignedPoints.Add(RightPointEncoded);
- Find_Adjacent(RightPointEncoded, PointsList);
- }
- //if the point to the left of this one exists
- if (PointsList.Contains(LeftPointEncoded))
- {
- PointsList.RemoveAt(PointsList.BinarySearch(LeftPointEncoded));
- AssignedPoints.Add(LeftPointEncoded);
- Find_Adjacent(LeftPointEncoded, PointsList);
- }
- //if the point above this one exists
- if (PointsList.Contains(UpPointEncoded))
- {
- PointsList.RemoveAt(PointsList.BinarySearch(UpPointEncoded));
- AssignedPoints.Add(UpPointEncoded);
- Find_Adjacent(UpPointEncoded, PointsList);
- }
- //if the point below this one exists
- if (PointsList.Contains(DownPointEncoded))
- {
- PointsList.RemoveAt(PointsList.BinarySearch(DownPointEncoded));
- AssignedPoints.Add(DownPointEncoded);
- Find_Adjacent(DownPointEncoded, PointsList);
- }
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- //encoded locations of floors
- List<ulong> GoodPoints = new List<ulong>();
- //x and y limits for printing
- int XLimit = 0;
- int YLimit = 0;
- //read in the list of good points
- uint Y_Position = 0;
- foreach (string Line in File.ReadLines("Generated.txt"))
- {
- uint X_Position = 0;
- foreach (char ch in Line)
- {
- if (ch == '.') GoodPoints.Add(Region.Encode_Point(X_Position, Y_Position));
- X_Position++;
- }
- Y_Position++;
- XLimit = (int)X_Position;
- uint[] xy = Region.Decode_Point(Region.Encode_Point(X_Position, Y_Position));
- }
- YLimit = (int)Y_Position;
- //sort the list of good points
- GoodPoints.Sort();
- //create List of regions
- List<Region> RegionList = new List<Region>();
- while (GoodPoints.Count != 0)
- {
- //assign the region with the root point
- Region NewRegion = new Region();
- //remove the root point from the good list
- ulong Currentpoint = GoodPoints[0];
- GoodPoints.RemoveAt(0);
- //find adjacent points
- NewRegion.Find_Adjacent(Currentpoint, GoodPoints);
- //add in the current point
- NewRegion.AssignedPoints.Add(Currentpoint);
- foreach (ulong point in NewRegion.AssignedPoints)
- {
- uint[] p = Region.Decode_Point(point);
- }
- RegionList.Add(NewRegion);
- }
- //print out the regions
- for (int i = 0; i < RegionList.Count; i++)
- {
- RegionList[i].print(XLimit, YLimit, i.ToString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement