Skip to content

Instantly share code, notes, and snippets.

@einarwh
Forked from bjartwolf/diamond.fsx
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save einarwh/8f4a8fa32852aa89bc09 to your computer and use it in GitHub Desktop.
Save einarwh/8f4a8fa32852aa89bc09 to your computer and use it in GitHub Desktop.
Tweaks just because.
open System
// We can not work with radius of the diamond outside 0 to 25
let createRadius i =
match i with
| i when i >= 0 && i < 26 -> Some i
| _ -> None
let radius = createRadius 20 |> Option.get
let axis = [|-radius .. radius|]
let TaxicabNorm (x, y) =
abs x + abs y
let indexedLetters =
['A' .. 'Z'] |> Seq.mapi (fun i l -> i,l)
let findLetter y =
indexedLetters |> Seq.find(fun (i, _) -> i = y) |> snd
let createAllPoints vector =
seq { for x in vector do for y in vector do yield (x, y) }
let diamond = createAllPoints axis |> Seq.filter (fun p -> TaxicabNorm p = radius)
let axisTo2D axis =
[| for _ in axis -> (Array.map (fun _ -> '.') axis) |]
let drawing = axisTo2D axis
let findIndexInAxis elem axis =
Array.findIndex (fun x -> x = elem) axis
for (x, y) in diamond do
let yindex = findIndexInAxis y axis
let xindex = findIndexInAxis x axis
drawing.[xindex].[yindex] <- abs y |> findLetter
let printArray (arr: char array) = new string (arr) |> sprintf "%A" |> Console.WriteLine
for line in drawing do
printArray line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment