Zing~

Project Euler problem 102~
c#

  1:     class Program
  2:     {
  3:         static void Main(string[] args)
  4:         {
  5:             Console.WriteLine("The number of triangles for which the interior contains the origin: ");
  6: 
  7:             TestBenchSetup();
  8:             TestBenchLoader(using_vector_cross_product_to_get_z_vector_direction);
  9: 
 10:             Console.WriteLine("press any key to exit");
 11:             Console.ReadKey();
 12:         }
 13: 
 14:         struct Point
 15:         {
 16:             public int x;
 17:             public int y;
 18:             public Point(int x, int y)
 19:             {
 20:                 this.x = x;
 21:                 this.y = y;
 22:             }
 23:         }
 24: 
 25:         /// <summary>
 26:         /// using the info from
 27:         /// http://stackoverflow.com/questions/2049582/how-to-determine-a-point-in-a-triangle
 28:         /// http://mathforum.org/library/drmath/view/54505.html
 29:         /// </summary>
 30:         static int using_vector_cross_product_to_get_z_vector_direction()
 31:         {
 32:             // funtion to check if the Up vector is point in/out to the screen
 33:             Func<Point, Point, Point, int> calculate_z_vector = (p1, p2, p3)
 34:                 => (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y);
 35:             // use the calculate_z_vector to find out z vector's direction(+/-)
 36:             var coords = new int[6];
 37:             var origin = new Point(0, 0);
 38:             var count = 0;
 39:             foreach (var line in File.ReadAllLines(@"..\..\triangles.txt"))
 40:             {
 41:                 coords = line.Split(new char[] { ',' }).Select(s => Convert.ToInt32(s)).ToArray();
 42:                 Point t1 = new Point(coords[0], coords[1]);
 43:                 Point t2 = new Point(coords[2], coords[3]);
 44:                 Point t3 = new Point(coords[4], coords[5]);
 45:                 // since the order of the point can be clockwise or counter-clockwise, test both scenario by
 46:                 // making sure the z vector sign is consistent respectively with other two sides
 47:                 var z1 = calculate_z_vector(origin, t1, t2) < 0;
 48:                 var z2 = calculate_z_vector(origin, t2, t3) < 0;
 49:                 var z3 = calculate_z_vector(origin, t3, t1) < 0;
 50: 
 51:                 if (z1 == z2 && z2 == z3)
 52:                     count++;
 53:             }
 54:             return count;
 55:         }
 56: 
 57:         static Stopwatch stopwatch = new Stopwatch();
 58:         static void TestBenchSetup()
 59:         {
 60:             // Uses the second Core or Processor for the Test
 61:             Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(2);
 62:             // Prevents "Normal" processes from interrupting Threads
 63:             Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
 64:             // Prevents "Normal" Threads from interrupting this thread
 65:             Thread.CurrentThread.Priority = ThreadPriority.Highest;
 66:         }
 67:         // see http://www.codeproject.com/KB/testing/stopwatch-measure-precise.aspx
 68:         static void TestBenchLoader(Func<int> test_method)
 69:         {
 70:             stopwatch.Reset();
 71:             stopwatch.Start();
 72:             long result = 0;
 73:             long avg_tick = 0;
 74:             long avg_ms = 0;
 75:             while (stopwatch.ElapsedMilliseconds < 1200)  // A Warmup of 1000-1500 ms
 76:             // stabilizes the CPU cache and pipeline.
 77:             {
 78:                 result = test_method(); // Warmup
 79:             }
 80:             stopwatch.Stop();
 81:             for (int repeat = 0; repeat < 20; ++repeat)
 82:             {
 83:                 stopwatch.Reset();
 84:                 stopwatch.Start();
 85:                 result = test_method();
 86:                 stopwatch.Stop();
 87:                 avg_tick += stopwatch.ElapsedTicks;
 88:                 avg_ms += stopwatch.ElapsedMilliseconds;
 89:             }
 90:             avg_tick = avg_tick / 20;
 91:             avg_ms = avg_ms / 20;
 92:             Console.WriteLine(string.Format("{0} way(ticks:{1}, ms:{2}) Ans:{3}",
 93:                 test_method.Method.Name.Replace('_', ' '), avg_tick, avg_ms, result));
 94:         }
 95:     }

Advertisements
This entry was posted in Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s