Beispiel #1
0
 /// <summary>Печатает пару координат x и y заданным цветом</summary>
 /// <remarks>После завершения метода цвет консоли будет таким же как до её вызова</remarks>
 private static void PrintCoordinates(Point2D point)
 {
     ConsoleColor oldForegoundColor = Console.ForegroundColor;
     Console.ForegroundColor = point.Color;
     Console.Write("({0},{1})", point.X, point.Y);
     Console.ForegroundColor = oldForegoundColor;
 }
Beispiel #2
0
 public void PrintDistance(Point2D other)
 {
     Console.Write("Расстояние между точками ");
     PrintCoordinates(this);
     Console.Write(" и ");
     PrintCoordinates(other);
     Console.WriteLine(" = {0:F3}", CalculateDistance(other));
 }
Beispiel #3
0
        static void Main(string[] args)
        {
            // Пример сортировки.
            // Класс List<T> способен отсортировать экземпляры Point2D благодаря наличиб реализации IComparable
            List<Point2D> points = new List<Point2D>();
            points.Add(new Point2D(5, 5));
            points.Add(new Point2D(2, 2));
            points.Add(new Point2D(4, 4));
            points.Add(new Point2D(1, 1));
            points.Add(new Point2D(3, 3));

            Console.WriteLine("До сортировки:");
            foreach (Point2D point in points)
            {
                Console.WriteLine("({0}, {1})", point.X, point.Y);
            }

            points.Sort();
            Console.WriteLine("После сортировки:");
            foreach (Point2D point in points)
            {
                Console.WriteLine("({0}, {1})", point.X, point.Y);
            }

            Console.WriteLine("--------------");
            Console.WriteLine("Пример форматированного вывода");

            // Форматированный вывод используя собственную реализацию IFormattable
            Console.WriteLine(Thread.CurrentThread.CurrentCulture.Name);
            Point2D p1 = new Point2D(-1, -2);
            Console.WriteLine("{0}", p1);
            Console.WriteLine("{0:R}", p1);
            Console.WriteLine("{0:S}", p1);
            Console.WriteLine("{0:C}", p1);
            Console.WriteLine();

            // На форматированный вывод влияет текущая культура
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-NZ");
            Console.WriteLine(Thread.CurrentThread.CurrentCulture.Name);

            Console.WriteLine("{0}", p1);
            Console.WriteLine("{0:R}", p1);
            Console.WriteLine("{0:S}", p1);
            Console.WriteLine("{0:C}", p1);
            //Console.WriteLine("{0:X}", p1);
        }
Beispiel #4
0
        static void Main()
        {
            Point2D point1 = new Point2D();
            point1.X = 10; point1.Y = -2;
            //point1.Color = ConsoleColor.Red;

            Point2D point2 = new Point2D();
            point2.X = 5; point2.Y = 1;
            //point2.Color = ConsoleColor.Yellow;

            Point2D point3 = new Point2D();
            point3.X = -2; point3.Y = 7;
            //point3.Color = ConsoleColor.Cyan;

            point1.PrintDistance(point2);
            point2.PrintDistance(point3);
            point3.PrintDistance(point1);
        }
Beispiel #5
0
        static void Main()
        {
            Point2D p1 = new Point2D(10,7);
            Point3D p2 = new Point3D(10, 7, -5);
            Point3D p3 = new Point3D(10, 7, 5);

            // Переменная p1 имеет тип Point2D, p2 - Point3D
            // Функция CalculateDistance из класса Point2D принимает аргумент типа Point2D
            // Т.к. класс Point3D является наследником Point3D, то этот вызов является допустимым
            // Фактически мы вычисляем растояние между двумя Point2D игнорируя координату Z
            Console.WriteLine(p1.CalculateDistance(p2)); // Результат - 0

            // Аналогично первому вызову
            Console.WriteLine(p2.CalculateDistance(p1)); // Результат - 0

            // Обе переменные имеют тип Point3D
            // Вызывается функция CalculateDistance класса Point3D
            Console.WriteLine(p2.CalculateDistance(p3)); // Результат - 10
        }
Beispiel #6
0
        static void Main()
        {
            Point2D point1; // Т.к. Point2D это struct, то new не нужен
            point1.X = 10; point1.Y = -2;
            point1.Color = ConsoleColor.Red;

            Point2D point2;
            point2.X = 5; point2.Y = 1;
            point2.Color = ConsoleColor.Yellow;

            // Экземпляры структур допускается создавать используя new()
            Point2D point3 = new Point2D();
            point3.X = -2; point3.Y = 7;
            point3.Color = ConsoleColor.Cyan;

            point1.PrintDistance(point2);
            point2.PrintDistance(point3);
            point3.PrintDistance(point1);
        }
Beispiel #7
0
 public double CalculateDistance(Point2D other)
 {
     return Math.Sqrt((other.X - this.X) * (other.X - this.X) + (other.Y - this.Y) * (other.Y - this.Y));
 }
Beispiel #8
0
 public double CalculateDistance(Point2D other)
 {
     return Math.Sqrt(Square(other.X - X) + Square(other.Y - Y));
 }
Beispiel #9
0
 public void PrintDistance(Point2D other)
 {
     Console.WriteLine("Расстояние между точками ({0},{1}) и ({2},{3}) = {4:F3}", X, Y, other.X, other.Y, CalculateDistance(other));
 }
Beispiel #10
0
 public Rectangle2D(int width, int height)
 {
     TopLeft = new Point2D();
     Width = width;
     Height = height;
 }
Beispiel #11
0
 static void Main(string[] args)
 {
     Point2D p1 = new Point2D(1,1);
     Point2D p2 = new Point2D(1, 1, ConsoleColor.White);
 }