X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=main.c;h=15fbce446fbc41771d509ec9f29ad86a6141c7c4;hb=4b4b752bb845106529d270b73419b97e11d595c2;hp=c5b306035975b13392f851d705c9d610a4ae68d5;hpb=6923ae2757901728f2bc4ef2162ca5d14ecfbd14;p=akamaru diff --git a/main.c b/main.c index c5b3060..15fbce4 100644 --- a/main.c +++ b/main.c @@ -49,11 +49,8 @@ model_init_snake (Model *model) model_init_polygons (model); for (i = 0; i < num_objects; i++) { - model->objects[i].position.x = random() % 200 + 20; - model->objects[i].position.y = random() % 200 + 20; - model->objects[i].previous_position.x = random() % 200 + 20; - model->objects[i].previous_position.y = random() % 200 + 20; - model->objects[i].mass = 1; + object_init (&model->objects[i], + random() % 200 + 20, random() % 200 + 20, 1); if (i + 1 < num_objects) { model->sticks[i * 2].a = &model->objects[i]; @@ -66,8 +63,6 @@ model_init_snake (Model *model) model->sticks[i * 2 + 1].length = random() % 20 + 20; } } - - model->anchor_object = NULL; } static void @@ -86,11 +81,7 @@ model_init_rope (Model *model) model_init_polygons (model); for (i = 0; i < num_objects; i++) { - model->objects[i].position.x = 200; - model->objects[i].position.y = 40 + i * stick_length; - model->objects[i].previous_position.x = 200; - model->objects[i].previous_position.y = 40 + i * stick_length; - model->objects[i].mass = 1; + object_init (&model->objects[i], 200, 40 + i * stick_length, 1); if (i + 1 < num_objects) { model->sticks[i].a = &model->objects[i]; @@ -98,8 +89,6 @@ model_init_rope (Model *model) model->sticks[i].length = stick_length; } } - - model->anchor_object = NULL; } static void @@ -133,11 +122,7 @@ model_init_curtain (Model *model) x = 200 + i * rope_offset; y = 40 + j * stick_length; index = i * num_rope_objects + j; - model->objects[index].position.x = x; - model->objects[index].position.y = y; - model->objects[index].previous_position.x = x; - model->objects[index].previous_position.y = y; - model->objects[index].mass = 1; + object_init (&model->objects[index], x, y, 1); if (j + 1 < num_rope_objects) { stick_index = i * (num_rope_objects - 1) + j; @@ -149,22 +134,21 @@ 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; const int string_length = 20; const int rope_offset = 20; - double x, y; - int i, j, index, string_index; + Object *object; + String *string; + int i, j; memset (model, 0, sizeof *model); model->objects = g_new (Object, num_objects); @@ -180,37 +164,22 @@ model_init_grid (Model *model) model->offsets[0].dx = rope_offset; model->offsets[0].dy = 0; + object = model->objects; + string = model->strings; for (i = 0; i < num_ropes; i++) { for (j = 0; j < num_rope_objects; j++) { - x = 200 + i * rope_offset; - y = 40 + j * string_length; - index = i * num_rope_objects + j; - model->objects[index].position.x = x; - model->objects[index].position.y = y; - model->objects[index].previous_position.x = x; - model->objects[index].previous_position.y = y; - model->objects[index].mass = 1; - - if (i + 1 < num_ropes) { - string_index = i * num_rope_objects + j; - model->strings[string_index].a = &model->objects[index]; - model->strings[string_index].b = &model->objects[index + num_rope_objects]; - model->strings[string_index].length = string_length; - } - - if (j + 1 < num_rope_objects) { - string_index = - (num_ropes - 1) * num_rope_objects + i * (num_rope_objects - 1) + j; - model->strings[string_index].a = &model->objects[index]; - model->strings[string_index].b = &model->objects[index + 1]; - model->strings[string_index].length = string_length; - } + object_init (object, 200 + i * rope_offset, 40 + j * string_length, 1); + + if (i + 1 < num_ropes) + string_init (string++, + object, object + num_rope_objects, string_length); + if (j + 1 < num_rope_objects) + string_init (string++, object, object + 1, string_length); + object++; } model->offsets[0].objects[i] = &model->objects[i * num_rope_objects]; } - - model->anchor_object = NULL; } static void @@ -220,6 +189,7 @@ model_init_molecule (Model *model) const int num_springs = num_objects * 2; const int spring_length = 50; int i; + Spring *spring; memset (model, 0, sizeof *model); model->objects = g_new (Object, num_objects); @@ -228,21 +198,17 @@ model_init_molecule (Model *model) model->num_springs = num_springs; model->k = 2; - for (i = 0; i < num_objects; i++) { - model->objects[i].position.x = 200 + i * 20; - model->objects[i].position.y = 200; - model->objects[i].previous_position.x = 200 + i * 20; - model->objects[i].previous_position.y = 200; - model->objects[i].mass = 0; - } + for (i = 0; i < num_objects; i++) + object_init (&model->objects[i], 200 + i * 20, 200, 0); + spring = model->springs; for (i = 0; i < num_objects; i++) { - model->springs[i * 2].a = &model->objects[i]; - model->springs[i * 2].b = &model->objects[(i + 1) % num_objects]; - model->springs[i * 2].length = spring_length; - model->springs[i * 2 + 1].a = &model->objects[i]; - model->springs[i * 2 + 1].b = &model->objects[(i + 2) % num_objects]; - model->springs[i * 2 + 1].length = spring_length; + spring_init (spring++, &model->objects[i], + &model->objects[(i + 1) % num_objects], + spring_length); + spring_init (spring++, &model->objects[i], + &model->objects[(i + 2) % num_objects], + spring_length); } } @@ -255,7 +221,9 @@ model_init_wobbly (Model *model) const int num_offset_springs = (width - 1) * height + width * (height - 1); const int distance = 30; double x, y; - int i, j, object_index, spring_index; + int i, j; + Object *object; + OffsetSpring *spring; memset (model, 0, sizeof *model); model->objects = g_new (Object, num_objects); @@ -266,35 +234,72 @@ model_init_wobbly (Model *model) model_init_polygons (model); - object_index = 0; - spring_index = 0; + object = model->objects; + spring = model->offset_springs; for (i = 0; i < width; i++) { for (j = 0; j < height; j++) { x = 200 + i * distance; y = 40 + j * distance; - model->objects[object_index].position.x = x; - model->objects[object_index].position.y = y; - model->objects[object_index].previous_position.x = x; - model->objects[object_index].previous_position.y = y; - model->objects[object_index].mass = 0; - - if (i + 1 < width) { - model->offset_springs[spring_index].a = &model->objects[object_index]; - model->offset_springs[spring_index].b = &model->objects[object_index + height]; - model->offset_springs[spring_index].dx = distance; - model->offset_springs[spring_index].dy = 0; - spring_index++; - } + object_init (object, x, y, 0); + + if (i + 1 < width) + offset_spring_init (spring++, object, object + height, distance, 0); - if (j + 1 < height) { - model->offset_springs[spring_index].a = &model->objects[object_index]; - model->offset_springs[spring_index].b = &model->objects[object_index + 1]; - model->offset_springs[spring_index].dx = 0; - model->offset_springs[spring_index].dy = distance; - spring_index++; - } + if (j + 1 < height) + offset_spring_init (spring++, object, object + 1, 0, distance); + + object++; + } + } +} + +static void +model_init_dock (Model *model) +{ + const int num_objects = 8; + const int num_spacers = (num_objects - 1) * (num_objects - 2) / 2; + const int num_springs = num_objects - 1; + const int distance = 30; + double x, y; + int i, j; + Object *object; + Spring *spring; + Spacer *spacer; - object_index++; + memset (model, 0, sizeof *model); + model->objects = g_new (Object, num_objects); + model->num_objects = num_objects; + model->springs = g_new (Spring, num_springs); + 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->polygons = g_new (Polygon, 1); + model->num_polygons = 1; + polygon_init_enclosing_rectangle (&model->polygons[0], 10, 10, 700, 500); + + model->anchors[0].x = 300; + model->anchors[0].y = 300; + model->anchors[0].object = &model->objects[0]; + + object = model->objects; + spring = model->springs; + spacer = model->spacers; + for (i = 0; i < num_objects; i++, object++) { + x = 200 + i * distance / 3; + y = 40; + object_init (object, x, y, 1); + if (i == 0) + continue; + spring_init (spring++, + &model->objects[0], + &model->objects[i], + distance); + for (j = 1; j < num_objects - i; j++) { + spacer_init (spacer++, object, object + j, distance); } } } @@ -436,14 +441,22 @@ draw_polygons (cairo_t *cr, Model *model, Color *color) for (i = 0; i < model->num_polygons; i++) { p = &model->polygons[i]; - cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4); - for (j = 0; j < p->num_points; j++) cairo_line_to (cr, p->points[j].x, p->points[j].y); cairo_close_path (cr); + + if (p->enclosing) { + cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.1); + cairo_fill_preserve (cr); + } + + cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4); + if (p->enclosing) + cairo_stroke (cr); + else + cairo_fill (cr); } - cairo_fill (cr); } @@ -519,10 +532,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; } @@ -537,7 +550,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; } @@ -553,8 +566,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; } @@ -592,7 +605,8 @@ create_model_store (void) { "Curtain", model_init_curtain }, { "Grid", model_init_grid }, { "Molecule", model_init_molecule }, - { "Wobbly", model_init_wobbly } + { "Wobbly", model_init_wobbly }, + { "Dock", model_init_dock } }; GtkTreeIter iter; @@ -658,7 +672,7 @@ create_window (Closure *closure) GtkWidget *model_combo; window = gtk_window_new (GTK_WINDOW_TOPLEVEL); - gtk_window_set_title (GTK_WINDOW (window), "Akamaru"); + gtk_window_set_title (GTK_WINDOW (window), "赤丸"); g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), &window);