Last active
November 1, 2022 10:13
-
-
Save savaged/bdf64b142b5557daac9d8a6449fcdd36 to your computer and use it in GitHub Desktop.
Fun plotting a circle in the console in a FP style
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
| CircPlotter.PlotCirc(); | |
| static class CircPlotter | |
| { | |
| const int R = 18; | |
| static int H => R * 2; | |
| static void SetDisplayPosition(int x, int y) | |
| { | |
| try | |
| { | |
| Console.SetCursorPosition(x, y); | |
| } | |
| catch (ArgumentOutOfRangeException e) | |
| { | |
| Console.WriteLine(e.Message); | |
| } | |
| } | |
| static void PrepDisplay() | |
| { | |
| Console.Clear(); | |
| SetDisplayPosition(0, 0); | |
| } | |
| static void CloseDisplay() => SetDisplayPosition(0, H * 2); | |
| static double T(int d) => d * Math.PI / 180; | |
| static int Coord(double t, Func<double, double> f) => H + Convert.ToInt32(R * f(t)); | |
| static int X(double t) => Coord(t, Math.Cos); | |
| static int Y(double t) => Coord(t, Math.Sin); | |
| static (int, int) Coords(int d) => (X(T(d)), Y(T(d))); | |
| static (int x, int y)[] CircCoords() => | |
| Enumerable.Range(1, 360).Select(d => Coords(d)).ToArray(); | |
| static void Plot((int x, int y) c) | |
| { | |
| SetDisplayPosition(c.x, c.y); | |
| Console.Write('*'); | |
| } | |
| internal static void PlotCirc() | |
| { | |
| PrepDisplay(); | |
| CircCoords().ToList().ForEach(c => Plot(c)); | |
| CloseDisplay(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment