2008-01-06 20:56:48 +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.Geometry;
|
|
|
|
using CodeImp.DoomBuilder.Rendering;
|
2008-05-11 00:42:34 +00:00
|
|
|
using SlimDX.Direct3D9;
|
2008-01-06 20:56:48 +00:00
|
|
|
using System.Drawing;
|
|
|
|
using CodeImp.DoomBuilder.Map;
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace CodeImp.DoomBuilder.Geometry
|
|
|
|
{
|
2008-09-07 21:28:20 +00:00
|
|
|
public class Polygon : List<Vector2D>
|
2008-01-06 20:56:48 +00:00
|
|
|
{
|
|
|
|
#region ================== Variables
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Properties
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#region ================== Constructors
|
|
|
|
|
|
|
|
// Constructor
|
2008-09-07 21:28:20 +00:00
|
|
|
public Polygon()
|
2008-01-06 20:56:48 +00:00
|
|
|
{
|
|
|
|
// Initialize
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
2008-09-07 21:28:20 +00:00
|
|
|
|
2008-01-06 20:56:48 +00:00
|
|
|
#region ================== Methods
|
|
|
|
|
|
|
|
// Point inside the polygon?
|
|
|
|
// See: http://local.wasp.uwa.edu.au/~pbourke/geometry/insidepoly/
|
|
|
|
public bool Intersect(Vector2D p)
|
|
|
|
{
|
|
|
|
float miny, maxy, maxx, xint;
|
2008-09-07 21:28:20 +00:00
|
|
|
Vector2D v1 = base[base.Count - 1];
|
2008-01-06 20:56:48 +00:00
|
|
|
Vector2D v2;
|
2008-09-07 21:28:20 +00:00
|
|
|
int index = 0;
|
2008-05-07 22:46:15 +00:00
|
|
|
uint c = 0;
|
2008-09-07 21:28:20 +00:00
|
|
|
|
2008-01-06 20:56:48 +00:00
|
|
|
// Go for all vertices
|
2008-09-07 21:28:20 +00:00
|
|
|
while(index < base.Count)
|
2008-01-06 20:56:48 +00:00
|
|
|
{
|
|
|
|
// Get next vertex
|
2008-09-07 21:28:20 +00:00
|
|
|
v2 = base[index];
|
2008-01-06 20:56:48 +00:00
|
|
|
|
|
|
|
// Determine min/max values
|
|
|
|
miny = Math.Min(v1.y, v2.y);
|
|
|
|
maxy = Math.Max(v1.y, v2.y);
|
|
|
|
maxx = Math.Max(v1.x, v2.x);
|
|
|
|
|
|
|
|
// Check for intersection
|
|
|
|
if((p.y > miny) && (p.y <= maxy))
|
|
|
|
{
|
|
|
|
if(p.x <= maxx)
|
|
|
|
{
|
|
|
|
if(v1.y != v2.y)
|
|
|
|
{
|
|
|
|
xint = (p.y - v1.y) * (v2.x - v1.x) / (v2.y - v1.y) + v1.x;
|
|
|
|
if((v1.x == v2.x) || (p.x <= xint)) c++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-09-07 21:28:20 +00:00
|
|
|
|
2008-01-06 20:56:48 +00:00
|
|
|
// Move to next
|
|
|
|
v1 = v2;
|
2008-09-07 21:28:20 +00:00
|
|
|
index++;
|
2008-01-06 20:56:48 +00:00
|
|
|
}
|
2008-09-07 21:28:20 +00:00
|
|
|
|
2008-05-07 22:46:15 +00:00
|
|
|
// Inside this polygon?
|
2008-09-07 21:28:20 +00:00
|
|
|
return (c & 0x00000001UL) != 0;
|
2008-01-06 20:56:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|