Skip to content

Instantly share code, notes, and snippets.

@marijnz
Last active March 4, 2019 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marijnz/e95263fe6e0a7e3efe04b5b807c284ff to your computer and use it in GitHub Desktop.
Save marijnz/e95263fe6e0a7e3efe04b5b807c284ff to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
using Unity.Collections;
using Unity.Jobs;
using Unity.Burst;
using UnityEngine;
using Debug = UnityEngine.Debug;
using static Unity.Mathematics.math;
using Unity.Mathematics;
[BurstCompile]
public struct FindPolygonContainingPoint : IJobParallelFor
{
[ReadOnly]
public NativeArray<float4> polygons;
[ReadOnly]
public float4 point;
[WriteOnly]
public NativeArray<int> result;
bool done;
// The code actually running on the job
public void Execute(int i)
{
if(done) return;
var p0 = polygons[i * 3 ];
var p1 = polygons[i * 3 + 1];
var p2 = polygons[i * 3 + 2];
var s = p0.y * p2.x - p0.x * p2.y + (p2.y - p0.y) * point.x + (p0.x - p2.x) * point.y;
var t = p0.x * p1.y - p0.y * p1.x + (p0.y - p1.y) * point.x + (p1.x - p0.x) * point.y;
if ((s < 0) == (t < 0))
{
var a = -p1.y * p2.x + p0.y * (p2.x - p1.x) + p0.x * (p1.y - p2.y) + p1.x * p2.y;
if(a < 0 ?
(s <= 0 && s + t >= a) :
(s >= 0 && s + t <= a))
{
result[0] = i;
done = true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment