summaryrefslogtreecommitdiffstats
path: root/src/construction.c
diff options
context:
space:
mode:
authorJesper Jensen <jesper@jnsn.dev>2026-09-08 23:35:45 +0200
committerJesper Jensen <jesper@jnsn.dev>2026-09-08 23:35:45 +0200
commit754a18240de72cec691389825ffa3487f7052ca7 (patch)
treecca42feea0c56261de5f077363200c05370c2215 /src/construction.c
parent93427194d9df65e06751c9442cc95661bdbc354e (diff)
downloadconstraint-754a18240de72cec691389825ffa3487f7052ca7.tar.gz
constraint-754a18240de72cec691389825ffa3487f7052ca7.zip
I think we actually have the examples working
At the same time, for the first time
Diffstat (limited to 'src/construction.c')
-rw-r--r--src/construction.c34
1 files changed, 26 insertions, 8 deletions
diff --git a/src/construction.c b/src/construction.c
index 996b2d2..6b55c23 100644
--- a/src/construction.c
+++ b/src/construction.c
@@ -2,6 +2,28 @@
#include <cglm/cglm.h>
#include <string.h>
+void tangents(vec2 c2, double r1, double r2, struct line *l) {
+ double r = r2 - r1;
+ double z = sqrt(c2[0]) + sqrt(c2[1]);
+ double d = z - sqrt(r);
+ if (d < -DBL_EPSILON) return;
+ d = sqrt(fabs(d));
+ l->norm[0] = (c2[0] * r + c2[1] * d) / z;
+ l->norm[1] = (c2[1] * r - c2[0] * d) / z;
+ l->C = r1;
+}
+
+void line_tangent_circles(struct circle c1, struct circle c2, uint8_t root, struct line *l) {
+ vec2 o;
+ glm_vec2_sub(c2.center, c2.center, o);
+
+ double i = ((root >> 0) & 0x1) * 2.0f - 1;
+ double j = ((root >> 1) & 0x1) * 2.0f - 1;
+
+ tangents(o, c1.radius * i, c2.radius * j, l);
+ l->C -= l->norm[0] * c1.center[0] + l->norm[1] * l->norm[1];
+}
+
bool circle_line_intersect(struct circle circle, struct line line, uint8_t root, struct point *point) {
double a = line.norm[0];
double b = line.norm[1];
@@ -108,10 +130,8 @@ static void transform_part(struct command *cmd, const struct command *last_cmd,
vec2 p;
glm_vec2_zero(p);
- if(line->norm[0] > line->norm[1])
- glm_vec2_copy((vec2){-line->C / line->norm[0], 0}, p);
- else
- glm_vec2_copy((vec2){0, -line->C / line->norm[1]}, p);
+ // Point closest to the origin
+ glm_vec2_scale(line->norm, -line->C / glm_vec2_norm2(line->norm), p);
// Rotate the line to the new orientation
affine_transform_vec2(rotate, line->norm, line->norm);
@@ -211,7 +231,7 @@ void place_points(struct drawing *drawing, double inputs[]) {
uint32_t i = 0;
for(struct command *current = drawing->root; current != NULL; current = current->next) {
- // if(i++ > 50) break;
+ // if(i++ > 87) break;
current->placed = true;
switch(current->op) {
@@ -293,9 +313,7 @@ void place_points(struct drawing *drawing, double inputs[]) {
// centers
abort();
} else {
- // We should probably implement tangents for circles that
- // don't happen to be 0 radius
- abort();
+ line_tangent_circles(current->arg1->circle, current->arg2->circle, current->root, &current->result.line);
}
}break;
case CMD_POINT_CIRCLE_LINE: {