Advertisement
NPSF3000

Unity Triangles

Dec 5th, 2011
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.92 KB | None | 0 0
  1.  
  2. //  All rights reservered - NPSF3000 2011 (c)
  3. //  NPSF3001 thisIsGarbage gmail.com
  4.  
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.Linq;
  8. using UnityEngine;
  9.  
  10. public class Triangles : MonoBehaviour
  11. {
  12.     /*  Sample Data  
  13.      *
  14.      * -340,495,-153,-910,835,-947
  15.      * -175,41,-421,-714,574,-645
  16.      * -547,712,-352,579,951,-786
  17.      * 419,-864,-83,650,-399,171
  18.      * -429,-89,-357,-930,296,-29
  19.      * ...
  20.      */
  21.  
  22.     void Update()
  23.     {
  24.         if (!Input.GetKeyDown(KeyCode.Return)) return;
  25.  
  26.         var sw = Stopwatch.StartNew();
  27.  
  28.         print(
  29.             (from triangle in
  30.                  from numberQ in
  31.                      from textLine in ((TextAsset)Resources.Load("triangles")).text.Split('\n')
  32.                      select new Queue<float>(
  33.                          from t in textLine.Split(',')
  34.                          select float.Parse(t))
  35.                  select (from p in Enumerable.Range(0, numberQ.Count / 2)
  36.                          select new { x = numberQ.Dequeue(), y = numberQ.Dequeue() }).ToList()
  37.  
  38.              where triangle.Count(x => x.y > 0) % 3 != 0
  39.  
  40.              let splitVert = (from vert in triangle
  41.                               group vert by vert.y >= 0 into grp
  42.                               orderby grp.Count() ascending
  43.                               select grp.ToArray()).ToArray()
  44.  
  45.              let intersects = (from end in splitVert[1]
  46.                                let start = splitVert[0][0]
  47.                                let m = (end.y - start.y) / (end.x - start.x)
  48.                                let b = start.y - m * start.x
  49.                                select -b / m).ToArray()
  50.  
  51.              where intersects.Count(x => x > 0) == 1
  52.  
  53.              group intersects by true into grp
  54.  
  55.              select grp.Count()).Max()
  56.          + " in " + sw.ElapsedMilliseconds + "ms"); // 60ms when compiled - 1000 triangles
  57.  
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement