Refractoring
This commit is contained in:
@ -1,49 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using TransportGame.Model;
|
||||
|
||||
namespace TransportGame.Utils
|
||||
{
|
||||
public static class Algorithmss
|
||||
{
|
||||
public static bool DoPolygonsIntersect(Vector2[] a, Vector2[] b)
|
||||
{
|
||||
foreach (var poly in new[] {a, b})
|
||||
{
|
||||
for (int i = 0; i < poly.Length; i++)
|
||||
{
|
||||
int j = (i + 1) % poly.Length;
|
||||
|
||||
var normal = new Vector2(poly[j].Y - poly[i].Y, poly[i].X - poly[j].X);
|
||||
|
||||
double? minA = null, maxA = null;
|
||||
foreach (var p in a)
|
||||
{
|
||||
var projected = Vector2.Dot(normal, p);
|
||||
if (minA == null || projected < minA)
|
||||
minA = projected;
|
||||
if (maxA == null || projected > maxA)
|
||||
maxA = projected;
|
||||
}
|
||||
|
||||
double? minB = null, maxB = null;
|
||||
foreach (var p in b)
|
||||
{
|
||||
var projected = Vector2.Dot(normal, p);
|
||||
if (minB == null || projected < minB)
|
||||
minB = projected;
|
||||
if (maxB == null || projected > maxB)
|
||||
maxB = projected;
|
||||
}
|
||||
|
||||
if (maxA <= minB || maxB <= minA)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
38
Game/Assets/Scripts/Utils/MathHelper.cs
Normal file
38
Game/Assets/Scripts/Utils/MathHelper.cs
Normal file
@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace TransportGame.Utils
|
||||
{
|
||||
public static class MathHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// Clamps given value in the [0,1] interval
|
||||
/// </summary>
|
||||
/// <param name="value">Value</param>
|
||||
/// <returns>Clamped value</returns>
|
||||
public static float Clamp01(float value)
|
||||
{
|
||||
return Clamp(value, 0, 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clamps given value in the [min,max] interval
|
||||
/// </summary>
|
||||
/// <param name="value">Value</param>
|
||||
/// <param name="min">Minimum value</param>
|
||||
/// <param name="max">Maximum value</param>
|
||||
/// <returns>Clamped value</returns>
|
||||
public static float Clamp(float value, float min, float max)
|
||||
{
|
||||
if (value < min)
|
||||
return min;
|
||||
|
||||
if (value > max)
|
||||
return max;
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,8 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 67f24adfb8b21234184d0caf81f424ea
|
||||
guid: 29212ca52a4f0a64bbc8cffe74a3f4b7
|
||||
timeCreated: 1434538689
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@ -4,6 +4,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using TransportGame.Model;
|
||||
using TransportGame.Primitives;
|
||||
|
||||
namespace TransportGame.Utils
|
||||
{
|
||||
@ -62,12 +63,12 @@ namespace TransportGame.Utils
|
||||
private void Subdivide()
|
||||
{
|
||||
float midx = Boundary.Left + Boundary.Width / 2f;
|
||||
float midy = Boundary.Top + Boundary.Height / 2f;
|
||||
float midy = Boundary.Bottom + Boundary.Height / 2f;
|
||||
|
||||
NorthWest = new QuadTree<T>(Boundary.Left, Boundary.Top, midx, midy);
|
||||
NorthEast = new QuadTree<T>(midx, Boundary.Top, Boundary.Right, midy);
|
||||
SouthEast = new QuadTree<T>(midx, midy, Boundary.Right, Boundary.Bottom);
|
||||
SouthWest = new QuadTree<T>(Boundary.Left, midy, midx, Boundary.Bottom);
|
||||
NorthWest = new QuadTree<T>(Boundary.Left, Boundary.Bottom, midx, midy);
|
||||
NorthEast = new QuadTree<T>(midx, Boundary.Bottom, Boundary.Right, midy);
|
||||
SouthEast = new QuadTree<T>(midx, midy, Boundary.Right, Boundary.Top);
|
||||
SouthWest = new QuadTree<T>(Boundary.Left, midy, midx, Boundary.Top);
|
||||
|
||||
foreach (var point in points)
|
||||
Add(point);
|
||||
@ -258,7 +259,7 @@ namespace TransportGame.Utils
|
||||
public IEnumerable<T> Query(Rectangle rect)
|
||||
{
|
||||
// No intersection
|
||||
if (!Rectangle.Intersects(rect, Boundary))
|
||||
if (!Rectangle.Intersect(rect, Boundary))
|
||||
return Enumerable.Empty<T>();
|
||||
|
||||
if (NorthWest == null)
|
||||
|
Reference in New Issue
Block a user