mirror of
https://git.do.srb2.org/STJr/UltimateZoneBuilder.git
synced 2024-11-22 20:02:48 +00:00
Replaced line intersection with something that does not cause infinite loops
This commit is contained in:
parent
554ccc442f
commit
d843a61d61
1 changed files with 73 additions and 60 deletions
|
@ -226,70 +226,83 @@ namespace CodeImp.DoomBuilder.Geometry
|
||||||
return new Vector2D(v1.x + u * (v2.x - v1.x), v1.y + u * (v2.y - v1.y));
|
return new Vector2D(v1.x + u * (v2.x - v1.x), v1.y + u * (v2.y - v1.y));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cohen-Sutherland algorithm
|
private static bool IsEqualFloat(float a, float b)
|
||||||
public static Line2D ClipToRectangle(Line2D line, RectangleF rect, out bool intersects)
|
{
|
||||||
|
return Math.Abs(a - b) < 0.0001f;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Some random self-written aglrithm instead of Cohen-Sutherland algorithm which used to hang up randomly
|
||||||
|
public static Line2D ClipToRectangle(Line2D line, RectangleF rect, out bool intersects)
|
||||||
{
|
{
|
||||||
int flags1 = MapSet.GetCSFieldBits(line.v1, rect);
|
float rateXY = 0f;
|
||||||
int flags2 = MapSet.GetCSFieldBits(line.v2, rect);
|
if (line.v2.y != line.v1.y)
|
||||||
intersects = false;
|
{
|
||||||
|
float dx = line.v2.x - line.v1.x;
|
||||||
|
float dy = line.v2.y - line.v1.y;
|
||||||
|
rateXY = dx / dy;
|
||||||
|
}
|
||||||
|
|
||||||
Line2D result = line;
|
float x1 = line.v1.x, y1 = line.v1.y;
|
||||||
while (true)
|
float x2 = line.v2.x, y2 = line.v2.y;
|
||||||
{
|
|
||||||
if (flags1 == 0 && flags2 == 0)
|
|
||||||
{
|
|
||||||
// Line is fully inside the box
|
|
||||||
intersects = true;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((flags1 & flags2) != 0)
|
|
||||||
{
|
|
||||||
// Both points are in the same outer area
|
|
||||||
intersects = false;
|
|
||||||
return new Line2D();
|
|
||||||
}
|
|
||||||
|
|
||||||
float x, y;
|
for (int i = 0; i < 2; i++)
|
||||||
int outFlags = flags1 != 0 ? flags1 : flags2;
|
{
|
||||||
if ((outFlags & 0x1) > 0) // Top
|
// check x1,y1
|
||||||
{
|
if (y1 < rect.Top)
|
||||||
x = result.v1.x + (result.v2.x - result.v1.x) * (rect.Top - result.v1.y) / (result.v2.y - result.v1.y);
|
{
|
||||||
y = rect.Top;
|
x1 += (rect.Top - y1) * rateXY;
|
||||||
}
|
y1 = rect.Top;
|
||||||
else if ((outFlags & 0x2) > 0) // Bottom
|
}
|
||||||
{
|
if (x1 < rect.Left)
|
||||||
x = result.v1.x + (result.v2.x - result.v1.x) * (rect.Bottom - result.v1.y) / (result.v2.y - result.v1.y);
|
{
|
||||||
y = rect.Bottom;
|
if (rateXY != 0) y1 += (rect.Left - x1) / rateXY;
|
||||||
}
|
x1 = rect.Left;
|
||||||
else if ((outFlags & 0x4) > 0) // Left
|
}
|
||||||
{
|
// check x2,y2
|
||||||
y = result.v1.y + (result.v2.y - result.v1.y) * (rect.Left - result.v1.x) / (result.v2.x - result.v1.x);
|
if (y2 < rect.Top)
|
||||||
x = rect.Left;
|
{
|
||||||
}
|
x2 += (rect.Top - y2) * rateXY;
|
||||||
else if ((outFlags & 0x8) > 0) // Right
|
y2 = rect.Top;
|
||||||
{
|
}
|
||||||
y = result.v1.y + (result.v2.y - result.v1.y) * (rect.Right - result.v1.x) / (result.v2.x - result.v1.x);
|
if (x2 < rect.Left)
|
||||||
x = rect.Right;
|
{
|
||||||
}
|
if (rateXY != 0) y2 += (rect.Left - x2) / rateXY;
|
||||||
else
|
x2 = rect.Left;
|
||||||
{
|
}
|
||||||
intersects = true;
|
// check x1,y1
|
||||||
return result;
|
if (y1 > rect.Bottom)
|
||||||
}
|
{
|
||||||
|
x1 -= (y1 - rect.Bottom) * rateXY;
|
||||||
|
y1 = rect.Bottom;
|
||||||
|
}
|
||||||
|
if (x1 > rect.Right)
|
||||||
|
{
|
||||||
|
if (rateXY != 0) y1 -= (x1 - rect.Right) / rateXY;
|
||||||
|
x1 = rect.Right;
|
||||||
|
}
|
||||||
|
// check x2,y2
|
||||||
|
if (y2 > rect.Bottom)
|
||||||
|
{
|
||||||
|
x2 -= (y2 - rect.Bottom) * rateXY;
|
||||||
|
y2 = rect.Bottom;
|
||||||
|
}
|
||||||
|
if (x2 > rect.Right)
|
||||||
|
{
|
||||||
|
if (rateXY != 0) y2 -= (x2 - rect.Right) / rateXY;
|
||||||
|
x2 = rect.Right;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (outFlags == flags1)
|
if ((IsEqualFloat(x1, x2) && (IsEqualFloat(x1, rect.Left) || IsEqualFloat(x1, rect.Right)) ||
|
||||||
{
|
(IsEqualFloat(y1, y2) && (IsEqualFloat(y1, rect.Bottom) || IsEqualFloat(y1, rect.Top)))))
|
||||||
result.v1 = new Vector2D(x, y);
|
{
|
||||||
flags1 = MapSet.GetCSFieldBits(result.v1, rect);
|
intersects = false;
|
||||||
}
|
return new Line2D();
|
||||||
else
|
}
|
||||||
{
|
|
||||||
result.v2 = new Vector2D(x, y);
|
intersects = true;
|
||||||
flags2 = MapSet.GetCSFieldBits(result.v2, rect);
|
return new Line2D(x1, y1, x2, y2);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue