Midterm 4

CSharp · Console

/Program.cs

// An array of N integer (x,y) pairs specifies the points on the plain.
// Find the path consisting of n-1 line segments at most
// that goes through all points and has no self-intersections.
// Print coordinates of original points along that path. Time is O(N log N).
using System;

public class Program
{
    public static void Main()
    {
		Point [] points = new []
		{ 
			new Point{ X=0,Y=0},
			new Point{ X=0,Y=2},
			new Point{ X=4,Y=5},
			new Point{ X=3,Y=0},
			new Point{ X=5,Y=2},
			new Point{ X=0,Y=6}
		};

		Array.Sort(points, ComparePoint);
		
		Console.WriteLine("No Self interaction sequence (X,Y) :");
		
        foreach (Point pt in points)
        {
            Console.WriteLine(pt.X + "," + pt.Y);
        }
	}
	
	// Compares 2 points lexicographically
    static int ComparePoint(Point a, Point b)
    {
        int byY = a.Y.CompareTo(b.Y);
		if (0 != byY)
			return byY;
		
		return a.X.CompareTo(b.X);
    }
	
	public class Point
    {
        public int X;
        public int Y;
    }
}
;