-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathRectUtils.cs
More file actions
92 lines (77 loc) · 2.18 KB
/
RectUtils.cs
File metadata and controls
92 lines (77 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using UnityEngine;
namespace UnityEditor.Experimental.GraphView
{
public class RectUtils
{
public static bool IntersectsSegment(Rect rect, Vector2 p1, Vector2 p2)
{
float minX = Mathf.Min(p1.x, p2.x);
float maxX = Mathf.Max(p1.x, p2.x);
if (maxX > rect.xMax)
{
maxX = rect.xMax;
}
if (minX < rect.xMin)
{
minX = rect.xMin;
}
if (minX > maxX)
{
return false;
}
float minY = Mathf.Min(p1.y, p2.y);
float maxY = Mathf.Max(p1.y, p2.y);
float dx = p2.x - p1.x;
if (Mathf.Abs(dx) > float.Epsilon)
{
float a = (p2.y - p1.y) / dx;
float b = p1.y - a * p1.x;
minY = a * minX + b;
maxY = a * maxX + b;
}
if (minY > maxY)
{
float tmp = maxY;
maxY = minY;
minY = tmp;
}
if (maxY > rect.yMax)
{
maxY = rect.yMax;
}
if (minY < rect.yMin)
{
minY = rect.yMin;
}
if (minY > maxY)
{
return false;
}
return true;
}
public static Rect Encompass(Rect a, Rect b)
{
return new Rect
{
xMin = Math.Min(a.xMin, b.xMin),
yMin = Math.Min(a.yMin, b.yMin),
xMax = Math.Max(a.xMax, b.xMax),
yMax = Math.Max(a.yMax, b.yMax)
};
}
public static Rect Inflate(Rect a, float left, float top, float right, float bottom)
{
return new Rect
{
xMin = a.xMin - left,
yMin = a.yMin - top,
xMax = a.xMax + right,
yMax = a.yMax + bottom
};
}
}
}