Exemplo n.º 1
0
        public void applyForces(ArborNode m, double g)
        {
            try
            {
                Queue <object> f = new Queue <object>();

                f.Enqueue(fRoot);
                while (f.Count > 0)
                {
                    object obj = f.Dequeue();
                    if (obj == null || obj == m)
                    {
                        continue;
                    }

                    ArborPoint ptx, i, k;
                    double     l, kMag, massx;

                    if (obj is ArborNode)
                    {
                        ArborNode node = (obj as ArborNode);
                        massx = node.Mass;
                        ptx   = node.Pt;

                        k    = m.Pt.sub(ptx);
                        kMag = k.magnitudeSquare();

                        i = ((kMag > 0) ? k : ArborPoint.newRnd(1)).normalize();
                        l = Math.Max(1, kMag);
                        m.applyForce(i.mul(g * massx).div(l));
                    }
                    else
                    {
                        Branch branch = (obj as Branch);
                        massx = branch.Mass;
                        ptx   = branch.Pt.div(massx);

                        k    = m.Pt.sub(ptx);
                        kMag = k.magnitudeSquare();

                        double h = branch.Size.X * branch.Size.Y;
                        if (h / kMag > fDist)
                        {
                            f.Enqueue(branch.Q[QNe]);
                            f.Enqueue(branch.Q[QNw]);
                            f.Enqueue(branch.Q[QSe]);
                            f.Enqueue(branch.Q[QSw]);
                        }
                        else
                        {
                            i = ((kMag > 0) ? k : ArborPoint.newRnd(1)).normalize();
                            l = Math.Max(1, kMag);
                            m.applyForce(i.mul(g * massx).div(l));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("BarnesHutTree.applyForces(): " + ex.Message);
            }
        }