Exemplo n.º 1
0
        /// <summary>
        /// Calculates a trajectory for a projectile with the given parameters: velocity, angle & initial height.
        /// </summary>
        /// <param name="projectile">the projectile a trajectory needs to be calculated for.</param>
        /// <param name="velocity">measured in meters per second.</param>
        /// <param name="angle">measured in degrees.</param>
        /// <param name="initialHeight">the height of the projectile being launched from.</param>
        /// <param name="trajectorySteps">the amount of steps a trajectory needs to be calculated for.</param>
        public Trajectory CalculateTrajectoryWithDrag(IProjectile projectile, double velocity, double angle, double initialHeight = 0)
        {
            VectorCollection vectors = new VectorCollection();

            vectors.Add(new Vector(0, initialHeight)); // adds the first position
            angle = DegreesToRadians(angle);
            double totalTime = 0;
            double xVelocity = velocity * Math.Cos(angle);
            double yVelocity = velocity * Math.Sin(angle);
            double x         = 0;
            double y         = initialHeight;
            double timeStep  = 0.001;

            for (int i = 0; y > -0.000_000_000_0001; i++)
            {
                double k = projectile.DragCoefficient * projectile.Area * AirDensityCalculator(y) / projectile.Mass / 8;
                // update current velocity to the previously calculated velocity
                velocity = CalculateNewVelocity(xVelocity, yVelocity);
                double ax = -k * velocity * xVelocity;
                double ay = -k * velocity * yVelocity - gravity;
                // calculate new velocity
                xVelocity = xVelocity + ax * timeStep;
                yVelocity = yVelocity + ay * timeStep;
                vectors.Add(CalculateVectorWithDrag(xVelocity, yVelocity, ref x, ref y, timeStep, i, ax, ay));
                totalTime += timeStep;
            }
            double impactAngle = CalculateImpactAngleWithDrag(initialHeight, angle, xVelocity, yVelocity);

            return(new Trajectory(vectors, Math.Round(totalTime, 4), Math.Round(impactAngle, 4), Math.Round(x, 4)));
        }
Exemplo n.º 2
0
        void ClassifyUntrainedData(IEnumerable <VectorCollection <T> > Groups)
        {
            Parallel.ForEach(UntrainedData, Node =>
            {
                object DistanceLock = new object();
                float ShortestValue = float.MaxValue;
                VectorCollection <T> ShortestGroup = null;

                Parallel.ForEach(Groups, Group =>
                {
                    float DistanceGet = Group.Center.GetEuclideanDistance(Node);

                    lock (DistanceLock)
                    {
                        if (DistanceGet < ShortestValue)
                        {
                            ShortestValue = DistanceGet;
                            ShortestGroup = Group;
                        }
                    }
                });

                Node.Tag = ShortestGroup.Tag;

                lock (ShortestGroup)
                {
                    ShortestGroup.Add(Node);
                }
            });
        }
Exemplo n.º 3
0
        public Trajectory CalculateTrajectoryV2(double velocity, double angle, double initialHeight = 0)
        {
            VectorCollection vectors   = new VectorCollection(); // vectors of trajectory
            double           totalTime = 0;                      // total time in air in seconds
            double           distance  = 0;                      // total distance travelled in meters on X-axis

            angle = DegreesToRadians(angle);                     // degrees to radians
            double xVelocity, yVelocity, ax, ay, x = 0, y = 0;

            xVelocity = velocity * Math.Cos(angle);
            yVelocity = velocity * Math.Sin(angle);
            double dt = 0.001;

            for (int i = 0; y > -0.0000000000001; i++)
            {
                ax        = 0;
                ay        = -gravity;
                xVelocity = xVelocity + ax * dt;
                yVelocity = yVelocity + ay * dt;
                x         = x + xVelocity * dt + ax * dt * dt;
                y         = y + yVelocity * dt + ay * dt * dt;
                vectors.Add(CalculateVector(velocity, angle, y, totalTime));
                totalTime += dt;
            }
            double temp = Math.Sqrt((velocity * velocity) * (Math.Sin(angle) * Math.Sin(angle)) + 2 * gravity * initialHeight);

            double impactAngle = CalculateImpactAngle(velocity, angle, temp);

            return(new Trajectory(vectors, Math.Round(totalTime, 4), Math.Round(impactAngle, 4), Math.Round(distance, 4)));
        }
Exemplo n.º 4
0
        private const double gravity = 9.81; // 9.81 meters per second squared.

        /// <summary>
        /// Calculates a trajectory with the given parameters: velocity, angle & initial height.
        /// </summary>
        /// <param name="velocity">measured in meters per second.</param>
        /// <param name="angle">measured in degrees.</param>
        /// <param name="initialHeight">the height of the projectile being launched from.</param>
        /// <param name="trajectorySteps">the amount of steps a trajectory needs to be calculated for.</param>
        public Trajectory CalculateTrajectory(double velocity, double angle, double initialHeight = 0, double trajectorySteps = 1000)
        {
            VectorCollection vectors   = new VectorCollection(); // vectors of trajectory
            double           totalTime = 0;                      // total time in air in seconds
            double           distance  = 0;                      // total distance travelled in meters on X-axis

            angle = DegreesToRadians(angle);                     // degrees to radians

            double temp = Math.Sqrt((velocity * velocity) * (Math.Sin(angle) * Math.Sin(angle)) + 2 * gravity * initialHeight);

            if (initialHeight == 0)                                                  // even ground
            {
                totalTime = (velocity * 2 * Math.Sin(angle)) / gravity;              // seconds in air
                distance  = ((velocity * velocity) / gravity) * Math.Sin(angle * 2); // meters travelled over X-axis
            }
            else // uneven ground
            {
                totalTime = (velocity * Math.Sin(angle) + temp) / gravity;                                  // seconds in air
                distance  = (velocity * Math.Cos(angle) / gravity) * ((velocity * Math.Sin(angle)) + temp); // meters travelled over X-axis
            }
            double stepper = totalTime / trajectorySteps;

            for (double currentTime = 0; Math.Round(currentTime, 10) <= Math.Round(totalTime, 10); currentTime += stepper)
            {
                vectors.Add(CalculateVector(velocity, angle, initialHeight, currentTime));
            }
            double impactAngle = CalculateImpactAngle(velocity, angle, temp);

            return(new Trajectory(vectors, Math.Round(totalTime, 4), Math.Round(impactAngle, 4), Math.Round(distance, 4)));
        }
        /// <summary cref="Object.MemberwiseClone" />
        public new object MemberwiseClone()
        {
            VectorCollection clone = new VectorCollection(this.Count);

            for (int ii = 0; ii < this.Count; ii++)
            {
                clone.Add((Vector)Utils.Clone(this[ii]));
            }

            return(clone);
        }
Exemplo n.º 6
0
            /// <summary>
            /// Gets the vectors with a specific value count
            /// </summary>
            /// <param name="count">Count of element in the vector values</param>
            /// <returns>Vector collection</returns>
            public VectorCollection GetVectorsCountValues(int count)
            {
                var vc = new VectorCollection();

                if (count > GetMaxCountValues())
                {
                    return(vc);
                }
                foreach (var vec in this)
                {
                    if (vec.Values.Count == count)
                    {
                        vc.Add(vec);
                    }
                }
                return(vc);
            }
Exemplo n.º 7
0
        /// <summary>
        /// Simplifies and combines the matrix
        /// </summary>
        /// <returns>Result group collection instance</returns>
        public ResultGroupCollection Simplify()
        {
            var vectors = new VectorCollection();

            foreach (var elm in Matrix.Keys)
            {
                var vect = vectors.GetVectorByKey(elm.Name);
                if (vect == null)
                {
                    vect = new Vector {
                        Key = elm.Name
                    };
                    vectors.Add(vect);
                }
                vect.Values.Add(Matrix[elm]);
            }

            var maxValues    = vectors.GetMaxCountValues();
            var resultGroups = new ResultGroupCollection();

            while (maxValues > 0)
            {
                if (vectors.Count == 1)
                {
                    var rgroup = new ResultGroup();
                    rgroup.Keys.Add(vectors[0].Key);
                    rgroup.Values.AddRange(vectors[0].Values);
                    resultGroups.Add(rgroup);
                }
                else
                {
                    var rgroup = new ResultGroup();
                    for (var i = 0; i < vectors.Count; i++)
                    {
                        var vecX = vectors[i];

                        for (var j = i + 1; j < vectors.Count; j++)
                        {
                            var vecY = vectors[j];
                            var elementsCoincidentes = new List <string>();
                            var numIgualdades        = 0;
                            foreach (var elmX in vecX.Values)
                            {
                                foreach (var elmY in vecY.Values)
                                {
                                    if (elmX != elmY)
                                    {
                                        continue;
                                    }
                                    numIgualdades++;
                                    if (!elementsCoincidentes.Contains(elmX))
                                    {
                                        elementsCoincidentes.Add(elmX);
                                    }
                                }
                            }
                            if (numIgualdades == maxValues)
                            {
                                if (rgroup.Values.Count == 0)
                                {
                                    rgroup.Values.AddRange(elementsCoincidentes);
                                    rgroup.Keys.Add(vecX.Key);
                                    rgroup.Keys.Add(vecY.Key);
                                }
                                else
                                {
                                    var igualdadesgrupo = 0;
                                    foreach (var elmX in rgroup.Values)
                                    {
                                        foreach (var elmY in elementsCoincidentes)
                                        {
                                            if (elmX == elmY)
                                            {
                                                igualdadesgrupo++;
                                            }
                                        }
                                    }
                                    if (igualdadesgrupo != maxValues)
                                    {
                                        if (rgroup.Keys.Count > 0)
                                        {
                                            resultGroups.Add(rgroup);
                                        }
                                        rgroup = new ResultGroup();
                                        rgroup.Values.AddRange(elementsCoincidentes);
                                        rgroup.Keys.Add(vecX.Key);
                                        rgroup.Keys.Add(vecY.Key);
                                    }
                                    else if (igualdadesgrupo == maxValues)
                                    {
                                        var xAdded = false;
                                        foreach (var key in rgroup.Keys)
                                        {
                                            if (key == vecX.Key)
                                            {
                                                xAdded = true;
                                            }
                                        }
                                        if (!xAdded)
                                        {
                                            rgroup.Keys.Add(vecX.Key);
                                        }
                                        var yAdded = false;
                                        foreach (var key in rgroup.Keys)
                                        {
                                            if (key == vecY.Key)
                                            {
                                                yAdded = true;
                                            }
                                        }
                                        if (!yAdded)
                                        {
                                            rgroup.Keys.Add(vecY.Key);
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if (maxValues == vecX.Values.Count && maxValues > vecY.Values.Count)
                                {
                                    if (rgroup.Keys.Count > 0 && !resultGroups.ContainsRGroup(vecX))
                                    {
                                        resultGroups.Add(rgroup);
                                    }
                                    rgroup = new ResultGroup();
                                    rgroup.Values.AddRange(vecX.Values);
                                    rgroup.Keys.Add(vecX.Key);
                                }
                                if (maxValues == vecY.Values.Count && maxValues > vecX.Values.Count)
                                {
                                    if (rgroup.Keys.Count > 0 && !resultGroups.ContainsRGroup(vecY))
                                    {
                                        resultGroups.Add(rgroup);
                                    }
                                    rgroup = new ResultGroup();
                                    rgroup.Values.AddRange(vecY.Values);
                                    rgroup.Keys.Add(vecY.Key);
                                }
                            }
                        }
                    }
                    if (rgroup.Keys.Count > 0 && rgroup.Values.Count > 0)
                    {
                        resultGroups.Add(rgroup);
                    }
                }
                maxValues--;
            }
            return(resultGroups);
        }