Refactoring, optimizations

This commit is contained in:
2015-06-13 21:36:32 +03:00
parent 9ac0deec0b
commit 778d732866
37 changed files with 169 additions and 66 deletions

View File

@ -97,5 +97,29 @@ namespace TransportGame.Model
return false;
}
/// <summary>
/// Tests if a point is inside the building lot
/// </summary>
/// <param name="point"></param>
/// <returns></returns>
public bool IsInside(Vector2 point)
{
return IsInsideTriangle(point, Points[0], Points[1], Points[2]) || IsInsideTriangle(point, Points[0], Points[2], Points[3]);
}
private static float Sign(Vector2 a, Vector2 b, Vector2 c)
{
return (a.X - c.X) * (b.Y - c.Y) - (b.X - c.X) * (a.Y - c.Y);
}
private static bool IsInsideTriangle(Vector2 pt, Vector2 a, Vector2 b, Vector2 c)
{
bool b1 = Sign(pt, a, b) < 0;
bool b2 = Sign(pt, b, c) < 0;
bool b3 = Sign(pt, c, a) < 0;
return (b1 == b2) && (b2 == b3);
}
}
}

View File

@ -37,7 +37,7 @@ namespace TransportGame.Model.Config
/// </summary>
public float MaxBuildingHeight { get; set; }
public float MinBuildingHeight { get; set; }
/// <summary>
/// Maximum number of primitive polygons to be generated per level
/// </summary>
@ -49,8 +49,8 @@ namespace TransportGame.Model.Config
LotSquareMaxSize = 20f;
LotSpacing = 0.1f;
MaxLotAttempts = 3;
MaxBuildingHeight = 20f;
MinBuildingHeight = 4f;
MaxBuildingHeight = 25f;
MinBuildingHeight = 5f;
MaxBuildingLevels = 7;
MaxPolygonsPerLevel = 4;
}

View File

@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace TransportGame.Model
{
public class Range
{
[XmlAttribute("min")]
public float Minimum { get; set; }
[XmlAttribute("max")]
public float Maximum { get; set; }
public Range()
{
Minimum = 0;
Maximum = 1;
}
public Range(float min, float max)
{
Minimum = min;
Maximum = max;
}
public override string ToString()
{
return String.Format("[{0}, {1}]", Minimum, Maximum);
}
}
}

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: b436281f9c7d49f4d935ae009d4559a8
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

View File

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using UnityEngine;
namespace TransportGame.Model
{
@ -277,5 +276,69 @@ namespace TransportGame.Model
{
return X.GetHashCode() * 7 + Y.GetHashCode();
}
private class LengthComparerImpl : IComparer<Vector2>
{
public int Compare(Vector2 a, Vector2 b)
{
if (a.LengthSq > b.LengthSq)
return 1;
if (a.LengthSq < b.LengthSq)
return -1;
return 0;
}
}
private class TrigonometricComparerImpl : IComparer<Vector2>
{
private int Quad(Vector2 v)
{
if (v.Y >= 0)
{
if (v.X >= 0)
return 0;
return 1;
}
else
{
if (v.X < 0)
return 2;
return 3;
}
}
public int Compare(Vector2 a, Vector2 b)
{
// If vectors are in different quadrants, we can use quadrant number
int qa = Quad(a), qb = Quad(b);
if (qa != qb)
{
return qa - qb;
}
// In same quadrant. Compute cross product which gives us sin(ab)*len(a)*len(b)
// Vectors are in same quadrant, so angle should be less than 90deg
float cross = Cross(a, b);
if (cross < 0) // Angle > 180 degrees => a > b
return 1;
if (cross > 0) // Angle < 180 degrees => a < b
return -1;
// Points are on the same line. Use distance
if (a.LengthSq > b.LengthSq)
return 1;
else if (a.LengthSq < b.LengthSq)
return -1;
// Points are equal
return 0;
}
}
public static IComparer<Vector2> LengthComparer = new LengthComparerImpl();
public static IComparer<Vector2> TrigonomicComparer = new TrigonometricComparerImpl();
}
}