city-generation/Game/Assets/Scripts/Model/BuildingLot.cs

68 lines
1.9 KiB
C#
Raw Normal View History

2015-06-03 20:54:22 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TransportGame.Business;
namespace TransportGame.Model
{
public class BuildingLot : IPositionable
{
/// <summary>
/// Gets or sets the list of points
/// </summary>
public Vector2[] Points { get; set; }
public BuildingLot()
{
Points = new Vector2[4];
}
public BuildingLot(params Vector2[] points)
{
Points = points;
}
public BuildingLot(IEnumerable<Vector2> points)
{
Points = points.ToArray();
}
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 Intersect(BuildingLot a, BuildingLot b)
{
return (a.Position - b.Position).LengthSq <= ConfigManager.Buildgen.LotSquareSize + ConfigManager.Buildgen.LotSpacing;
}
/// <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 Intersect(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;
}
}
}