google / cassowary.dart Goto Github PK
View Code? Open in Web Editor NEWCassowary constraint solving algorithm in Dart
License: BSD 3-Clause "New" or "Revised" License
Cassowary constraint solving algorithm in Dart
License: BSD 3-Clause "New" or "Revised" License
Hello, I've been exploring the library and it's been great, however I was wondering if I could get help building constraints for a specific case.
Imagine I make a box. It's easy to add constraints for a normal box, it would just look something like
[
pos.top >= cm(0),
pos.left >= cm(0),
pos.right >= cm(0),
pos.bottom >= cm(0),
(pos.right).equals(pos.left + pos.w) | Priority.strong,
(pos.bottom).equals(pos.top + pos.h) | Priority.strong,
(pos.centerX).equals((pos.left + pos.right) / cm(2.0)) |
Priority.strong,
(pos.centerY).equals((pos.top + pos.bottom) / cm(2.0)) |
Priority.strong,
];
This is just describing a solid box. I set the right/bottom/centerX/centerY to strong because other constraints may want to modify the size later on.
For translating the top and left position, we just do
[
(pos.left).equals(bounds.left + cm(offset.dx)) | Priority.strong,
(pos.top).equals(bounds.top + cm(offset.dy)) | Priority.strong,
]
This is making so if the offset x/y change, the left and top must change (theoretically)
Changing the width and height works well too, the right and bottom edges resize as needed and the top and left edges remain in place. This is because the constraints are satisfied well.
To get width/height to work, we add the following constraints:
[
(pos.right).equals(pos.left + cm(width)) | Priority.required,
(pos.bottom).equals(pos.top + cm(height)) | Priority.required,
]
This is making so if the width or height change, the right and bottom edges must change (theoretically).
The offset, width and height may change, when they do, the solver rebuilds the constraints with the updated values.
However, if you resize from the top left corner, or the top corner, or the left corner, you will translate the box instead of resize + change top/left. I expected the translation constraints to run, finish, then the sizing constraints to run, then finish, but clearly that's not what's happening. So I was wondering what flaw exists in my logic when thinking about this.
I wanted to translate, and then resize, not simutaeniously. Should I flush translation first then flush resize second to enforce an order?
My best guess is that Stays have not been implemented here and the solver is forcing the equation by changing the width and height variables, which are unused. Like if Stays existed, I would've set width/height to stayed values so they don't change.
Reproducible example
final a = Param();
final b = Param();
final c1 = a >= cm(10);
final c2 = b <= cm(5);
final c3 = b >= a;
final solver = Solver();
print('add c1 ${solver.addConstraint(c1).message}');
print('add c2 ${solver.addConstraint(c2).message}');
print('add c3 ${solver.addConstraint(c3).message}');
print('remove c3 ${solver.removeConstraint(c3).message}');
solver1.flushUpdates();
print('vars a=${a.value} b=${b.value}');
Output
add c1 Success
add c2 Success
add c3 Unsatisfiable constraint
remove c3 Unknown constraint
vars a=10 b=10
Solver says "Unknown constraint" on removing c3 constraint, but c3 constraint has an impact on the solution.
When I create a constraint and print it to logs I end up with something like:
+Instance of 'Variable' >= 0 | priority = 1000000000.0 (required)
This happens even after I set a name
to the variable. Is there a way to get variable names when printing constraints?
I looked at the Variable
class and toString()
is not implemented.
Here's a description of my problem:
The customer has various walls of a fixed length, and hes want to efficiently dress them with segments of a material i have in fixed lengths (let's say lengths of a 2 meters and b 3 meters).
I have an unlimited supply of these components, but you I want to count how many of each component are to be used to fill the wall while minimizing waste.
Constraints:
The goal is to find an arrangement of components and their segmenting on the wall that minimizes the number of segments used and counts how many of each segment length is used in each wall.
Do you think this solver help me solve my problem?
I tried the example in https://github.com/google/cassowary.dart/blob/master/example/main.dart .
I'm getting these results:
left: -0.0, right: 100.0
left: -50.0, mid: 0.0, right: 50.0
The first line of results looks ok.
However I don't understand why adding the midpoint Variable
would change left
to -50, therefore breaking the edgesPositive
constraint.
I would have expected.
left: -0.0, mid: 50.0, right: 100.0
Maybe I'm missing something.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.