102 lines
2.8 KiB
C#
102 lines
2.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using TransportGame.Business;
|
|
using TransportGame.Utils;
|
|
|
|
namespace TransportGame.Model
|
|
{
|
|
public class BuildingLot : IPositionable
|
|
{
|
|
/// <summary>
|
|
/// Gets or sets the list of points
|
|
/// </summary>
|
|
public Vector2[] Points { get; set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the size of the lot
|
|
/// </summary>
|
|
public float Size { get; set; }
|
|
|
|
/// <summary>
|
|
/// Initializes the building lot
|
|
/// </summary>
|
|
public BuildingLot()
|
|
{
|
|
Points = new Vector2[4];
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes lot with given size
|
|
/// </summary>
|
|
/// <param name="size">size</param>
|
|
public BuildingLot(float size)
|
|
{
|
|
Points = new Vector2[4];
|
|
Size = size;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes lot with given points and size
|
|
/// </summary>
|
|
/// <param name="size">Size</param>
|
|
/// <param name="points">Points</param>
|
|
public BuildingLot(float size, params Vector2[] points)
|
|
{
|
|
Points = points;
|
|
Size = size;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes lot with given points and size
|
|
/// </summary>
|
|
/// <param name="size">Size</param>
|
|
/// <param name="points">Points</param>
|
|
public BuildingLot(float size, IEnumerable<Vector2> points)
|
|
{
|
|
Points = points.ToArray();
|
|
Size = size;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the lot position
|
|
/// </summary>
|
|
public Vector2 Position
|
|
{
|
|
get { return Points.Aggregate((x, y) => x + y) / Points.Length; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tests if two building lots intersect
|
|
/// </summary>
|
|
/// <param name="a">First lot</param>
|
|
/// <param name="b">Second lot</param>
|
|
/// <returns></returns>
|
|
public static bool DoesIntersect(BuildingLot a, BuildingLot b)
|
|
{
|
|
return Algorithmss.DoPolygonsIntersect(a.Points, b.Points);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Tests if a building lot intersects a line segment (such as a road segment)
|
|
/// </summary>
|
|
/// <param name="a"></param>
|
|
/// <param name="b"></param>
|
|
/// <returns></returns>
|
|
public static bool DoesIntersect(BuildingLot a, LineSegment b)
|
|
{
|
|
for (int i = 0; i < a.Points.Length; i++)
|
|
{
|
|
int j = (i + 1 >= a.Points.Length) ? 0 : i + 1;
|
|
|
|
LineSegment s = new LineSegment(a.Points[i], a.Points[j]);
|
|
if (LineSegment.Intersect(s, b).HasValue)
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
}
|