コード例 #1
0
ファイル: Circle.cs プロジェクト: philipwolfe/Miscellaneous
        Circle FindProto()
        {
            /*
             * Search through the prototype list to see if there is already a shape table with the
             * proper number of sides. If there is, we set the reference to it, and we're done.
             * If not, we have to create one...
             */
            Console.WriteLine("FindProto: {0} {1}", size, sides);
            foreach (Circle c in prototypes)
            {
                if ((c.sides == sides) &&
                    (c.size == size))
                {
                    return(c);
                }
            }
            Console.WriteLine("FindProto2:");

            /*
             * We didn't find an existing prototype. We'll have to create a new one...
             */
            Circle proto = new Circle();

            proto.sides = sides;
            proto.size  = size;
            proto.shape = new Point[sides + 1];

            Console.WriteLine("FindProto3:");

            double inc;
            double angle;

            inc   = 3.14159 * 2.0 / sides;
            angle = inc / 2;
            for (int index = 0; index <= sides; index++)
            {
                //CODEGEN: Uncomment next line to hide problem.
                //Console.WriteLine("");
                double dX = (double)size * Math.Cos(angle);
                double dY = (double)size * Math.Sin(angle);

                proto.shape[index].X = (int)Math.Round(dX);
                proto.shape[index].Y = (int)Math.Round(dY);
                angle += inc;
            }
            Console.WriteLine("FindProto4:");

            /*
             * Special case to handle the size 1 and 2 cases, which don't work with the above...
             */
            if (size == 1)
            {
                proto.shape[0].X = 0;
                proto.shape[0].Y = 0;
            }
            else if (size == 2)
            {
                proto.shape[0].X = 0;
                proto.shape[0].Y = 0;
                proto.shape[1].X = 1;
                proto.shape[1].Y = 0;
                proto.shape[2].X = 1;
                proto.shape[2].Y = 1;
                proto.shape[3].X = 0;
                proto.shape[3].Y = 1;
                proto.shape[4].X = 0;
                proto.shape[4].Y = 0;
            }

            prototypes.Add(proto);
            return(proto);
        }