2008-01-08 18:52:20 +00:00
|
|
|
|
|
|
|
#region ================== Copyright (c) 2007 Pascal vd Heiden
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2007 Pascal vd Heiden, www.codeimp.com
|
|
|
|
* This program is released under GNU General Public License
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Namespaces
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Globalization;
|
|
|
|
using System.Text;
|
|
|
|
using CodeImp.DoomBuilder.Map;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace CodeImp.DoomBuilder.Geometry
|
|
|
|
{
|
2009-02-09 15:58:18 +00:00
|
|
|
public sealed class SidedefAngleSorter : IComparer<Sidedef>
|
2008-01-08 18:52:20 +00:00
|
|
|
{
|
|
|
|
// Variables
|
2008-01-18 19:52:19 +00:00
|
|
|
private Sidedef baseside;
|
2008-01-08 18:52:20 +00:00
|
|
|
private Vertex basevertex;
|
|
|
|
|
|
|
|
// Constructor
|
2008-01-18 19:52:19 +00:00
|
|
|
public SidedefAngleSorter(Sidedef baseside, Vertex fromvertex)
|
2008-01-08 18:52:20 +00:00
|
|
|
{
|
|
|
|
// Initialize
|
2008-01-18 19:52:19 +00:00
|
|
|
this.baseside = baseside;
|
|
|
|
this.basevertex = fromvertex;
|
2008-01-08 18:52:20 +00:00
|
|
|
|
|
|
|
// We have no destructor
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
}
|
|
|
|
|
2008-01-18 19:52:19 +00:00
|
|
|
// This calculates the relative angle between two sides
|
|
|
|
private float CalculateRelativeAngle(Sidedef a, Sidedef b)
|
|
|
|
{
|
|
|
|
float s, n, ana, anb;
|
|
|
|
Vector2D va, vb;
|
|
|
|
bool dir;
|
|
|
|
|
|
|
|
// Determine angles
|
|
|
|
ana = a.Line.Angle; if(a.Line.End == basevertex) ana += Angle2D.PI;
|
|
|
|
anb = b.Line.Angle; if(b.Line.End == basevertex) anb += Angle2D.PI;
|
|
|
|
|
|
|
|
// Take the difference from angles
|
|
|
|
n = Angle2D.Difference(ana, anb);
|
|
|
|
|
|
|
|
// Get line end vertices a and b that are not connected to basevertex
|
|
|
|
if(a.Line.Start == basevertex) va = a.Line.End.Position; else va = a.Line.Start.Position;
|
|
|
|
if(b.Line.Start == basevertex) vb = b.Line.End.Position; else vb = b.Line.Start.Position;
|
|
|
|
|
|
|
|
// Determine rotation direction
|
|
|
|
dir = baseside.IsFront;
|
|
|
|
if(baseside.Line.End == basevertex) dir = !dir;
|
|
|
|
|
|
|
|
// Check to which side the angle goes and adjust angle as needed
|
|
|
|
s = Line2D.GetSideOfLine(va, vb, basevertex.Position);
|
|
|
|
if((s < 0) && dir) n = Angle2D.PI2 - n;
|
|
|
|
if((s > 0) && !dir) n = Angle2D.PI2 - n;
|
|
|
|
|
|
|
|
// Return result
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2008-01-08 18:52:20 +00:00
|
|
|
// Comparer
|
|
|
|
public int Compare(Sidedef x, Sidedef y)
|
|
|
|
{
|
|
|
|
float ax, ay;
|
|
|
|
|
2008-01-18 19:52:19 +00:00
|
|
|
// Calculate angles
|
|
|
|
ax = CalculateRelativeAngle(baseside, x);
|
|
|
|
ay = CalculateRelativeAngle(baseside, y);
|
2008-01-08 18:52:20 +00:00
|
|
|
|
|
|
|
// Compare results
|
2008-10-07 05:50:39 +00:00
|
|
|
/*
|
2008-01-08 18:52:20 +00:00
|
|
|
if(ax < ay) return 1;
|
|
|
|
else if(ax > ay) return -1;
|
|
|
|
else return 0;
|
2008-10-07 05:50:39 +00:00
|
|
|
*/
|
2008-10-08 20:09:44 +00:00
|
|
|
return Math.Sign(ay - ax);
|
2008-01-08 18:52:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|