Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- class HTree
- {
- public static void DrawHTree (int x, int y, int length, int depth) {
- if (length == 0) {
- return;
- }
- DrawHTree(x, y, length, depth, 0);
- }
- public static void DrawHTree (int x, int y, int length, int depth, int currentDepth) {
- if (depth == currentDepth) {
- return;
- }
- int halfLength = length / 2; // 1
- int xLeft = x - halfLength; // -1
- int xRight = x + halfLength; // 1
- int yTop = y + halfLength; // 1
- int yBottom = y - halfLength; // -1
- DrawLine(xLeft, y, xRight, y); // (-1,0) -> (1,0)
- DrawLine(xLeft, yTop, xLeft, yBottom); // (-1,1) -> (-1,-1)
- DrawLine(xRight, yTop, xRight, yBottom); // (1, 1) -> (1,-1)
- int newLength = length / Math.Sqrt(2);
- currentDepth++;
- DrawHTree(xLeft, yTop, newLength, currentDepth); // (-1,1) 1
- DrawHTree(xLeft, yBottom, newLength, currentDepth); // (-1,-1) 1
- DrawHTree(xRight, yTop, newLength, currentDepth); //
- DrawHTree(xRight, yBottom, newLength, currentDepth);
- }
- public static void DrawLine(int x1, int y1, int x2, int y2) {
- Console.WriteLine("(" + x1 + "," + y1 + ")");
- Console.WriteLine("(" + x2 + "," + y2 + ")");
- }
- // if (depth == givenLength) { // 0, 1, 2
- //return;
- //}
- // halfLength = length / 2 => 1
- // (x - halfLength, y) (x + halfLength, y)
- // (x + halfLength, y + halfLength) (x + halfLength, y - halfLength)
- // (x - halfLength, y + halfLength) (x - halfLength, y - halfLength)
- // drawHTree (x + halfLength, y + halfLength, length/Sqrt(2), depth)
- // drawHTree (x + halfLength, y - halfLength, length/Sqrt(2), depth)
- //drawHTree (x - halfLength, y + halfLength, length/Sqrt(2), depth)
- //drawHTree (x - halfLength, y - halfLength, length/Sqrt(2), depth)
- static void Main()
- {
- System.Console.WriteLine("Practice makes Perfect!");
- }
- }
- // x, y (0,0) length (2)
- // if (depth == givenLength) { // 0, 1, 2
- //return;
- //}
- // halfLength = length / 2 => 1
- // (x - halfLength, y) (x + halfLength, y)
- // (x + halfLength, y + halfLength) (x + halfLength, y - halfLength)
- // (x - halfLength, y + halfLength) (x - halfLength, y - halfLength)
- // drawHTree (x + halfLength, y + halfLength, length/Sqrt(2), depth)
- // drawHTree (x + halfLength, y - halfLength, length/Sqrt(2), depth)
- //drawHTree (x - halfLength, y + halfLength, length/Sqrt(2), depth)
- //drawHTree (x - halfLength, y - halfLength, length/Sqrt(2), depth)
- O(4^d) -> 1 + 4 + 4 * 4 + ... + 4^(depth - 1) = 4^depth - 1 /(4- 1)= O(4^depth)
Add Comment
Please, Sign In to add comment