summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/quad.c1
-rw-r--r--examples/quad2.c101
-rw-r--r--examples/stacked.c2
-rw-r--r--examples/triangle_tip.c6
4 files changed, 108 insertions, 2 deletions
diff --git a/examples/quad.c b/examples/quad.c
index a22e872..b689f61 100644
--- a/examples/quad.c
+++ b/examples/quad.c
@@ -26,6 +26,7 @@ void draw_from_constraints(struct constraints *c, struct topology *t, struct can
begin_drawing(cv);
draw_topology(cv, t);
draw_constraints(cv, c);
+ draw_elements(cv, &drawing);
end_drawing(cv);
free_drawing(&drawing);
diff --git a/examples/quad2.c b/examples/quad2.c
new file mode 100644
index 0000000..fd465fd
--- /dev/null
+++ b/examples/quad2.c
@@ -0,0 +1,101 @@
+#include "cad.h"
+#include "cad/svg.h"
+#include "cad/debug.h"
+
+void draw_from_constraints(struct constraints *c, struct topology *t, struct canvas *cv) {
+ struct drawing drawing = {};
+ struct subassembly assemblies[16] = {};
+ size_t assemblies_num;
+ bool rc = solve_constraints(c, &drawing, assemblies, &assemblies_num);
+ assert(rc);
+
+ // Copy over all the parameter values to a new array
+ // @PERF: Maybe we should just store them in a separate array to start with
+ double *params = malloc(sizeof(double) * (c->length));
+ for(size_t i = 0; i < c->length; i++) {
+ params[i] = c->elements[i].v;
+ }
+
+ place_points(&drawing, params);
+ dump_program(&drawing, params);
+ free(params);
+
+ reconstruct_drawing(c, assemblies, &assemblies_num);
+ print_subassemblies(c, assemblies, assemblies_num);
+
+ begin_drawing(cv);
+ draw_topology(cv, t);
+ draw_constraints(cv, c);
+ draw_elements(cv, &drawing);
+ end_drawing(cv);
+
+ free_drawing(&drawing);
+}
+
+int main(int argc, char *argv[]) {
+ struct constraints constraints = {};
+ struct topology topo = {};
+
+ struct component bp = {.type = COM_POINT};
+ struct component yx = {.type = COM_LINE};
+
+ struct component p1 = {.type = COM_POINT};
+ struct component p2 = {.type = COM_POINT};
+ struct component p3 = {.type = COM_POINT};
+ struct component p4 = {.type = COM_POINT};
+
+ struct component l1 = {.type = COM_LINE};
+ struct component l2 = {.type = COM_LINE};
+ struct component l3 = {.type = COM_LINE};
+ struct component l4 = {.type = COM_LINE};
+
+ add_constraint(&constraints, (struct constraint[]){
+ POINT_ON_LINE(&bp, &yx),
+
+ PP_DISTANCE(&p1, &bp, 10),
+ PL_DISTANCE(&p1, &yx, 5),
+
+ LL_ANGLE(&l2, &yx, DEG(30)),
+
+ PP_DISTANCE(&p1, &p2, 10),
+ PP_DISTANCE(&p4, &p1, 20),
+
+ POINT_ON_LINE(&p1, &l1),
+ POINT_ON_LINE(&p2, &l1),
+
+ POINT_ON_LINE(&p2, &l2),
+ POINT_ON_LINE(&p3, &l2),
+
+ POINT_ON_LINE(&p3, &l3),
+ POINT_ON_LINE(&p4, &l3),
+
+ POINT_ON_LINE(&p4, &l4),
+ POINT_ON_LINE(&p1, &l4),
+
+ LL_ANGLE(&l4, &l1, DEG(90)),
+ LL_ANGLE(&l2, &l3, DEG(90)),
+ LL_ANGLE(&l3, &l4, DEG(90)),
+
+ CEND(),
+ });
+
+ add_fragment(&topo, (struct topology_elem[]){
+ TOPO_MOVETO(&p1),
+ TOPO_LINETO(&p2),
+ TOPO_LINETO(&p3),
+ TOPO_LINETO(&p4),
+ TOPO_LINETO(&p1),
+
+ TOPO_END(),
+ });
+
+ struct canvas canvas = {
+ .state = CANVAS_INIT,
+ .f = stdout,
+ };
+
+ draw_from_constraints(&constraints, &topo, &canvas);
+
+ free_constraints(&constraints);
+ return 0;
+}
diff --git a/examples/stacked.c b/examples/stacked.c
index aeee9ee..0d52898 100644
--- a/examples/stacked.c
+++ b/examples/stacked.c
@@ -76,11 +76,13 @@ void draw_from_constraints(struct constraints *c, struct topology *t, struct can
}
place_points(&drawing, params);
+ dump_program(&drawing, params);
free(params);
begin_drawing(cv);
draw_topology(cv, t);
draw_constraints(cv, c);
+ // draw_elements(cv, &drawing);
end_drawing(cv);
free_drawing(&drawing);
diff --git a/examples/triangle_tip.c b/examples/triangle_tip.c
index 232b16e..87875de 100644
--- a/examples/triangle_tip.c
+++ b/examples/triangle_tip.c
@@ -17,14 +17,16 @@ void draw_from_constraints(struct constraints *c, struct topology *t, struct can
}
place_points(&drawing, params);
- free(params);
reconstruct_drawing(c, assemblies, &assemblies_num);
print_subassemblies(c, assemblies, assemblies_num);
+ // dump_program(&drawing, params);
+ free(params);
begin_drawing(cv);
draw_topology(cv, t);
draw_constraints(cv, c);
+ // draw_elements(cv, &drawing);
end_drawing(cv);
free_drawing(&drawing);
@@ -66,7 +68,7 @@ int main(int argc, char *argv[]) {
POINT_ON_LINE(&p5, &t2base),
// The edge they don't share is constrained
- LL_ANGLE(&t1bas2, &t2base, DEG(70)),
+ LL_ANGLE(&t1bas2, &t2base, DEG(80)),
CEND(),
});