All Projects → lithdew → casso

lithdew / casso

Licence: MIT license
A Go implementation of the Cassowary constraint solving algorithm.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to casso

ConstraintFlowPlayground
Playground app for Constraint Layout Flow
Stars: ✭ 48 (-36%)
Mutual labels:  constraint-layout
D3 Cloud
Create word clouds in JavaScript.
Stars: ✭ 3,460 (+4513.33%)
Mutual labels:  layout-algorithm
ModernSimpleProfileUI
Design a Modern Simple Profile UI with Constraint Layout in Android Studio 3.1 Canary 6
Stars: ✭ 24 (-68%)
Mutual labels:  constraint-layout
KeepSafeNew
Sample app to demonstrate MVP (Model - View - Presenter), Android Architecture Components (Room Persistence, LiveData), RxJava2, ButterKnife in Android.
Stars: ✭ 58 (-22.67%)
Mutual labels:  constraint-layout
WordCloud.jl
word cloud generator in julia
Stars: ✭ 66 (-12%)
Mutual labels:  layout-algorithm
cassowary.lua
A Lua port of the cassowary constraint solver engine
Stars: ✭ 34 (-54.67%)
Mutual labels:  cassowary
HeadLines
HeadLines is a 📰 news app that delivers you with the latest news. It has interactive UI and easy to use. The app can be scrolled offline to watch your bookmarked news. Give this app a try and let me know.
Stars: ✭ 16 (-78.67%)
Mutual labels:  constraint-layout
FoodApp
Proof of concept for food app [JetPack + Coroutines + Flow + MockK + JaCoCo coverage + SonarQube]
Stars: ✭ 25 (-66.67%)
Mutual labels:  constraint-layout
Vivagraphjs
Graph drawing library for JavaScript
Stars: ✭ 3,442 (+4489.33%)
Mutual labels:  layout-algorithm
Android-Starter-Kit
This is up-to-date android studio project for native android application, that is using modern tools and libraries.
Stars: ✭ 16 (-78.67%)
Mutual labels:  constraint-layout
Trailers
An open source app that is refactored to demo MVVM architecture
Stars: ✭ 20 (-73.33%)
Mutual labels:  constraint-layout
jungrapht-visualization
visualization and sample code from Java Universal Network Graph ported to use JGraphT models and algorithms
Stars: ✭ 37 (-50.67%)
Mutual labels:  layout-algorithm
razcal
Build cross platform desktop app with Lua, MoonScript, and Layout Language
Stars: ✭ 15 (-80%)
Mutual labels:  cassowary
android-constraintlayout-demo
Demo usage of various ConstraintLayout features
Stars: ✭ 49 (-34.67%)
Mutual labels:  constraint-layout
MotionLayoutSpeedDial
All MotionLayout of SpeedDial - FloatingActionButton
Stars: ✭ 22 (-70.67%)
Mutual labels:  constraint-layout
GithubApp-android-architecture
Let's learn a deep look at the Android architecture
Stars: ✭ 16 (-78.67%)
Mutual labels:  constraint-layout
Cassowary
High performance swift implement of constraint solving algorithm cassowary
Stars: ✭ 45 (-40%)
Mutual labels:  cassowary
jasper
Haxe port of Kiwi's implementation of the cassowary algorithm.
Stars: ✭ 22 (-70.67%)
Mutual labels:  cassowary
Rainlayout
Constraintlayout based rain-animation view developed backed by Kotlin Coroutines.
Stars: ✭ 32 (-57.33%)
Mutual labels:  constraint-layout
mvp-sample
Demonstrates how to implement MVP (Model View Presenter) pattern using Kotlin, RXJava, Retrofit, Dagger and DataBinding
Stars: ✭ 35 (-53.33%)
Mutual labels:  constraint-layout

casso

MIT License go.dev reference Discord Chat

casso is a low-level Go implementation of the popular Cassowary constraint solving algorithm.

casso allows you to efficiently and incrementally describe partially-conflicting required/preferential constraints over a set of variables, and solve for a solution against them that is legitimately locally-error-better much like the simplex algorithm.

It is popularly used in Apple's Auto Layout Visual Format Language, and in Grid Style Sheets.

Description

Linear equality and inequality constraints arise naturally in specifying many aspects of user interfaces, such as requiring that one window be to the left of another, requiring that a pane occupy the leftmost 1/3 of a window, or preferring that an object be contained within a rectangle if possible. Current constraint solvers designed for UI applications cannot efficiently handle simultaneous linear equations and inequalities. This is a major limitation. We describe Cassowary—an incremental algorithm based on the dual simplex method that can solve such systems of constraints efficiently.

Paper written by Greg J. Badros, and Alan Borning. For more information, please check out the paper here.

Example

s := casso.NewSolver()

containerWidth := casso.New()

childX := casso.New()
childCompWidth := casso.New()

child2X := casso.New()
child2CompWidth := casso.New()

// c1: childX == (50.0 / 1024) * containerWidth
// c2: childCompWidth == (200.0 / 1024) * containerWidth
// c3: childCompWidth >= 200.0
// c4: child2X - childX - childCompWidth == 50
// c5: child2CompWidth == 50 + containerWidth + child2X

c1 := casso.NewConstraint(casso.EQ, 0, childX.T(1.0), containerWidth.T(-50.0/1024))
c2 := casso.NewConstraint(casso.EQ, 0, childCompWidth.T(1.0), containerWidth.T(-200.0/1024))
c3 := casso.NewConstraint(casso.GTE, -200, childCompWidth.T(1.0))
c4 := casso.NewConstraint(casso.EQ, -50, child2X.T(1.0), childX.T(-1.0), childCompWidth.T(-1.0))
c5 := casso.NewConstraint(casso.EQ, 50, child2CompWidth.T(1.0), containerWidth.T(-1.0), child2X.T(1.0))

// Mark 'containerWidth' as an editable variable with strong precedence.
// Suggest 'containerWidth' to take on the value 2048.

require.NoError(t, s.Edit(containerWidth, casso.Strong))
require.NoError(t, s.Suggest(containerWidth, 2048))

// Add constraints to the solver.

_, err := s.AddConstraint(c1)
require.NoError(t, err)

_, err = s.AddConstraintWithPriority(casso.Weak, c2)
require.NoError(t, err)

_, err = s.AddConstraintWithPriority(casso.Strong, c3)
require.NoError(t, err)

_, err = s.AddConstraint(c4)
require.NoError(t, err)

_, err = s.AddConstraint(c5)
require.NoError(t, err)

// Grab computed values.

require.EqualValues(t, 2048, s.Val(containerWidth))
require.EqualValues(t, 400, s.Val(childCompWidth))
require.EqualValues(t, 1448, s.Val(child2CompWidth))

// Suggest 'containerWidth' to take on the value 500.

require.NoError(t, s.Suggest(containerWidth, 500))

// Grab computed values.

require.EqualValues(t, 500, s.Val(containerWidth))
require.EqualValues(t, 200, s.Val(childCompWidth))
require.EqualValues(t, 175.5859375, s.Val(child2CompWidth))

Remarks

Symbols/references to variables are represented as unsigned 64-bit integers. The first two bits of a symbol denote the symbols type, with the rest of the bits denoting the symbols ID.

A symbol with an ID of zero is marked to be invalid. As a result, a program at any given moment in time may only generate at most 2^62 - 1 symbols, or 4,611,686,018,427,387,903 symbols.

This was done for performance reasons to minimize memory usage and reduce the number of cycles needed to perform some operations. If you need this restriction lifted for a particular reason, please open up a Github issue.

Benchmarks

$ cat /proc/cpuinfo | grep 'model name' | uniq
model name : Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz

$ go test -bench=. -benchtime=10s
goos: linux
goarch: amd64
pkg: github.com/lithdew/casso
BenchmarkAddConstraint-8         8426456              1701 ns/op            1168 B/op         11 allocs/op
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].