From 4be5b6593af8db64cd39dba290a2c64193bcdabd Mon Sep 17 00:00:00 2001 From: =?utf8?q?Kristian=20H=C3=B8gsberg?= Date: Sat, 10 Jun 2006 02:48:52 -0400 Subject: [PATCH] Add anchor constraint and use it to lock center point in dock model. --- akamaru.c | 28 +++++++++++++++++++++------- akamaru.h | 12 ++++++++++-- main.c | 34 +++++++++++++++------------------- 3 files changed, 46 insertions(+), 28 deletions(-) diff --git a/akamaru.c b/akamaru.c index ae01a27..8045454 100644 --- a/akamaru.c +++ b/akamaru.c @@ -76,6 +76,14 @@ spacer_init (Spacer *spacer, Object *a, Object *b, double length) spacer->length = length; } +void +anchor_init (Anchor *anchor, Object *object, double x, double y) +{ + anchor->object = object; + anchor->x = x; + anchor->y = y; +} + void polygon_init (Polygon *p, int num_points, ...) { @@ -316,6 +324,15 @@ model_constrain_polygon (Model *model, Polygon *polygon) } } +static void +model_constrain_anchor (Model *model, Anchor *anchor) +{ + anchor->object->position.x = anchor->x; + anchor->object->position.y = anchor->y; + anchor->object->previous_position.x = anchor->x; + anchor->object->previous_position.y = anchor->y; +} + static void model_constrain_offset (Model *model, Offset *offset) { @@ -344,13 +361,10 @@ model_constrain (Model *model) double dx, dy, x, y, distance, fraction; int i; - /* Anchor object constraint. */ - if (model->anchor_object != NULL) { - model->anchor_object->position.x = model->anchor_position.x; - model->anchor_object->position.y = model->anchor_position.y; - model->anchor_object->previous_position.x = model->anchor_position.x; - model->anchor_object->previous_position.y = model->anchor_position.y; - } + if (model->mouse_anchor.object != NULL) + model_constrain_anchor (model, &model->mouse_anchor); + for (i = 0; i < model->num_anchors; i++) + model_constrain_anchor (model, &model->anchors[i]); /* String constraints. */ for (i = 0; i < model->num_strings; i++) { diff --git a/akamaru.h b/akamaru.h index 1646a34..cf7bf8b 100644 --- a/akamaru.h +++ b/akamaru.h @@ -13,6 +13,7 @@ typedef struct _String String; typedef struct _Spring Spring; typedef struct _OffsetSpring OffsetSpring; typedef struct _Spacer Spacer; +typedef struct _Anchor Anchor; typedef struct _Polygon Polygon; typedef struct _Offset Offset; typedef struct _Model Model; @@ -59,6 +60,11 @@ struct _Spacer { int length; }; +struct _Anchor { + Object *object; + double x, y; +}; + struct _Polygon { int num_points; Point *points; @@ -81,13 +87,14 @@ struct _Model { OffsetSpring *offset_springs; int num_spacers; Spacer *spacers; + int num_anchors; + Anchor *anchors; int num_polygons; Polygon *polygons; double k; double friction; - Object *anchor_object; - Vector anchor_position; + Anchor mouse_anchor; double theta; }; @@ -99,6 +106,7 @@ void spring_init (Spring *spring, Object *a, Object *b, double length); void stick_init (Stick *stick, Object *a, Object *b, double length); void string_init (String *string, Object *a, Object *b, double length); void spacer_init (Spacer *spacer, Object *a, Object *b, double length); +void anchor_init (Anchor *anchor, Object *object, double x, double y); void polygon_init (Polygon *p, int num_points, ...); void polygon_init_diamond (Polygon *polygon, double x, double y); diff --git a/main.c b/main.c index 39cb5ad..3b1d8d4 100644 --- a/main.c +++ b/main.c @@ -63,8 +63,6 @@ model_init_snake (Model *model) model->sticks[i * 2 + 1].length = random() % 20 + 20; } } - - model->anchor_object = NULL; } static void @@ -91,8 +89,6 @@ model_init_rope (Model *model) model->sticks[i].length = stick_length; } } - - model->anchor_object = NULL; } static void @@ -138,15 +134,13 @@ model_init_curtain (Model *model) model->offsets[0].objects[i] = &model->objects[i * num_rope_objects]; } - - model->anchor_object = NULL; } static void model_init_grid (Model *model) { - const int num_ropes = 4; - const int num_rope_objects = 4; + const int num_ropes = 8; + const int num_rope_objects = 8; const int num_objects = num_ropes * num_rope_objects; const int num_strings = num_ropes * (num_rope_objects - 1) + (num_ropes - 1) * num_rope_objects; @@ -186,8 +180,6 @@ model_init_grid (Model *model) model->offsets[0].objects[i] = &model->objects[i * num_rope_objects]; } - - model->anchor_object = NULL; } static void @@ -281,13 +273,17 @@ model_init_dock (Model *model) model->num_springs = num_springs; model->spacers = g_new (Spacer, num_spacers); model->num_spacers = num_spacers; + model->anchors = g_new (Anchor, 1); + model->num_anchors = 1; model->k = 0.1; - model_init_polygons (model); model->polygons = g_new (Polygon, 1); - polygon_init_rectangle (&model->polygons[0], -400, 300, 1400, 350); model->num_polygons = 1; + polygon_init_rectangle (&model->polygons[0], -400, 300, 1400, 350); + model->anchors[0].x = 300; + model->anchors[0].y = 300; + model->anchors[0].object = &model->objects[0]; object = model->objects; spring = model->springs; @@ -528,10 +524,10 @@ button_press_event (GtkWidget *widget, if (event->button != 1) return TRUE; - closure->model->anchor_position.x = event->x; - closure->model->anchor_position.y = event->y; - closure->model->anchor_object = model_find_nearest (closure->model, - event->x, event->y); + closure->model->mouse_anchor.x = event->x; + closure->model->mouse_anchor.y = event->y; + closure->model->mouse_anchor.object = + model_find_nearest (closure->model, event->x, event->y); return TRUE; } @@ -546,7 +542,7 @@ button_release_event (GtkWidget *widget, if ((event->state & GDK_BUTTON1_MASK) == 0) return TRUE; - closure->model->anchor_object = NULL; + closure->model->mouse_anchor.object = NULL; return TRUE; } @@ -562,8 +558,8 @@ motion_notify_event (GtkWidget *widget, gdk_window_get_pointer (event->window, &x, &y, &state); - closure->model->anchor_position.x = x + 0.5; - closure->model->anchor_position.y = y + 0.5; + closure->model->mouse_anchor.x = x + 0.5; + closure->model->mouse_anchor.y = y + 0.5; return TRUE; } -- 2.43.0