]> git.cworth.org Git - akamaru/commitdiff
New feature: polygons can now enclose objects.
authorKristian Høgsberg <krh@redhat.com>
Sun, 11 Jun 2006 18:24:49 +0000 (14:24 -0400)
committerKristian Høgsberg <krh@redhat.com>
Sun, 11 Jun 2006 18:24:49 +0000 (14:24 -0400)
akamaru.c
akamaru.h
dock.c
main.c

index 88277f66bc9dd78a0c491e243731f3089627c24e..cc551436cd371449ec3a2f19ef7c9faf5559c1ed 100644 (file)
--- a/akamaru.c
+++ b/akamaru.c
@@ -21,7 +21,7 @@
 #include "akamaru.h"
 
 const double elasticity = 0.5;
-const double friction = 1;
+const double friction = 4;
 const double gravity = 50;
 
 void
@@ -85,7 +85,7 @@ anchor_init (Anchor *anchor, Object *object, double x, double y)
 }
 
 void
-polygon_init (Polygon *p, int num_points, ...)
+polygon_init (Polygon *p, int enclosing, int num_points, ...)
 {
   double dx, dy, length;
   int i, j;
@@ -97,6 +97,7 @@ polygon_init (Polygon *p, int num_points, ...)
   va_start (ap, num_points);
   p->num_points = num_points;
   p->points = g_new (Point, num_points);
+  p->enclosing = enclosing;
 
   for (i = 0; i < num_points; i++) {
     p->points[i].x = va_arg (ap, double);
@@ -120,7 +121,7 @@ polygon_init (Polygon *p, int num_points, ...)
 void
 polygon_init_diamond (Polygon *polygon, double x, double y)
 {
-  return polygon_init (polygon, 5, 
+  return polygon_init (polygon, FALSE, 5, 
                       x, y, 
                       x + 10, y + 40,
                       x + 90, y + 40,
@@ -132,7 +133,14 @@ void
 polygon_init_rectangle (Polygon *polygon, double x0, double y0,
                        double x1, double y1)
 {
-  return polygon_init (polygon, 4, x0, y0, x0, y1, x1, y1, x1, y0);
+  return polygon_init (polygon, FALSE, 4, x0, y0, x0, y1, x1, y1, x1, y0);
+}
+
+void
+polygon_init_enclosing_rectangle (Polygon *polygon, double x0, double y0,
+                               double x1, double y1)
+{
+  return polygon_init (polygon, TRUE, 4, x0, y0, x0, y1, x1, y1, x1, y0);
 }
 
 void
@@ -276,10 +284,10 @@ polygon_contains_point (Polygon *polygon, Point *point)
     dy = point->y - polygon->points[i].y;
 
     if (polygon->normals[i].x * dx + polygon->normals[i].y * dy >= 0)
-      return FALSE;
+      return polygon->enclosing;
   }
 
-  return TRUE;
+  return !polygon->enclosing;
 }
 
 static void
@@ -297,7 +305,6 @@ polygon_reflect_object (Polygon *polygon, Object *object)
     if (d > distance) {
       distance = d;
       edge = i;
-      polygon->edge = i;
       n = &polygon->normals[i];
     }
   }
index cf7bf8b682412c6970706af7c020d5cd291efa1a..77f8d580646a4acf3ef09003295238fa50d4f18f 100644 (file)
--- a/akamaru.h
+++ b/akamaru.h
@@ -69,7 +69,7 @@ struct _Polygon {
   int num_points;
   Point *points;
   Vector *normals;
-  int edge;
+  int enclosing;
 };
 
 struct _Model {
@@ -108,10 +108,12 @@ 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 (Polygon *p, int enclosing, int num_points, ...);
 void polygon_init_diamond (Polygon *polygon, double x, double y);
 void polygon_init_rectangle (Polygon *polygon, double x0, double y0,
                             double x1, double y1);
+void polygon_init_enclosing_rectangle (Polygon *polygon, double x0, double y0,
+                                      double x1, double y1);
 
 void model_fini (Model *model);
 
diff --git a/dock.c b/dock.c
index bc439ff0efe528c0c1e506aa666938c0cdee5d4a..54e4746774bffa9f92cd58471f57c1aece50c6d2 100644 (file)
--- a/dock.c
+++ b/dock.c
@@ -94,13 +94,13 @@ model_init_dock (Model *model, int num_items, int x, int y, int spacing)
 
   model->polygons = g_new (Polygon, 1);
   model->num_polygons = 1;
-  polygon_init_rectangle (&model->polygons[0], -4000, y, 4000, 2000);
+  polygon_init_enclosing_rectangle (&model->polygons[0], 0, 0, 1024 - 50, y);
 
   model->anchors[0].x = x;
   model->anchors[0].y = y;
   model->anchors[0].object = &model->objects[0];
 
-  object_init (&model->objects[0], x, y, 1);
+  object_init (&model->objects[0], x, y, 20);
 
   object = &model->objects[1];
   spring = model->springs;
diff --git a/main.c b/main.c
index fd1318fd8eeacfad574f6507f79fc9e887e3abc4..15fbce446fbc41771d509ec9f29ad86a6141c7c4 100644 (file)
--- a/main.c
+++ b/main.c
@@ -279,7 +279,7 @@ model_init_dock (Model *model)
 
   model->polygons = g_new (Polygon, 1);
   model->num_polygons = 1;
-  polygon_init_rectangle (&model->polygons[0], -400, 300, 1400, 350);
+  polygon_init_enclosing_rectangle (&model->polygons[0], 10, 10, 700, 500);
 
   model->anchors[0].x = 300;
   model->anchors[0].y = 300;
@@ -441,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);
 
 }