Point class : Class Definition « Class Interface « C# / C Sharp






Point class

        

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GeoSyndication.Common
{
    public class Point
    {
        #region Private Fields

        private readonly double longitude;
        private readonly double latitude;
        private readonly double altitude;
        private string format = "{0} {1}";      // Default Setting, lat, lng 

        #endregion

        #region Public Properties

        public double Longitude
        {
            get { return longitude; }
        }

        public double Latitude
        {
            get { return latitude; }
        }

        public double Altitude
        {
            get { return altitude; }
        }

        public string Format
        {
            get
            {
                return format.Replace("{0}", "{lat}").Replace("{1}", "{lng}").Replace("{2}", "{alt}");
            }

            set
            {
                this.format = value.Replace("{lat}", "{0}").Replace("{lng}", "{1}").Replace("{alt}", "{2}");
            }
        } 

        #endregion

        #region Constructor

        public Point(double latitude, double longitude)
        {
            this.latitude = latitude;
            this.longitude = longitude;
        }

        public Point(double latitude, double longitude, double altitude)
        {
            this.latitude = latitude;
            this.longitude = longitude;
            this.altitude = altitude;
        } 

        #endregion

        #region Public Methods

        public override string ToString()
        {
            string retVal = string.Empty;

            retVal = string.Format(format, latitude.ToString(), longitude.ToString(), altitude.ToString());

            return retVal;
        }

        public static Point[] ParsePoints(string line)
        {
            List<Point> _line = new List<Point>();
            string[] s = line.Split(' ');

            if (s.Length % 2 != 0)
            {
                throw new ArgumentException("There must be an even number of points in a line or polygon");
            }

            for (int x = 0; x < s.Length + 1 / 2; x += 2)
            {
                Point p = new Point(double.Parse(s[x]), double.Parse(s[x + 1]));
                _line.Add(p);
            }

            return _line.ToArray();
        }

        public static Point[] ParsePoints(string line, string format)
        {
            List<Point> _line = new List<Point>();
            string[] s = line.Split(' ');

            for (int x = 0; x < s.Length + 1 / 2; x += 2)
            {
                Point p = new Point(double.Parse(s[x]), double.Parse(s[x + 1]));
                p.Format = format;
                _line.Add(p);
            }

            return _line.ToArray();
        } 

        #endregion
    }
}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.simulate a bank account
2.Using Initializers
3.A simple inventory exampleA simple inventory example
4.Declaring and Defining Classes
5.Declaring Class Instances
6.Assign value to classAssign value to class
7.Declare class and use itDeclare class and use it
8.Illustrates how to declare classes, object references, and create objectsIllustrates how to declare classes, object references, and create objects
9.Illustrates how to assign default values to fields using initializersIllustrates how to assign default values to fields using initializers
10.illustrates how to use a 'has a' relationshipillustrates how to use a 'has a' relationship
11.Illustrates nested classesIllustrates nested classes
12.Multiple constructors in a class definitionMultiple constructors in a class definition
13.Demonstrate the use of a nested class to contain dataDemonstrate the use of a nested class to contain data
14.Show name hiding in a derived classShow name hiding in a derived class
15.Illustrates hidingIllustrates hiding
16.Variable in and out a classVariable in and out a class
17.Create classCreate class
18.A program that uses the Building classA program that uses the Building class
19.This program creates two Building objectsThis program creates two Building objects
20.Return an objectReturn an object
21.Uses a class from Example16_3a.cs
22.A Simple C# ClassA Simple C# Class
23.Persons Info class
24.Message class