Skip to content

Instantly share code, notes, and snippets.

@savaged
Created November 1, 2022 10:29
Show Gist options
  • Select an option

  • Save savaged/91fbe78427b8ad34fd956fd3a156be01 to your computer and use it in GitHub Desktop.

Select an option

Save savaged/91fbe78427b8ad34fd956fd3a156be01 to your computer and use it in GitHub Desktop.
Fun plotting a circle in the console
open System
let R = 18
let H = R * 2
let setDisplayPosition (x, y) =
try
Console.SetCursorPosition(x, y)
with
| :? ArgumentOutOfRangeException -> printfn "Attempted to set cursor out of range!"
let prepDisplay =
Console.Clear()
setDisplayPosition(0, 0)
let closeDisplay = setDisplayPosition(0, H * 2) // TODO figure out why the console truncates the 'circle'
let T d = d * Math.PI / 180.00
let coord t (f: double -> double) = H + Convert.ToInt32((double R) * f(t))
let X t = coord t Math.Cos
let Y t = coord t Math.Sin
let coords d = X(T(d)), Y(T(d))
let circCoords = [1..360] |> List.map (fun d -> coords(d))
let plot (x, y) =
setDisplayPosition(x, y)
Console.Write('*')
let plotCirc =
prepDisplay
circCoords |> List.iter (fun (x, y) -> plot(x, y))
closeDisplay
plotCirc
@savaged

savaged commented Nov 1, 2022

Copy link
Copy Markdown
Author

Need to work out why the F# version truncates the circle whereas the C# version does not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment