예제 #1
0
        public void RandomizeParticles()
        {
            IGenerateRandom random = new MersenneTwisterGenerateRandom();

            for (int i = 0; i < ParticleCount; i++)
            {
                var p = new Particle(2);
                p.Location[0] = random.NextDouble(OutputCanvas.ActualWidth);
                p.Location[1] = random.NextDouble(OutputCanvas.ActualHeight);
                p.Velocity[0] = 3;
                p.Velocity[1] = random.NextDouble(2.0*Math.PI);
                _particles.Add(p);
            }
        }
예제 #2
0
        /// <summary>
        /// Find the nearest neighbor particle.
        /// </summary>
        /// <param name="target">The particle to look for neighbors to.</param>
        /// <param name="particles">All particles.</param>
        /// <param name="k">The number of particles to find.</param>
        /// <param name="maxDist">The max distance to check.</param>
        /// <returns>The nearest neighbors.</returns>
        private IList<Particle> FindNearest(Particle target, IEnumerable<Particle> particles, int k, double maxDist)
        {
            IList<Particle> result = new List<Particle>();
            var tempDist = new double[k];
            int worstIndex = -1;

            foreach (Particle particle in particles)
            {
                if (particle == target)
                {
                    continue;
                }
                double d = _distanceCalc.Calculate(particle.Location, target.Location);

                if (d > maxDist)
                {
                    continue;
                }

                if (result.Count < k)
                {
                    tempDist[result.Count] = d;
                    result.Add(particle);
                    worstIndex = MaxIndex(tempDist);
                }
                else if (d < tempDist[worstIndex])
                {
                    tempDist[worstIndex] = d;
                    result[worstIndex] = particle;
                    worstIndex = MaxIndex(tempDist);
                }
            }

            return result;
        }