Aug 22 2008

Example usage for text in diagrams: eye charts!

Published by Dougal at 10:53 pm under Programming

eye chart This evening I’ve been smoothing out my additions to the Diagrams library I mentioned yesterday, and making an example program to show it off. Well, I think showing it off is a rather strong term. We’ll call it demonstrating which sounds less grand.

The output is the Snellen eye chart you see there. (I didn’t know they were called Snellen charts either, but that’s the wonder of Wikipedia for you.) The code is below:

module Main where
 
import Graphics.Rendering.Diagrams
 
main = renderAs PNG "snellen.png" (Width 400) snellen
 
snellen = vcatA hcenter $ zipWith text sizes letters
 
sizes = [36,24,18,12,9,6,5,4,3]
letters = ["A","D F","H Z P","T X U D","Z A D N H","P N T U H X"
          ,"U A Z N F D T","N P H T A F X U","X D F H P T Z A N"
          ,"F A X T D N H U P Z"]

Quite simply we have two lists — in the first we store all the point sizes from largest to small, and in the second we store all the lines of text. (Painstakingly copied out from one of the images online. No expense spared for you guys!) We zip the two lists together, combining sizes with strings using this function:

text :: Double -> String -> Diagram

which takes a font size and text string and produces a corresponding diagram (black text only, for this function at least). Then we stack our newly created diagrams one above the other, centring each on the page. The thing gets rendered into a 400px width image and written out. Easy!

If anyone has a good idea of examples that I can make (or work towards) with this new feature it would be great to hear. As I hinted at above, there is a facility to change the colour of outline and fill of the text which I didn’t demonstrate here.

Comments Off

Comments are closed at this time.