Advertisement
NPSF3000

CullingSystem

Dec 27th, 2011
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6.  
  7. //  Very simple, very stupid - but demonstrates a point about paying attention to real world data, not theoretical n^2
  8. namespace CullingSystem
  9. {
  10.     class Program
  11.     {
  12.  
  13.         static int pointCount = 1000;
  14.  
  15.  
  16.         struct Vector3
  17.         {
  18.             public float x;
  19.             public float y;
  20.             public float z;
  21.  
  22.             public Vector3(float x, float y, float z)
  23.             {
  24.                 this.x = x;
  25.                 this.y = y;
  26.                 this.z = z;
  27.             }
  28.             public Vector3(float randomDistance)
  29.             {
  30.                 this.x = (float)rnd.NextDouble() * randomDistance;
  31.                 this.y = (float)rnd.NextDouble() * randomDistance;
  32.                 this.z = (float)rnd.NextDouble() * randomDistance;
  33.             }
  34.  
  35.             public static Vector3 operator -(Vector3 a, Vector3 b)
  36.             {
  37.                 return new Vector3(a.x - b.x, a.y - b.y, a.z - b.z);
  38.             }
  39.  
  40.             public float sqrMagnitude()
  41.             {
  42.                 return x * x + y * y + z * z;
  43.             }
  44.         }
  45.  
  46.  
  47.  
  48.         struct CullingData
  49.         {
  50.             public Vector3 position;
  51.             public float sqrSight;
  52.  
  53.             public CullingData(Vector3 position, float sight)
  54.             {
  55.                 this.position = position;
  56.                 this.sqrSight = sight * sight;
  57.             }
  58.  
  59.         }
  60.         static Random rnd = new Random();
  61.  
  62.         static void Main(string[] args)
  63.         {
  64.             while (true)
  65.             {
  66.                 Console.ReadLine();
  67.  
  68.                 var data = new CullingData[pointCount];
  69.  
  70.                 for (int i = 0; i < pointCount; i++)
  71.                     data[i] = new CullingData(new Vector3(1000), rnd.Next(50, 100));
  72.  
  73.                 var sw = Stopwatch.StartNew();
  74.  
  75.                 var pairs = new List<CullingData[]>();
  76.  
  77.                 foreach (var point in data)
  78.                     foreach (var opoint in data)
  79.                         if ((point.position - opoint.position).sqrMagnitude() < point.sqrSight) pairs.Add(new[] { point, opoint });
  80.  
  81.                 sw.Stop();
  82.  
  83.                 Console.WriteLine("Time Elapsed: {0}ms, pairs found: " + pairs.Count, sw.ElapsedTicks * 1000f / Stopwatch.Frequency);
  84.             }
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement