Showing posts with label development. Show all posts
Showing posts with label development. Show all posts

Friday, February 7, 2020

Fractal Friday 2020.02.07

This week I'm sharing the results of a cool new feature in MathPaint: fractals warped by real-valued functions. The application now supports defining a custom real-valued function that is applied during each iterative evaluation to the real part of the complex number 'z', before z is squared.

The possible effects are endless - from slight skewing to complete warping of the image, but generally the fractal retains its self-similarity and zoomability, as long as the real function doesn't drive it to a premature divergence or convergence.

A variety of approaches produced the images below, including use of a sine function with different powers of the input argument, a step function (a simple round() with some scaling applied), and a natural-log-base exponential function. All are warped Julia sets except for the next-to-last, which is based on Mandelbrot. A variety of MathPaint graphics features are also used such as edge drawing and different forms of color mapping.







More next week! You can follow Mathaesthetics on Instagram, Facebook, Twitter and Pinterest for more cool images and announcements about MathPaint, the app that makes them.


Wednesday, January 15, 2020

Hello World, from MathPaint! (Preview)

If you've been following Mathaesthetics posts you've seen a variety of images shared - different types of generated art from fractals to repeated 2D plots to vector fields and scalar fields and more.

All images on this blog are created by our flagship software product, which has been in development since November 2019. Today for the first time I'm publicly sharing some screenshots of the user-interface, starting with a few document windows:

Hello World, from MathPaint!

These MathPaint documents were used to create our app icon and current Facebook cover.

MathPaint is a Mac OS desktop application written in Swift. A private beta release is planned for February 2020, and the completed 1.0 application will be released later this year, through the Apple AppStore.

CoreImage effects can be applied to any MathPaint canvas
I've always felt that creativity and mathematics belong together. MathPaint is designed for creative professionals who would like to harness of of the endless generative capability of mathematics to create visual forms, patterns, textures, and algorithmic art. You don't need to be a programmer to create such designs with MathPaint, and the release will include visual templates and presets that are intuitive to browse and modify, plus cool automation features:

The "Repeater" feature can add multiple iterated re-renderings to a model

I also think MathPaint is a natural tool for students. While there are robust software products for mathematical modeling and computation, MathPaint focuses on aesthetics and uniquely permits the creative impulse to guide mathematical learning and exploration.

I will be using MathPaint to make art, and I hope to see a community of makers who create original expressive work with this software. Even algorithmic artists used to coding may find some rewarding features and workflows with this tool.

Document settings give you a page-layout app's control over your math-art canvas.

Now that the first screenshots are out, look for weekly updates and posts on MathPaint progress! And of course, a continuing stream of art, including our Fractal Friday posts, all made by MathPaint.



Monday, December 23, 2019

Dev notebook: an enum alternative to GKState / GKStateMachine

I worked on my first GameplayKit project earlier this year and there were things I liked about the lightweight, general state-machine model provided by the GKState and GKStateMachine classes. However I also found a few odd patterns in these classes: their implementation without using enumeration values for states, the inelegant (StateClass).class.self syntax model required by the state machine enterState() method, and the update() interface which I found extra and somewhat orthogonal to the pure function of the state machine (I keep time-based concerns in entities and components).

In my current project, which is not a game, I wanted to use a pattern similar to GKStateMachine for various UI and model states, but decided to implement a simple framework that addressed these issues rather than use GKStateMachine.

The basic pattern is provided by a protocol that *could* be implemented by any class, but the interface strongly suggests that a state will be an enum : Int type:


protocol MPState {
    var rawEnumValue : Int { get }
    var validNextStates : [Int] { get }
    
    func didEnter(from prevState: MPState?, with machine: MPStateMachine)
    func willExit(to nextState: MPState?, with machine: MPStateMachine)
}


Obviously the rawEnumValue will be very easy to implement as a wrapper on rawValue if the state is implemented as an enum : Int rather than some other type of struct or class. Also, the required validNextStates computed property is expected to return values that can be compared with rawEnumValue for inclusion/exclusion, and this logic is made explicit in an extension to the protocol (which also implements didEnter and willExit as no-ops to make them optional):


extension MPState {
    func isValidNextState(_ nextState : MPState, 
                          with machine: MPStateMachine) -> Bool {

        return self.validNextStates.contains(nextState.rawEnumValue)
    }
    
    func didEnter(from prevState: MPState?, with machine: MPStateMachine) {
        // no-op, override to handle
    }
    
    func willExit(to nextState: MPState?, with machine: MPStateMachine) {
        // no-op, override to handle
    }
}


One thing this pattern precludes: the state object itself can't retain any persistent reference to either the state machine or any model object, since enumerations in swift can't have stored properties. This is handled by simply passing the machine object into the three relevant methods; the machine is a full-fledged class which also has an optional model property that can bring in more object context.


The state-machine class provides an interface like that of GKStateMachine, but instead of passing a class to the enter() method you can just pass your enumerated state value. It has a similar currentState read-only computed property (returning a private machineState value which can only be updated by successful enter). Here's the entire implementation:

class MPStateMachine : NSObject {
    var model: Any? = nil
    
    var currentState : MPState? {
        get {
            return self.machineState
        }
    }
    
    func canEnterState(_ nextState : MPState) -> Bool {
        if nil != self.machineState {
            return self.machineState!.isValidNextState(nextState, with: self)
        }
        return true // assume we can enter any state from nil
    }
    
    func enter(_ state: MPState) -> Bool {
        var setState : Bool = false
        if self.machineState == nil {
            self.machineState = state
            state.didEnter(from: nil, with: self)
            setState = true
        } else if self.machineState!.isValidNextState(state, with: self) {
            let oldState = self.machineState!
            self.machineState?.willExit(to: state, with: self)
            self.machineState = state
            state.didEnter(from: oldState, with: self)
            setState = true
        }
        return setState
    }
    
    fileprivate var machineState : MPState? = nil

}

The model property in the state machine is up to the client code to define; it will typically be the model object whose state is being managed, i.e. the domain-specific owner of the state machine instance.

As a sample implementation I created an MPApplicationState which tracks the app states of starting, running, terminating, owned by my AppDelegate class. This demonstrates the pattern of how valid next-states can be handled by a simple switch statement for most state collections, but you could always add helper methods for various state-transition contexts for more complex cases that need to inspect the model:

enum MPApplicationState : Int, MPState {

    case starting
    case running
    case terminating
    
    var rawEnumValue: Int {
        get {
            return self.rawValue
        }
    }
    
    var validNextStates: [Int] {
        get {
            var nextStates : [Int] = []
            switch self {
                case .starting:
                    nextStates.append(MPApplicationState.running.rawValue)
                    nextStates.append(MPApplicationState.terminating.rawValue)
                case .running:
                    nextStates.append(MPApplicationState.terminating.rawValue)
                case .terminating:
                    nextStates.removeAll()
            }
            return nextStates
        }
    }
    
    func didEnter(from prevState: MPState?, with machine: MPStateMachine) {
        print ("*** MPApplicationState *** did enter state: \(String(describing:self)) from \(String(describing:prevState))")
    }
    
    func willExit(to nextState: MPState, with machine: MPStateMachine) {
        print ("*** MPApplicationState *** will exit state: \(String(describing:self)) to state: \(String(describing:nextState))")
    }

}

Just to show the functionality (not really because it's useful) I created an MPStateMachine property on my AppDelegate (allocated at the declaration), and transition states in the following method overrides:

func applicationWillFinishLaunching(_ notification: Notification) {
    _ = self.stateMachine.enter(MPApplicationState.starting)

}


func applicationDidFinishLaunching(_ aNotification: Notification) {
    _ = self.stateMachine.enter(MPApplicationState.running)
    // etc...
}

func applicationWillTerminate(_ aNotification: Notification) {

    _ = self.stateMachine.enter(MPApplicationState.terminating)
}

I'm not bothering to check the output values for these simple cases but the idea is that more involved state transitions could do so, and/or pre-check the ability of the state machine to transition with a call a like:


   self.stateMachine.canEnterState(MPApplicationState.running)

And now checking the current application state looks a bit nicer than it would with GKStateMachine:

    if appDelegate.stateMachine.currentState != MPApplicationState.terminating {

        // do something...

That's it!

Tuesday, December 10, 2019

Painting with Trigonometry

I'm excited to share today's image generated from the application under development:


It's a pretty organic background-pattern sort of texture, but it highlights a few cool things about the app, which is focused on creating beautiful images with mathematical techniques.

Saturday, November 30, 2019

Dev notebook: brush-like drawing in Swift, without CGPattern

I'm spending a lot of time in Cocoa drawing and Core Graphics lately, working in Swift.

The API around the Core Graphics CGPattern object in Swift is a little challenging - it requires C callbacks and unsafe pointers for basic pattern-creation and drawing functionality. It also doesn't work exactly as I'd like it to; I want to have more of a 'brush' metaphor where I can vary the 'paint' flow of the pattern being used to draw a line or a curve. But I definitely want a pattern-like construct that I can apply different stroke and fill colors to in order to draw.

My solution is to use NSImage, which is really convenient because it's a high-level object in Swift that can easily be created in memory or loaded from a .png resource in the app bundle.

The approach: given an NSImage that represents your brush pattern, create a copy NSImage that is rendered in the desired stroke color, then use that copy to draw your desired lines and curves, or tile it along with clipping to fill an area. I'll include an example of drawing a line with varying density, and of filling an area with clipping and offset adjustment.