timepackage and spits out an SVG of a clock with all the hands - hour, minute and second - pointing in the right direction. How hard can that be?
y1for this line) is the same for each hand of the clock. The numbers that need to change for each hand of the clock - the parameters to whatever builds the SVG - are the
y2attributes. We'll need an X and a Y for each of the hands of the clock.
text/templatepackage, or we could just send bytes into a
bytes.Bufferor a writer. But we know we'll need those numbers, so let's focus on testing something that creates them.
Pointwhere the tip of the second hand should go, and a function to get it.
(360 / 60 ) * 37 = 222, but it's easier just to remember that it's
37/60of a complete rotation.
Cos returns the cosine of the radian argument x.
clockfacepackage; they may never get exported, and they may get deleted (or moved) once I have a better grip on what's going on.
mathpackage! If a full turn of a circle is 2π radians, we know that halfway round should just be π radians.
math.Piprovides us with a value for π.
testNameconverts a time into a digital watch format (HH:MM:SS), and
time.Timeusing only the parts we actually care about (again, hours, minutes and seconds).[^1] Here they are:
float64) and we should now have all the tests passing...
secondsInRadiansfunction. By dividing
math.Piby 30 and then by multiplying it by 30 we've ended up with a number that's no longer the same as
+Inf(infinity). Dividing by +Inf seems to result in zero and we can see this with the following:
math/bigpackage. But given the objective is to draw an SVG and not land on the moon landings I think we can live with a bit of fuzziness.
Points- they'll work if the X and Y elements are within 0.0000001 of each other. That's still pretty accurate.
clockfacepackage directory, called (confusingly),
clockface. In there we'll put the
mainpackage that will create the binary that will build an SVG:
main.go, you'll start with this code but change the import for the clockface package to point at your own version:
os.Stdout- one string at a time.
SecondHandfunction is super tied to being an SVG... without mentioning SVGs or actually producing an SVG...
SVGWritercontain things that look like the sort of SVG tag we're expecting for a particular time. For instance:
zeka program that will automate all of that hard work for us. Even better, there's an online version at https://www.onlinetool.io/xmltogo/. Just paste the SVG from the top of the file into one box and - bam - out pops:
SVG) but it's definitely good enough to start us off. Paste the struct into the
clockface_testfile and let's write a test with it:
Unmarshallit into an
Svg. We then look at each
Svgto see if any of them have the expected
Y2values. If we get a match we return early (passing the test); if not we fail with a (hopefully) informative message.
%fformat directive is printing our coordinates to the default level of precision - six decimal places. We should be explicit as to what level of precision we're expecting for the coordinates. Let's say three decimal places.
x1values, for instance?
x1etc. aren't really
stringsare they? They're numbers!
styleof the hand? Or, for that matter, the empty
Textnode that's been generated by
Svgstruct, and the tests, to sharpen everything up.
float64s instead of
SVGbecause it's the right thing to do.
containsLine(line Line, lines Line) boolto really make these tests shine: