Created
April 3, 2018 02:33
-
-
Save GeePawHill/11442fbe64187fa2ec50e9ed277d7866 to your computer and use it in GitHub Desktop.
A real-world TDD "what to do?"
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
| /* This is the Box. Note that it's extremely similar to the Stroke, essentially only changing Stroke's behavior by making it | |
| four-way instead of one-way. I see no other way to code it without re-factoring the four-level tree. | |
| This is simple. I haven't run it yet, but I bet it runs. | |
| Should I have tested it? The expense of testing it *without* the factoring is dauntingly high. The factoring is on the agenda, and | |
| I know how to do it. But it will take a good deal of work, as Stroke and Box are part of a half-dozen entries at their level of the | |
| tree, with another half-dozen at the third, and a good dozen at the fourth. (The top level isn't shown, and is actually the most | |
| rational and composite-like.) | |
| */ | |
| package org.geepawhill.contentment.actors; | |
| import java.util.Random; | |
| import org.geepawhill.contentment.actor.GenericActor; | |
| import org.geepawhill.contentment.actor.ScriptWorld; | |
| import org.geepawhill.contentment.format.Format; | |
| import org.geepawhill.contentment.fragments.Curve; | |
| import org.geepawhill.contentment.geometry.Bezier; | |
| import org.geepawhill.contentment.geometry.PointPair; | |
| import org.geepawhill.contentment.step.AtomStep; | |
| import org.geepawhill.contentment.timing.Timing; | |
| public class Box extends GenericActor | |
| { | |
| private final Curve curves[]; | |
| private Random random; | |
| public Box(ScriptWorld world, PointPair points) | |
| { | |
| super(world); | |
| random = new Random(); | |
| this.curves = new Curve[4]; | |
| this.curves[0] = jiggle(points.northLine()); | |
| this.curves[1] = jiggle(points.eastLine()); | |
| this.curves[2] = jiggle(points.southLine()); | |
| this.curves[3] = jiggle(points.westLine()); | |
| } | |
| public Curve jiggle(PointPair points) | |
| { | |
| double variance = points.distance() * .1; | |
| Bezier chosen = new Bezier(points.from, points.along(random.nextDouble()).jiggle(random, 1d, variance), | |
| points.along(random.nextDouble()).jiggle(random, 1d, variance), points.to); | |
| return new Curve(groupSource(), () -> chosen, Format.DEFAULT); | |
| } | |
| public Box format(Format format) | |
| { | |
| for (Curve curve : curves) | |
| { | |
| curve.format(format); | |
| } | |
| return this; | |
| } | |
| @Override | |
| public Box draw(double ms) | |
| { | |
| for (Curve curve : curves) | |
| { | |
| world.add(new AtomStep(Timing.ms(ms), curve)); | |
| } | |
| return this; | |
| } | |
| } |
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
| /* | |
| Here's the deal. This is the original 'Stroke', which draws a stroke. Note that GenericActor, Curve, and Bezier are all tested | |
| only under fire -- they have shipped for months and their issues seem worked out. Curve is largely untestable in its current factoring. | |
| Overall, the factoring of the system is weird. The next file is my answer to Box, which is conceptually just four Strokes, but you'll | |
| note that it's not just a little container with four Strokes in it. Instead, it's a container with four of the *parts* of Stroke that | |
| do the drawing. This is because the whole system is a kind of four-level tree, with each level comprised of type-specific elements | |
| rather than in an classic composite fashion. | |
| That factoring is broken. But like most folks, I work for a living. I need to ship a video this week, and I need a Box. | |
| */ | |
| package org.geepawhill.contentment.actors; | |
| import java.util.Random; | |
| import org.geepawhill.contentment.actor.GenericActor; | |
| import org.geepawhill.contentment.actor.ScriptWorld; | |
| import org.geepawhill.contentment.format.Format; | |
| import org.geepawhill.contentment.fragments.Curve; | |
| import org.geepawhill.contentment.geometry.Bezier; | |
| import org.geepawhill.contentment.geometry.PointPair; | |
| import org.geepawhill.contentment.step.AtomStep; | |
| import org.geepawhill.contentment.timing.Timing; | |
| public class Stroke extends GenericActor { | |
| private final Bezier bezier; | |
| private final Curve atom; | |
| private Random random; | |
| public Stroke(ScriptWorld world, PointPair points) { | |
| super(world); | |
| random = new Random(); | |
| this.bezier = jiggle(random, points); | |
| this.atom = new Curve(groupSource(), () -> bezier, Format.DEFAULT); | |
| } | |
| public Bezier jiggle(Random random, PointPair points) { | |
| double variance = points.distance() * .1; | |
| Bezier chosen = new Bezier(points.from, points.along(random.nextDouble()).jiggle(random, 1d, variance), | |
| points.along(random.nextDouble()).jiggle(random, 1d, variance), points.to); | |
| return chosen; | |
| } | |
| public Stroke format(Format format) { | |
| atom.format(format); | |
| return this; | |
| } | |
| @Override | |
| public Stroke draw(double ms) { | |
| world.add(new AtomStep(Timing.ms(ms), atom)); | |
| return this; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment