コード例 #1
0
        public String Scan(double polarAngle, double scanAngle, int energy)
        {
            String bigoutput = "";

            //updateSelf();
            // cordinates of the spaceship
            ArrayList myList = new ArrayList();

            if (this._energy >= energy)
            {
                double x = this.XCoordinate;
                double y = this.YCoordinate;

                int r = this.Capacity / 100;

                Point a, b, c;

                int dist = (int)Math.Round(energy / Math.Abs(Math.Sin((Math.PI - scanAngle) / 2 * Math.PI / 180.0)), 0);

                double xA = Math.Round(x + r * Math.Cos(polarAngle * Math.PI / 180.0), 2);
                double yA = Math.Round(y + r * Math.Sin(polarAngle * Math.PI / 180.0), 2);
                // B and C are points of the scan extremes

                double xB = Math.Round(xA + dist * Math.Cos((polarAngle - scanAngle * 0.5) * Math.PI / 180.0), 2);
                double yB = Math.Round(yA + dist * Math.Sin((polarAngle - scanAngle * 0.5) * Math.PI / 180.0), 2);


                double xC = Math.Round(xA + dist * Math.Cos((polarAngle + scanAngle * 0.5) * Math.PI / 180.0), 2);
                double yC = Math.Round(yA + dist * Math.Sin((polarAngle + scanAngle * 0.5) * Math.PI / 180.0), 2);

                a = new Point(xA, yA);
                b = new Point(xB, yB);
                c = new Point(xC, yC);



                var dict = this.SpaceshipsInsideWithShadow(a, b, c);

                foreach (KeyValuePair <Spaceship, double> kvp in dict)
                {
                    Console.WriteLine(((Spaceship)(kvp.Key)).Name + " with ratio " + kvp.Value);
                    myList.Add(kvp.Key);
                }



                /*
                 * Printing as 2D array
                 *
                 */

                bool pretty_print = true;

                if (pretty_print)
                {
                    int xmax, ymax;

                    xmax = (int)(Math.Max(xC, Math.Max(xA, xB)) - Math.Min(xC, Math.Min(xA, xB)));
                    ymax = (int)(Math.Max(yC, Math.Max(yA, yB)) - Math.Min(yC, Math.Min(yA, yB)));
                    Console.WriteLine(xmax + " " + ymax);
                    int[,] toPrint = new int[xmax, ymax];

                    for (int i = 0; i < xmax; i++)
                    {
                        for (int j = 0; j < ymax; j++)
                        {
                            toPrint[i, j] = -1;
                        }
                    }

                    Point new_a = new Point(a.x - Math.Min(xC, Math.Min(xA, xB)), a.y - Math.Min(yC, Math.Min(yA, yB))),
                          new_b = new Point(b.x - Math.Min(xC, Math.Min(xA, xB)), b.y - Math.Min(yC, Math.Min(yA, yB))),
                          new_c = new Point(c.x - Math.Min(xC, Math.Min(xA, xB)), c.y - Math.Min(yC, Math.Min(yA, yB)));

                    foreach (KeyValuePair <Spaceship, double> kvp in dict)
                    {
                        Random    random = new Random();
                        Spaceship sp     = kvp.Key;
                        double    ratio  = kvp.Value;
                        int       chnum  = (int)Math.Ceiling(16 * ratio);
                        Console.WriteLine(sp.code.Substring(0, chnum));

                        String substring    = sp.code.Substring(0, chnum);
                        Point  translocated = new Point(sp.XCoordinate - Math.Min(xC, Math.Min(xA, xB)),
                                                        sp.YCoordinate - Math.Min(yC, Math.Min(yA, yB)));

                        if ((int)translocated.x < xmax && (int)translocated.x >= 0 && (int)translocated.y >= 0 && (int)translocated.y < ymax)
                        {
                            toPrint[(int)translocated.x, (int)translocated.y] = -1;
                        }

                        int stringIndex = 0;

                        int startPoint = -90;

                        Point   inter1, inter2;
                        Point[] aux_point_array = GameMaths.CutsSpaceshipAtPoints(a, b, c, new Point(sp.XCoordinate, sp.YCoordinate), sp._capacity / 100);
                        if (aux_point_array != null)
                        {
                            inter1    = aux_point_array[0];
                            inter2    = aux_point_array[1];
                            inter1.x += 1;
                            inter1.y += 1;
                            if (GameMaths.IsInside(a, b, c, inter1))
                            {
                                inter1.x  -= 1;
                                inter2.y  -= 1;
                                startPoint = (int)((180.0 / Math.PI) * Math.Atan2(inter1.y - sp.XCoordinate, inter1.x - sp.YCoordinate));
                                Console.WriteLine(startPoint);
                            }
                            else
                            {
                                startPoint = (int)((180.0 / Math.PI) * Math.Atan2(inter2.y - sp.YCoordinate, inter2.x - sp.XCoordinate));
                                Console.WriteLine(startPoint);
                            }
                        }

                        for (double i = startPoint; i >= startPoint - 360; i -= 0.5)
                        {
                            int xcircle, ycircle;
                            xcircle = (int)(translocated.x + sp._capacity / 100 * Math.Cos(i * Math.PI / 180.0));
                            ycircle = (int)(translocated.y + sp._capacity / 100 * Math.Sin(i * Math.PI / 180.0));
                            if (GameMaths.IsInside(new_a, new_b, new_c, new Point(ycircle, xcircle)))
                            {
                                if (xcircle < xmax && xcircle >= 0 && ycircle < ymax && ycircle >= 0 && toPrint[xcircle, ycircle] == -1)

                                {
                                    toPrint[xcircle, ycircle] = int.Parse("" + substring[stringIndex % chnum]);
                                    stringIndex++;
                                }
                            }
                        }


                        for (int i = ymax - 1; i >= 0; i--)
                        {
                            for (int j = 0; j < xmax; j++)
                            {
                                if (toPrint[j, i] <= 9 && toPrint[j, i] >= 0 && GameMaths.IsInside(new_a, new_b, new_c, new Point(j, i)))
                                {
                                    bigoutput += (toPrint[j, i]).ToString();
                                }
                                else
                                {
                                    bigoutput += " ";
                                }
                            }

                            bigoutput += "\n";
                        }


                        /*
                         * end of printing
                         */
                    }

                    return(bigoutput);
                }
            }

            return(bigoutput);
        }