Add More Primitives
00:00 Adding More Primitives. Two other primitives closely related to a line are SVG polylines and polygons, which extend the idea of a line. They’re both tuples of two or more points, which look nearly identical.
The difference is that a polygon is closed, meaning that it connects the first and last point, while the polyline isn’t, leaving the last point unconnected. In case you’re wondering, the
points parameter must be passed as a keyword argument to avoid conflating it with the
value positional argument.
Sometimes it may be more convenient to combine existing lines instead of individual points, especially when they don’t form a continuous polyline. That’s when a
DisjointLines primitive comes in handy.
01:30 Note that this is merely a logical collection of lines and has no SVG equivalent. Other, more specific SVG primitives that you’ll be interested in include a rectangle and a text element. You implement them as data classes rather than tuples or named tuples because they’re not inherently sequences of elements.
Last but not least, you’ll apply the null object pattern again by implementing a dummy
NullPrimitive, which returns an empty string. This will let you avoid checking for a special case when there are no primitives to represent something in SVG, ultimately leading to cleaner and more readable code.
Become a Member to join the conversation.