All Projects → alskipp → Swift Diagram Playgrounds

alskipp / Swift Diagram Playgrounds

Drawing diagrams in Swift using a recursive enum data structure

Programming Languages

swift
15916 projects
enum
40 projects

Projects that are alternatives of or similar to Swift Diagram Playgrounds

Glsp
Graphical language server platform for building web-based diagram editors
Stars: ✭ 53 (-79.92%)
Mutual labels:  protocol, diagram
ssdp
Python asyncio library for Simple Service Discovery Protocol (SSDP).
Stars: ✭ 25 (-90.53%)
Mutual labels:  protocol
Sharer
Arduino & .NET serial communication library to read/write variables and remote call functions using the Sharer protocol. Works on Windows, Linux and MacOS.
Stars: ✭ 21 (-92.05%)
Mutual labels:  protocol
UsingPlayground
👶 Demo how to use Playground
Stars: ✭ 25 (-90.53%)
Mutual labels:  playground
BedrockProxy
Allow Minecraft Bedrock players on your BungeeCord server!
Stars: ✭ 16 (-93.94%)
Mutual labels:  protocol
nxtp
Nxtp is a lightweight protocol for generalized crosschain transfers.
Stars: ✭ 129 (-51.14%)
Mutual labels:  protocol
wing-binlog
php mysqlbinlog monitoring system
Stars: ✭ 37 (-85.98%)
Mutual labels:  protocol
Libcsp
Cubesat Space Protocol - A small network-layer delivery protocol designed for Cubesats
Stars: ✭ 258 (-2.27%)
Mutual labels:  protocol
ethereum-dissectors
🔍Wireshark dissectors for Ethereum devp2p protocols
Stars: ✭ 82 (-68.94%)
Mutual labels:  protocol
tutorial
Tutorials for GoPlus (The Go+ Language)
Stars: ✭ 27 (-89.77%)
Mutual labels:  playground
VisualDebugger
The most elegant and easiest way to visual you data in playground
Stars: ✭ 22 (-91.67%)
Mutual labels:  playground
swift-algorithms-data-structs
📒 Algorithms and Data Structures in Swift. The used approach attempts to fully utilize the Swift Standard Library and Protocol-Oriented paradigm.
Stars: ✭ 42 (-84.09%)
Mutual labels:  playground
observable-playground
Know your Observables before deploying to production
Stars: ✭ 96 (-63.64%)
Mutual labels:  playground
xquic
XQUIC Library released by Alibaba is a cross-platform implementation of QUIC and HTTP/3 protocol.
Stars: ✭ 943 (+257.2%)
Mutual labels:  protocol
awesome-protocols
Curated list of awesome technology protocols with a reference to official RFCs
Stars: ✭ 16 (-93.94%)
Mutual labels:  protocol
pinout
An open source Python package that generates hardware pinout diagrams as SVG images.
Stars: ✭ 298 (+12.88%)
Mutual labels:  diagram
typescript-playground
TypeScript Playground
Stars: ✭ 52 (-80.3%)
Mutual labels:  playground
go-playground
GNU/Emacs mode that setup local Go playground for code snippets like play.golang.org or even better :)
Stars: ✭ 64 (-75.76%)
Mutual labels:  playground
Ipfs Desktop
An unobtrusive and user-friendly desktop application for IPFS on Windows, Mac and Linux.
Stars: ✭ 3,710 (+1305.3%)
Mutual labels:  protocol
Playgroundbook
Tool for Swift Playground books
Stars: ✭ 257 (-2.65%)
Mutual labels:  playground

Swift-Diagram-Playgrounds

Swift 3.0

This is an adaption of Apple’s sample code for the Protocol-Oriented Programming in Swift talk given during WWDC 2015.

Included is Apple’s original example playground file Crustacean.playground that uses a Protocol-oriented design (updated for Swift 3). In addition there's an alternative version CrustaceanEnumOriented.playground that uses a recursive enum as the data structure.

Finally there's the Diagrams.playground which adds a bit more functionality and includes several pages of example diagrams.

The playgrounds demonstrate two different approaches to creating Diagrams as value types and show how to draw them into a CGContext.


screenshot


Apple’s version uses a variety of structs that conform to the Drawable protocol to represent different shapes. The alternative approach uses a recursive enum to achieve the same result. It looks like this:

public enum Diagram {
  case Polygon([CGPoint])
  case Line([CGPoint])
  case Arc(radius: CGFloat, startAngle: CGFloat, endAngle: CGFloat)
  case Circle(radius: CGFloat)
  indirect case Scale(x: CGFloat, y: CGFloat, diagram: Diagram)
  indirect case Translate(x: CGFloat, y: CGFloat, diagram: Diagram)
  indirect case Rotate(angle: CGFloat, diagram: Diagram)
  case Diagrams([Diagram])
}

Note: livePreview will merrily consume processing power to continuously redraw still images, therefore it's recommended to manually stop execution of the Playground after the images have rendered.


Protocol-Oriented or Enum-Oriented – which is better?

The two approaches are a good demonstration of the expression problem. Which approach is easier to extend? Using a protocol-oriented technique allows you to add new types without too much hassle. In Apple’s example code a Bubble struct is added by implementing the Drawable protocol and Equatable (no pre-existing code needs to be adjusted). If a Bubble case were added to the enum version it would necessitate the altering of pre-existing functions (Equatable for Diagram and the drawDiagram function) this is more hassle and more error prone. However, we don't need to add a new case to the enum to draw Bubbles, we can simply add a function that constructs a bubble and returns a Diagram, in that case no code needs to be altered.

The use of a Renderer protocol makes it much easier to add a TestRenderer to log drawing. But using the Renderer protocol to add diagram transformation functionality is potentially very cumbersome. It is easy to add a ScaledRenderer type, but it would be more complicated to add a TranslateRenderer, or a RotateRenderer and duplicates functionality that is already provided by CGContext. The enum approach doesn't attempt to provide the logic for Diagram transformation, it simply stores the information needed and uses CGContext functions to do the hard work.

Which approach is better? I dunno ¯\_(ツ)_/¯

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].