Swiftpack.co - Package - temoki/TortoiseGraphics

Swift Swift Package Manager Carthage compatible Version License Platform

A turtle graphics (a key feature of the Logo engine written in Swift.

The commands were implemented with reference to the turtle in Python 3 standard libraries.

Example

// Turtle Star!
🐢.beginFill()
🐢.repeat(36) {
    🐢.forward(200)
    🐢.left(170)
}
🐢.endFill()

Result

Usage

Draw in NSView (macOS)

class CanvasView: NSView {

    public override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)

        // Get current context
        guard let cgContext = NSGraphicsContext.current?.cgContext else { return }

        // Instantiate a GraphicsCanvas
        let canvas = GraphicsCanvas(size: bounds.size, context: cgContext)

        // Command 🐢 on canvas.
        canvas.drawing { 🐢 in
            🐢.right(90)
            🐢.forward(100)
        }
    }

}

Draw in UIView (iOS)

class CanvasView: UIView {

    override func draw(_ rect: CGRect) {
        super.draw(rect)

        // Get current context
        guard let context = UIGraphicsGetCurrentContext() else { return }

        // Instantiate a GraphicsCanvas
        let canvas = GraphicsCanvas(size: bounds.size, context: context)

        // Command 🐢 on canvas.
        canvas.drawing { 🐢 in
            🐢.right(90)
            🐢.forward(100)
        }
    }

}

Make or Write image

// Instantiate a ImageCanvas
let canvas = ImageCanvas(size: CGSize(width: 300, height: 300))

// Command 🐢 on canvas
canvas.drawing { 🐢 in
    🐢.right(90)
    🐢.forward(100)
}

// Get drawn CGImage
let cgImage = canvas.cgImage

// Get drawn NSImage or UIImage
let image = canvas.image

// Write to image file (PNG, JPEG, TIFF, GIF)
canvas.writePNG(to: URL(fileURLWithPath: "./image.png")
canvas.writeJPEG(to: URL(fileURLWithPath: "./image.jpeg")
canvas.writeTIFF(to: URL(fileURLWithPath: "./image.tiff")
canvas.writeGIF(to: URL(fileURLWithPath: "./image.gif")

With multiple tortoises

canvas.drawingWithTortoises(count: 2) { tortoises in
    let 🐢 = tortoises[0]
    let 🐇 = tortoises[1]
    🐢.shape(.tortoise)
    🐇.shape(.classic)

    🐢.penColor(.red)
    🐢.fillColor(.orange)
    🐢.left(90)

    🐇.penColor(.purple)
    🐇.fillColor(.lightBlue)
    🐇.right(90)

    // Turtle Star!
    🐢.beginFill()
    🐇.beginFill()
    🐢.repeat(36) {
        🐢.forward(120)
        🐇.forward(120)
        🐢.left(170)
        🐇.right(170)
    }
    🐢.endFill()
    🐇.endFill()
}

Playgrounds

On Xcode

  1. Open TortoiseGraphics.xcworkspace in Xcode.
  2. Build TortoiseGraphics scheme for My Mac.
  3. Select Playground in project navigator.
  4. Let's play!

On Swift Playgrounds (iPad app)

Subscription

  1. Launch Safari on your iPad and browse this page.
  2. Tap the following link to subscribe.
  3. You can get Tortoise Graphics playground book on Swift Playgrounds app.

Download

  1. Download TortoisePlayground.playgroundbook.zip from here.
  2. Unarchive the downloaded zip file.
  3. Send the unarchvied TortoisePlayground.playgroundbook file to your iPad by AirDrop.
  4. Open it with Swift Playgrounds app.
  5. Let's play!

Classes

  • ImageCanvas
  • GraphicsCanvas
  • Tortoise
  • Shape
  • Vec2D

Tortoise Commands

Motion

Move and Draw

  • forward(), fd()
  • backword(), back(), bk()
  • right(), rt()
  • left(), lt()
  • setPosition(), setPos(), goto()
  • setX()
  • setY()
  • setHeading(), setH()
  • home()
  • dot()
  • circle()
  • repeat() {}

Tell tortoise's state

  • position, pos
  • towards()
  • xcor
  • ycor
  • heading
  • distance()
  • random()

Pen Control

Drawing state

  • penDown(), pd(), down()
  • penUp(), pu(), up()
  • penSize()
  • width()
  • isDown
  • penSize
  • width

Color control

  • penColor()
  • penColor
  • fillColor()
  • fillColor

Filling

  • filling
  • beginFill()
  • endFill()

More drawing control

  • reset()
  • clear()

Tortoise state

Visiblity

  • showTortoise(), st()
  • hideTortoise(), ht()
  • isVisible
  • shape()
  • shape

Requirements

  • Swift 4.2 (Xcode 10)
  • macOS 10.10 or later
  • iOS 10.0 or later

Installation

Swift Package Manager

dependencies: [
    .Package(url: "https://github.com/temoki/TortoiseGraphics.git", majorVersion: 0)
]

Carthage

github "temoki/TortoiseGraphics"

Cocoapods

pod 'TortoiseGraphics'

License

TortoiseGraphics is released under the MIT license. See LICENSE for details.