Esempio n. 1
0
 private void _removeFromCollection(List <WSMCountry> collection, WSMCountry cDef0)
 {
     collection.Remove(cDef0);
     // also remove all occurances in neightbour lists
     foreach (var cDef1 in collection)
     {
         cDef1.NeighborList.RemoveAll(cd => object.Equals(cd, cDef0));
     }
 }
        internal WSMCountry ShallowCopy()
        {
            WSMCountry copy = new WSMCountry(Name);

            copy.Collection         = Collection;
            copy.HasInternalBorders = HasInternalBorders;
            copy.Brush        = Brush;
            copy.NeighborList = new List <WSMCountry>(NeighborList);
            return(copy);
        }
Esempio n. 3
0
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            double x, y, lat, lon;

            _getXY(e.Location, out x, out y);
            _ortho.ToSphere(x, y, out lat, out lon);
            string     text    = null;
            WSMCountry cDef    = _ttC.GetDataAtPointer(e) as WSMCountry;
            string     country = (cDef == null ? "Sea" : cDef.Name);

            if (!double.IsNaN(lat) && !double.IsNaN(lon))
            {
                text = "Latitude:" + GeoCoordinateUtilities.FormatLatitude(lat) + "   Longitude:" +
                       GeoCoordinateUtilities.FormatLongitude(lon);
                _pointerGeoCoordinate = new GeoCoordinate(lat, lon);
            }
            else
            {
                _pointerGeoCoordinate = null;
                country = "SPACE";
            }
            text        += ("   [" + country + "]");
            _pointerText = text;

            if (!toolStripButtonPICK.Checked && _mdOrtho != null)
            {
                // moving view
                double deltaDeg = 90.0 / _worldScreenRadius;
                double φ0       = _mdOrtho.φ0 + deltaDeg * (e.Location.Y - _mdsy);
                if (φ0 > 90)
                {
                    φ0 = 90;
                }
                if (φ0 < -90)
                {
                    φ0 = -90;
                }
                double λ0 = _mdOrtho.λ0 - deltaDeg * (e.Location.X - _mdsx);
                λ0 = OrthographicProjection.SanitizeLongitude(λ0);
                trackBar1.Value = (int)Math.Round(φ0 * 100);
                trackBar2.Value = (int)Math.Round(λ0 * 100);
                _updateProjection();
                _invalidateAllLayers(true);
                return;
            }

            // not moving view
            _mouseOverCountry    = cDef;
            _layerDynamic1.Dirty = true;
            _layerDynamic2.Dirty = true;
            pictureBox1.Refresh();
        }
Esempio n. 4
0
        public void Read()
        {
            Collection.Clear();
            System.IO.StringReader stream = new System.IO.StringReader(Properties.Resources.countryBoundaries);
            using (TextFieldParser parser = new TextFieldParser(stream))
            {
                parser.TextFieldType = FieldType.Delimited;
                parser.SetDelimiters(",");
                while (!parser.EndOfData)
                {
                    // Processing row
                    string[] fields = parser.ReadFields();

                    if (fields != null)
                    {
                        string     country = fields[6];
                        WSMCountry cDef    = new WSMCountry(country)
                        {
                            Brush = null
                        };

                        _initPolyInfo(fields[0]);
                        while (true)
                        {
                            WSMPolygon poly = _getNextPoly();
                            if (poly == null)
                            {
                                break;
                            }
                            cDef.AddPolygon(poly);
                            if (poly.Polytype == 1)
                            {
                                cDef.HasInternalBorders = true;
                            }
                        }


                        if (cDef.Collection.Count > 0)
                        {
                            if (cDef.Collection.Count > 1)
                            {
                                cDef.Collection.Sort((p1, p2) => (p1.Polytype - p2.Polytype));
                            }


                            cDef.Finish();
                            Collection.Add(cDef);
                        }
                    }
                }
            }
        }
Esempio n. 5
0
        Region _internalBorderRegion(WSMCountry cDef)
        {
            Region reg = null;

            foreach (WSMPolygon poly in cDef.Collection)
            {
                if (!poly.Draw)
                {
                    continue;
                }

                if (!poly.IsInView(_zoomRadius * 2))
                {
                    continue;
                }


                var _poly = _getPoly(poly);
                if (_poly == null)
                {
                    continue;
                }

                GraphicsPath gp = new GraphicsPath();
                gp.AddPolygon(_poly);

                if (poly.Polytype == 0)
                {
                    // external
                    if (reg == null)
                    {
                        reg = new Region(gp);
                    }
                    else
                    {
                        reg.Union(gp);
                    }
                }
                else
                {
                    // remove internal
                    reg?.Exclude(gp);
                }
            }

            return(reg);
        }
Esempio n. 6
0
 void _drawCountryOutline(Graphics g, WSMCountry cDef, Pen pen)
 {
     // outlines
     foreach (WSMPolygon poly in cDef.Collection)
     {
         if (!poly.Draw)
         {
             continue;
         }
         PointF[] _poly = _getPoly(poly);
         if (_poly == null)
         {
             continue;
         }
         g.DrawLines(pen, _poly);
     }
 }
        public bool IsNeighbour(WSMCountry countryDefinition2)
        {
            if (MinLatitude > countryDefinition2.MaxLatitude)
            {
                return(false);
            }
            if (MaxLatitude < countryDefinition2.MinLatitude)
            {
                return(false);
            }

            foreach (WSMPolygon poly1 in Collection)
            {
                foreach (WSMPolygon poly2 in countryDefinition2.Collection)
                {
                    if (poly1.MinLatitude > poly2.MaxLatitude)
                    {
                        continue;
                    }
                    if (poly1.MaxLatitude < poly2.MinLatitude)
                    {
                        continue;
                    }

                    foreach (var pt1 in poly1.Collection)
                    {
                        foreach (var pt2 in poly2.Collection)
                        {
                            if (pt1.Latitude == pt2.Latitude && pt1.Longitude == pt2.Longitude)
                            {
                                return(true);
                            }
                        }
                    }
                }
            }

            return(false);
        }
Esempio n. 8
0
        void _color()
        {
            //if (!madeNN)
            //{
            //    for (int i = 0; i < _collection.Count; i++)
            //        for (int j = i + 1; j < _collection.Count; j++)
            //        {
            //            if (_collection[i].IsNeighbour(_collection[j]))
            //            {
            //                _collection[i].NeighborList.Add(_collection[j]);
            //                _collection[j].NeighborList.Add(_collection[i]);
            //            }
            //        }
            //    madeNN = true;
            //}

            List <Point> list = new List <Point>();

            for (int i = 0; i < Collection.Count; i++)
            {
                Collection[i].NeighborList.Clear();
                for (int j = i + 1; j < Collection.Count; j++)
                {
                    list.Add(new Point(i, j));
                }
            }
            var sync = new object();

            Parallel.For(0, list.Count, index =>
            {
                Point pt = list[index];
                if (Collection[pt.X].IsNeighbour(Collection[pt.Y]))
                {
                    //
                    lock (sync)
                    {
                        Collection[pt.X].NeighborList.Add(Collection[pt.Y]);
                        Collection[pt.Y].NeighborList.Add(Collection[pt.X]);
                    }
                }
            });



            // safe copy to retain neighbours
            List <WSMCountry> copyCollection = new List <WSMCountry>();

            foreach (var cDef0 in Collection)
            {
                cDef0.Tag   = null;
                cDef0.Brush = null;
                WSMCountry copy = cDef0.ShallowCopy();
                // point to each other
                cDef0.Tag = copy;
                copy.Tag  = cDef0;
                copyCollection.Add(copy);
            }

            List <WSMCountry> stack = new List <WSMCountry>();


            // Choose one node (state) on the graph with less than five neighbours and remove it from the graph
            bool inAction = true;

            while (inAction)
            {
                inAction = false;

                for (int nnMin = 4; nnMin >= 0 && !inAction; nnMin--)
                {
                    foreach (var cDef0 in Collection)
                    {
                        if (cDef0.NeighborList.Count == nnMin)
                        {
                            _removeFromCollection(Collection, cDef0);
                            stack.Add(cDef0);
                            inAction = true;
                            break;
                        }
                    }
                }
            }

            // all nodes gone!
            // 5.Add the nodes back the graph in reverse order from which you removed them(think stack here).
            for (int i = stack.Count - 1; i >= 0; i--)
            {
                WSMCountry cDef0 = stack[i];
                Collection.Add(cDef0);
                // resore neighbour list as before
                WSMCountry copy = cDef0.Tag as WSMCountry;
                cDef0.Tag = null;
                if (copy == null)
                {
                    System.Diagnostics.Debug.WriteLine("FAILED!!"); return;
                }
                cDef0.NeighborList = copy.NeighborList;

                //  6.Colour the added node with a colour that is not used by any of its current neighbours.
                List <Brush> bList = _initRandomBrush();
                int          ic    = 0;
                while (cDef0.Brush == null && ic < bList.Count)
                {
                    Brush brush = bList[ic++];

                    bool brushOk = true;
                    foreach (var cDef in cDef0.NeighborList)
                    {
                        if (cDef.Brush == null)
                        {
                            continue;                     // not colored yet
                        }
                        if (brush == cDef.Brush)
                        {
                            brushOk = false;                      // this one is used
                        }
                    }
                    if (brushOk)
                    {
                        cDef0.Brush = brush;
                    }
                }

                if (cDef0.Brush == null)
                {
                    cDef0.Brush = Brushes.DarkSalmon;
                    System.Diagnostics.Debug.WriteLine("FAILED!!" + cDef0.Name);
                }
            }


            // islands.. find best color (next furthest away from anything else)
            foreach (var cDef0 in Collection)
            {
                if (cDef0.NeighborList.Count == 0)
                {
                    GeoCoordinate     gc0          = cDef0.CentralCoordinate;
                    List <WSMCountry> distanceList = new List <WSMCountry>();
                    // get list of nearest countries
                    foreach (var cDef1 in Collection)
                    {
                        if (cDef0 == cDef1)
                        {
                            continue;
                        }
                        GeoCoordinate gc1      = cDef1.CentralCoordinate;
                        double        distance = gc0.GetDistanceTo(gc1);
                        distanceList.Add(cDef1);
                        cDef1.Distance = distance;
                    }

                    // order near to far
                    distanceList.Sort(delegate(WSMCountry p1, WSMCountry p2)
                    {
                        if (p1.Distance > p2.Distance)
                        {
                            return(1);
                        }
                        if (p2.Distance > p1.Distance)
                        {
                            return(-1);
                        }
                        return(0);
                    });

                    // full colour list
                    List <Brush> bList = _initRandomBrush();

                    // remove colours until only one remains
                    // that color is furthest
                    foreach (WSMCountry cDef1 in distanceList)
                    {
                        bList.Remove(cDef1.Brush);
                        if (bList.Count == 1)
                        {
                            break;
                        }
                    }

                    cDef0.Brush = bList[0];
                }
            }
        }