Created
November 28, 2013 18:17
-
-
Save antonijn/7696197 to your computer and use it in GitHub Desktop.
Dragon curve (memory inefficient)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| namespace dragon | |
| { | |
| internal struct Vector2 | |
| { | |
| public Vector2(double x, double y) | |
| { | |
| this.x = x; | |
| this.y = y; | |
| } | |
| public double x; | |
| public double y; | |
| public static Vector2 operator +(Vector2 l, Vector2 r) | |
| { | |
| return new Vector2(l.x + r.x, l.y + r.y); | |
| } | |
| public static Vector2 operator -(Vector2 l, Vector2 r) | |
| { | |
| return new Vector2(l.x - r.x, l.y - r.y); | |
| } | |
| public Vector2 turn() | |
| { | |
| return new Vector2(y, -x); | |
| } | |
| public override string ToString() | |
| { | |
| return "(" + x.ToString() + ", " + y.ToString() + ")"; | |
| } | |
| } | |
| class Program | |
| { | |
| private static int u(int n) | |
| { | |
| if (n == 0) | |
| { | |
| return 2; | |
| } | |
| return u(n - 1) * 2 - 1; | |
| } | |
| private static Vector2[] v(int n) | |
| { | |
| if (n == 0) | |
| { | |
| return new Vector2[] | |
| { | |
| new Vector2(-1, 0), | |
| new Vector2(0, 0) | |
| }; | |
| } | |
| Vector2[] prev = v(n - 1); | |
| return prev.Take(prev.Length - 1).Concat( | |
| prev.Reverse() | |
| .Select(x => (x - prev.Last()).turn() + prev.Last()) | |
| ).ToArray(); | |
| } | |
| private static void plot(Vector2[] curve) | |
| { | |
| double[] borders = new double[4]; | |
| foreach (Vector2 vec in curve) | |
| { | |
| borders[0] = Math.Min(borders[0], vec.x); | |
| borders[1] = Math.Min(borders[1], vec.y); | |
| borders[2] = Math.Max(borders[2], vec.x); | |
| borders[3] = Math.Max(borders[3], vec.y); | |
| } | |
| int w = (int)(borders[2] - borders[0] + 1) * 2; | |
| int h = (int)(borders[3] - borders[1] + 1) * 2; | |
| char[,] chars = new char[w, h]; | |
| for (int x = 0; x < w; ++x) | |
| { | |
| for (int y = 0; y < h; ++y) | |
| { | |
| chars[x, y] = ' '; | |
| } | |
| } | |
| Vector2 prev = curve.First(); | |
| prev.x -= borders[0]; | |
| prev.y -= borders[1]; | |
| for (int i = 1; i < curve.Length; ++i) | |
| { | |
| Vector2 curr = curve[i]; | |
| curr.x -= borders[0]; | |
| curr.y -= borders[1]; | |
| for (int x = (int)Math.Min(curr.x, prev.x) * 2; x <= (int)Math.Max(curr.x, prev.x) * 2; ++x) | |
| { | |
| for (int y = (int)Math.Min(curr.y, prev.y) * 2; y <= (int)Math.Max(curr.y, prev.y) * 2; ++y) | |
| { | |
| chars[x, y] = '#'; | |
| } | |
| } | |
| prev = curr; | |
| } | |
| StringBuilder builder = new StringBuilder(); | |
| for (int y = 0; y < h; ++y) | |
| { | |
| for (int x = 0; x < w; ++x) | |
| { | |
| builder.Append(chars[x, y]); | |
| } | |
| builder.AppendLine(); | |
| } | |
| System.IO.File.WriteAllText(curve.Length + ".txt", builder.ToString()); | |
| } | |
| static void Main(string[] args) | |
| { | |
| const int numIt = 21; | |
| Vector2[][] iterations = new Vector2[numIt][]; | |
| double[,] borders = new double[numIt, 4]; | |
| for (int i = 0; i < iterations.Length; ++i) | |
| { | |
| iterations[i] = v(i); | |
| plot(iterations[i]); | |
| } | |
| Console.ReadKey(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment