public static void EarInit(VertexStructure vertices)
 {
     VertexStructure v1 = vertices;
     do {
         VertexStructure v2 = v1.next;
         VertexStructure v0 = v1.prev;
         v1.ear = Diagonal (vertices, v0, v2);
         v1 = v1.next;
     } while (!ReferenceEquals (v1, vertices));
 }
 public static bool Diagonalie(VertexStructure vertices, VertexStructure a, VertexStructure b)
 {
     VertexStructure c = vertices;
     do{
         VertexStructure c1 = c.next;
         if ( !ReferenceEquals(c,a) && !ReferenceEquals(c1,a)
             && !ReferenceEquals(c,b) && !ReferenceEquals(c1,b)
             && Intersect(a.v,b.v,c.v,c1.v) ) {
             return false;
         }
         c = c.next;
     }
     while (!ReferenceEquals(c,vertices));
     return true;
 }
        // Returns a list of points. Every sequence of three points make up a triangle.
        public static List<Point> GetTriangulateTrianglePoints(VertexStructure vertices, int numVertices)
        {
            List<Point> pointList = new List<Point> ();

            EarInit(vertices);
            int n = numVertices;

            while ( n > 3 ) {
                VertexStructure v2 = vertices;

                do {
                    if (v2.ear) {

                        VertexStructure v3 = v2.next;
                        VertexStructure v4 = v3.next;
                        VertexStructure v1 = v2.prev;
                        VertexStructure v0 = v1.prev;

                        // Save triangle v1,v2,v3
                        // Save diagonal v1,v3
                        pointList.Add(v1.v);
                        pointList.Add(v2.v);
                        pointList.Add(v3.v);

                        // Cut off ear v2
                        v1.next = v3;
                        v3.prev = v1;
                        vertices = v3;

                        // Update earity of v1 & v3
                        v1.ear = Diagonal (vertices, v0, v3);
                        v3.ear = Diagonal (vertices, v1, v4);

                        n--;
                        break;
                    }
                    v2 = v2.next;
                } while (!ReferenceEquals (v2, vertices));
            }

            pointList.Add (vertices.v);
            pointList.Add (vertices.next.v);
            pointList.Add (vertices.next.next.v);

            return pointList;
        }
Пример #4
0
 public static void DrawVertexStructure(VertexStructure vs, Context cr)
 {
     VertexStructure head = vs;
     int i = 0;
     do {
         cr.MoveTo (vs.v);
         cr.SetSourceRGB (0, 0, 0.8);
         cr.Arc (vs.v.X, vs.v.Y, 2, 0, 2 * Math.PI);
         cr.Fill ();
         cr.LineWidth = 1;
         cr.MoveTo (vs.v);
         cr.LineTo(vs.next.v);
         cr.Stroke();
         vs = vs.next;
         //Logger.Log("Meh..." + i);
         i++;
     } while(!ReferenceEquals(vs,head));
 }
        public static int Count(VertexStructure vertices)
        {
            int n = 0;
            VertexStructure v = vertices;
            do{
                v = v.next;
                n++;
            }
            while(!ReferenceEquals (v, vertices));

            return n;
        }
 public static bool Diagonal(VertexStructure vertices, VertexStructure a, VertexStructure b)
 {
     return InCone(a,b) && InCone(b,a) && Diagonalie(vertices, a, b);
 }
 public static void Add(ref VertexStructure head, Point p)
 {
     VertexStructure vs = new VertexStructure (p);
     VertexStructure.Add (ref head,vs);
 }
 public static void Add(ref VertexStructure head, VertexStructure v)
 {
     if (head != null) {
         v.next = head;
         v.prev = head.prev;
         head.prev.next = v;
         head.prev = v;
     } else {
         head = v;
         head.prev = v;
         head.prev = v;
     }
 }
        public static void Triangulate(VertexStructure vertices, int numVertices)
        {
            EarInit(vertices);
            int n = numVertices;

            while ( n > 3 ) {
                VertexStructure v2 = vertices;

                do {
                    if (v2.ear) {

                        VertexStructure v3 = v2.next;
                        VertexStructure v4 = v3.next;
                        VertexStructure v1 = v2.prev;
                        VertexStructure v0 = v1.prev;

                        // Save triangle v1,v2,v3
                        // Save diagonal v1,v3

                        // Cut off ear v2
                        v1.next = v3;
                        v3.prev = v1;
                        vertices = v3;

                        // Update earity of v1 & v3
                        v1.ear = Diagonal (vertices, v0, v3);
                        v3.ear = Diagonal (vertices, v1, v4);

                        n--;
                        break;
                    }
                    v2 = v2.next;
                } while (!ReferenceEquals (v2, vertices));
            }
        }
        public static bool InCone(VertexStructure a, VertexStructure b)
        {
            VertexStructure a0 = a.prev;
            VertexStructure a1 = a.next;

            if (LeftOn (a.v, a1.v, a0.v)) {
                return Left (a.v, b.v, a0.v)
                    && Left (b.v, a.v, a1.v);
            }

            return !( LeftOn(a.v, b.v, a1.v)
                    && LeftOn(b.v,a.v,a0.v) );
        }
Пример #11
0
        public void onKeyPress(object o, KeyPressEventArgs args)
        {
            Logger.Log ("key press " + args.Event.Key);

            if (args.Event.Key == Gdk.Key.p && state != MyState.AddingPoints) {
                state = MyState.AddingPoints;
                return;
            }

            if (args.Event.Key == Gdk.Key.p && state == MyState.AddingPoints) {
                foreach (PointD p in demo.points) {
                    VertexStructure.Add (ref vertices,p);
                }
                demo.points.Clear ();
                demo.vertexStructs.Add (vertices);
                state = MyState.NotAddingPoints;
                vertices = null;
                return;
            }

            if (args.Event.Key == Gdk.Key.t) {
                foreach (VertexStructure polygon in demo.vertexStructs) {
                    List<PointD> pointList = Geometry.ComputationalGeometry.GetTriangulateTrianglePoints (polygon,VertexStructure.Count(polygon));
                    List<PointD>.Enumerator iter = pointList.GetEnumerator();

                    iter.MoveNext ();
                    do {
                        PointD a = iter.Current;
                        iter.MoveNext ();
                        PointD b = iter.Current;
                        iter.MoveNext ();
                        PointD c = iter.Current;

                        Triangle t = new Triangle (a, b, c);

                        ActiveTriangle nt = new ActiveTriangle (demo,t);

                        demo.addObject(nt);

                    } while(iter.MoveNext ());

                    Logger.Log ("Iterated");
                }
                demo.vertexStructs.Clear ();
                return;
            }

            if (args.Event.Key == Gdk.Key.e || args.Event.Key == Gdk.Key.n) {
                List<IGameObject> goList = demo.getObjectsByTag (ActiveTriangle.tag);
                foreach (IGameObject go in goList){
                    ActiveTriangle t = (ActiveTriangle) go;
                    switch (args.Event.Key) {
                    case Gdk.Key.e:
                        t.setEvil ();
                        break;
                    case Gdk.Key.n:
                        t.setNeutral ();
                        break;
                    default:
                        t.setNeutral ();
                        break;
                    }
                }
            }

            if (args.Event.Key == Gdk.Key.o) {
                demo.addObject (new Protagonist (demo));
            }
        }