Home

Awesome

Project logo

haxe-kiwi is a port of Kiwi and Kiwi Java, implementations of the Cassowary constraint solving algorithm. Run the demo in your browser.

Features

Supports:

Doesn't support:

Usage

haxe-kiwi depends on assertion library Sure, install that first:

haxelib install sure

Include the library through Project.xml:

<include path="lib/haxe-kiwi/include.xml" />

See the demo code, the unit tests, or run the demo in the browser for usage examples.

Screenshot of demo app

// Basic usage
var solver = new Solver();

// Constraints are written in the form: a [==|<=|>=] b [*/] c [+-] d
var problem:String = '{"inequalities":["x == 20", "x == y + 10", "z == y + 30", "q == z + x", "foo == z + x", "bar == foo + x", "baz == foo * 10", "boz == x / 10 + y / 10 + x * 5"]}';
var structure:{inequalities:Array<String>} = Json.parse(problem);

var resolver:VarResolver = new VarResolver(); // Simple map wrapper that caches variables so that duplicates aren't added to the solver
for (inequality in structure.inequalities) {
    var constraint = ConstraintParser.parseConstraint(inequality, resolver, "required");
    solver.addConstraint(constraint);
}
solver.updateVariables(); // Update the values of the external solver variables

// Trace the value of external solver variable x (expect 20)
var x = resolver.resolveVariable("x");
trace("x = " + x.value);

resolver.traceVariables(); // Trace all the variables captured by the resolver

Notes

Acknowledgement