Ejemplo n.º 1
0
        /// <summary>
        /// This methods was taken from the exercice level 1 GetLocation. And use a BaseTool method named TrilaterationSolver.
        /// TODO: search for a best Trilateration Algorithm.
        /// </summary>
        /// <param name="allSatellites"></param>
        /// <returns></returns>
        public XYCoordinates GetLocation(AllSatellitesData allSatellites)  //I took this function from exercice Level 1 but I extend it passing more information in its parameter.
        {
            //At the moment it is only calling this BaseTools methods, but it could be an interface to a generic calls with diferents implementations and parameters.
            XYCoordinates xYCoordinates = BaseTools.TrilaterationSolver(_rebelSatellites.rebelSatellitesPositionList[allSatellites.satellites[0].name],
                                                                        allSatellites.satellites[0].distance,
                                                                        _rebelSatellites.rebelSatellitesPositionList[allSatellites.satellites[1].name],
                                                                        allSatellites.satellites[1].distance,
                                                                        _rebelSatellites.rebelSatellitesPositionList[allSatellites.satellites[2].name],
                                                                        allSatellites.satellites[2].distance);

            //reformat GetLocation asnwer to match exercice reuirements. Just 1 decimal number.
            xYCoordinates.xCoord = (float)Math.Round(xYCoordinates.GetCoordinateX(), 1);
            xYCoordinates.yCoord = (float)Math.Round(xYCoordinates.GetCoordinateY(), 1);
            return(xYCoordinates);
        }
        /// <summary>
        /// This main method resolve the required conditions of the topsecret level 2 exercice.
        /// Ask for the methods created in exercice level 1 GetPosition and GetMessage
        /// </summary>
        /// <param name="allSatelites"></param>
        /// <returns></returns>
        public TopSecretResponseData GetImperialShipInformation(AllSatellitesData allSatelites)
        {
            //use the TopSecret exercice required response Data from the response.
            TopSecretResponseData topSecretResponseData = new TopSecretResponseData();

            //Get Location
            ShipLocator   shipLocator  = new ShipLocator();
            XYCoordinates shipLocation = shipLocator.GetLocation(allSatelites);

            topSecretResponseData.position.x = shipLocation.GetCoordinateX();
            topSecretResponseData.position.y = shipLocation.GetCoordinateY();

            ///Get Message
            MessageAssembler      messageAssembler       = new MessageAssembler();
            List <List <string> > messagesFromSatellites = messageAssembler.GetMessagesListFromSatelliteData(allSatelites);

            topSecretResponseData.message = messageAssembler.GetMessage(messagesFromSatellites);

            return(topSecretResponseData);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// This Method determine the position of a point in a 2D Dimension from the 3 distances to 3 known points (trilateration algorithm).
        /// TODO: Look for a better solution. It can not determine when distances and known points given are incorrect. The results from wrong given points will be not correct.
        /// </summary>
        /// <param name="knownPoint1"></param>
        /// <param name="distance1"></param>
        /// <param name="knownPoint2"></param>
        /// <param name="distance2"></param>
        /// <param name="knownPoint3"></param>
        /// <param name="distance3"></param>
        /// <returns></returns>
        public static XYCoordinates TrilaterationSolver(
            XYCoordinates knownPoint1, float distance1,
            XYCoordinates knownPoint2, float distance2,
            XYCoordinates knownPoint3, float distance3)
        {
            float[] point1 = new float[2];
            float[] point2 = new float[2];
            float[] point3 = new float[2];
            float[] auxX   = new float[2];
            float[] auxY   = new float[2];
            float[] p3p1   = new float[2];
            float   temp   = 0;
            float   temp2;
            float   p3p1i = 0;
            float   xvalue;
            float   yvalue;
            float   jvalue = 0;
            float   ivalue = 0;
            float   d;
            float   finalPointX;
            float   finalPointY;

            //Get vectors from points
            point1[0] = knownPoint1.GetCoordinateX();
            point1[1] = knownPoint1.GetCoordinateY();
            point2[0] = knownPoint2.GetCoordinateX();
            point2[1] = knownPoint2.GetCoordinateY();
            point3[0] = knownPoint3.GetCoordinateX();
            point3[1] = knownPoint3.GetCoordinateY();


            for (int i = 0; i < point1.Length; i++)
            {
                temp2 = point2[i] - point1[i];
                temp += (temp2 * temp2);
            }
            d = (float)Math.Sqrt(temp);
            for (int i = 0; i < point1.Length; i++)
            {
                auxX[i] = (float)(point2[i] - point1[i]) / (float)(Math.Sqrt(temp));;
            }
            for (int i = 0; i < point3.Length; i++)
            {
                p3p1[i] = point3[i] - point1[i];
            }
            for (int i = 0; i < auxX.Length; i++)
            {
                ivalue += (auxX[i] * p3p1[i]);
            }
            for (int i = 0; i < point3.Length; i++)
            {
                temp2  = point3[i] - point1[i] - (auxX[i] * ivalue);
                p3p1i += (temp2 * temp2);
            }
            for (int i = 0; i < point3.Length; i++)
            {
                auxY[i] = (point3[i] - point1[i] - (auxX[i] * ivalue)) / (float)Math.Sqrt(p3p1i);;
            }
            for (int i = 0; i < auxY.Length; i++)
            {
                jvalue += (auxY[i] * p3p1[i]);
            }
            xvalue = (float)(Math.Pow(distance1, 2) - Math.Pow(distance2, 2) + Math.Pow(d, 2)) / (2 * d);
            yvalue = (float)((Math.Pow(distance1, 2) - Math.Pow(distance3, 2) + Math.Pow(ivalue, 2) + Math.Pow(jvalue, 2)) / (2 * jvalue)) - ((ivalue / jvalue) * xvalue);

            finalPointX = knownPoint1.GetCoordinateX() + (auxX[0] * xvalue) + (auxY[0] * yvalue);
            finalPointY = knownPoint1.GetCoordinateY() + (auxX[1] * xvalue) + (auxY[1] * yvalue);

            //TODO: Here I clould at least check if the 2D point resulted match with the others points distances.

            XYCoordinates location = new XYCoordinates(finalPointX, finalPointY);

            return(location);
        }