Index: world.c
===================================================================
--- world.c	(revision 1025)
+++ world.c	(revision 1026)
@@ -7,18 +7,212 @@
  **/
 
 #include <stdio.h>
+#include <stdlib.h>
+#include <math.h>
+#include <stdarg.h>
 #include "world.h"
 
 
-double collide_circle(ray_t *ray, object_t *obj){
+
+double (*collide_func[NUM_SHAPES])(ray_t *ray, object_t *obj) =
+    {
+    collide_poly
+    };
+int (*create_func[NUM_SHAPES])(object_t *obj, va_list ap) =
+{
+    create_poly
+};
+double collide_circle(ray_t *ray, object_t *obj)
+{
     return -1;
 }
-double collide_rect(ray_t *ray, object_t *obj){
-    return -2;
+
+double collide_seg(ray_t *ray, point_t p3, point_t p4)
+{   
+    double denom, nume_a, nume_b, ua, ub, xint, yint, dist;
+    point_t p1 = ray->p;
+    point_t p2 = {ray->p.x+cos(ray->d),ray->p.y+sin(ray->d)};
+
+   // printf("(%g,%g) --> (%g,%g) with (%g,%g) --> (%g,%g)\n",p1.x,p1.y,p2.x,p2.y,p3.x,p3.y,p4.x,p4.y);
+    denom = (p2.y-p1.y)*(p4.x-p3.x)-(p2.x-p1.x)*(p4.y-p3.y);
+    if (denom == 0) {
+	return RAY_MISS;
+    }
+    nume_a = (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);
+    nume_b = (p4.x-p3.x)*(p3.y-p1.y)-(p4.y-p3.y)*(p3.x-p1.x);
+
+    ua = nume_a/denom;
+    if (ua < 0 || ua > 1) {
+	return RAY_MISS;
+    }
+
+    ub = nume_b/denom;
+    if (ub < 0) {
+	return RAY_MISS;
+    }
+
+    xint = p3.x+ua*(p4.x-p3.x);
+    yint = p3.y+ua*(p4.y-p3.y);
+    dist = sqrt((xint-p1.x)*(xint-p1.x)+(yint-p1.y)*(yint-p1.y));
+    return dist;
+
 }
+double collide_rect(ray_t *ray, object_t *obj) {}
+    /*
+{
+    seg_t s;
+    double min = RAY_MISS, x;
+    rect_t *rect = (rect_t *)obj->props;
 
-double collide(ray_t *ray, object_t *obj) {
+    s.p1 = s.p2 = rect->p1;
+    s.p2.y = rect->p2.y;
+    if ((x = collide_seg(ray, (seg_t *)&s)) < min) 
+	min = x;
+
+    s.p1 = rect->p2;
+    if ((x = collide_seg(ray, (seg_t *)&s)) < min) 
+	min = x;
+
+    s.p2 = rect->p1;
+    s.p2.x = rect->p2.x;
+    if ((x = collide_seg(ray, (seg_t *)&s)) < min) 
+	min = x;
+
+    s.p1 = rect->p1;
+    if ((x = collide_seg(ray, (seg_t *)&s)) < min) 
+	min = x;
+
+    return min;
+}
+*/
+
+double collide_poly(ray_t *ray, object_t *obj)
+{
+    int i;
+    double min = RAY_MISS;
+    double x;
+    poly_t *p = (poly_t *) obj->props;
+    
+    if (obj->id != ID_POLY){
+	return -1;
+    }
+
+    for (i = 0; i < p->num_pts - 1; i++) {
+	if ((x = collide_seg(ray, p->pts[i], p->pts[i+1])) < min){
+	    min = x;
+	}
+    }
+    if (p->type == POLY_DISCONNECTED) {
+	if ((x = collide_seg(ray,p->pts[i],p->pts[0])) < min)
+	    min = x;
+    }
+    return min;
+}
+
+double collide(ray_t *ray, object_t *obj) 
+{
     if (ray == NULL || obj == NULL)
+    {
 	return -1;
+    }
     return collide_func[obj->id](ray, obj);
 }
+
+
+
+object_t *create(int id, ...)
+{
+    object_t *obj;
+    va_list args;
+
+    obj = malloc(sizeof(object_t));
+    obj->id = id;
+    va_start(args, id);
+    if (create_func[id](obj, args) < 0) {
+	free(obj);
+	obj = NULL;
+    }
+    va_end(args);
+    return obj;
+}
+
+/**
+ *
+ **/
+int create_poly(object_t *obj, va_list ap) 
+{
+    int i;
+    poly_t *p;
+
+    int argc =  va_arg(ap, int);
+    int poly_type = va_arg(ap, int);
+
+    p = malloc(sizeof(poly_t));
+
+
+    p->num_pts = argc;
+    p->pts = malloc((argc) * sizeof(point_t));
+    p->type = poly_type;
+
+    for(i=0;i < argc; i++) {
+	p->pts[i].x = va_arg(ap, double);
+	p->pts[i].y = va_arg(ap, double);
+    }
+    obj->id = ID_POLY;
+    obj->bbox = NULL;
+    obj->props = p;
+    return 1;
+}
+
+int create_rect(object_t *obj, va_list ap)
+{
+    int i;
+    poly_t *p;
+    int argc = va_arg(ap, int);
+}
+
+void destroy_poly (object_t *obj)
+{
+    poly_t *p;
+    int i;
+
+    if (obj == NULL)
+	return;
+    p =  (poly_t *)obj->props;
+    if (p != NULL) free(p->pts);
+    free(p);
+    free(obj);
+
+    for (i = 0; i < p->num_pts; i++)
+    {
+    }
+
+
+
+
+}
+
+void print_object(object_t *obj)
+{
+    if (obj == NULL) {
+	printf("No object\n");
+	return;
+    }
+    int i;
+    poly_t *p;
+    switch (obj->id) {
+	case ID_POLY:
+	    p = (poly_t *) obj->props;
+	    printf("POLYGON (%d points, %s) { ", p->num_pts, p->type?"connected" : "disconnected");
+	    for (i = 0; i < p->num_pts; i++){
+		printf("(%g, %g) ", p->pts[i].x, p->pts[i].y);
+	    }
+	    printf("}\n");
+	    break;
+	default:
+	    break;
+		    
+    }
+}
+
+
