Exemple #1
0
    private MapTriangle FindTriangle(MapPlace toLocate, List <MapPlace> locatedPlaces, List <ShipmentEdge> allEdges)
    {
        MapTriangle mapTriangle = new MapTriangle();

        List <ShipmentEdge> edgesByIdNode = ShipmentsView.instance.Model.GetEdgesByIdNode(toLocate.Id);

        List <MapPlace> vertices = locatedPlaces.FindAll(e => edgesByIdNode.Exists(f => f.IdNodeA == e.Id || f.IdNodeB == e.Id));

        if (vertices.Count < 2)
        {
            return(null);
        }

/*
 *      if (vertices.Count > 2) throw new Exception("mmmh many vertexes");
 */

        ShipmentEdge edge = allEdges.Find(
            e =>
            (e.IdNodeA == vertices[0].Id && e.IdNodeB == vertices[1].Id) ||
            (e.IdNodeB == vertices[0].Id && e.IdNodeA == vertices[1].Id));

        mapTriangle.Nodes = new List <int>(2);
        mapTriangle.Edges = new List <ShipmentEdge>(3);
        mapTriangle.Nodes.AddRange(new [] { toLocate.Id, vertices[0].Id, vertices[1].Id });
        mapTriangle.Edges.AddRange(new []
        {
            edge, edgesByIdNode.Find(e => e.IdNodeA == vertices[0].Id || e.IdNodeB == vertices[0].Id),
            edgesByIdNode.Find(e => e.IdNodeA == vertices[1].Id || e.IdNodeB == vertices[1].Id)
        });
        return(mapTriangle);
    }
Exemple #2
0
    private void LocatePlaceWithOneRestriction(MapPlace toLocate, List <MapPlace> prevPlaces, List <MapPlace> locatedPlaces, float xMax, float yMax, float distanceMin)
    {
        MapPlace     located       = prevPlaces[0];
        Vector2      otherPosition = located.transform.position;
        ShipmentEdge edge          = ShipmentsView.instance.Model.GetEdgesByIdNode(toLocate.Id).Find(e =>
                                                                                                     e.IdNodeA == located.Id || e.IdNodeB == located.Id);
        float f;

        do
        {
            toLocate.transform.localPosition =
                new Vector2((Randomizer.RandomBoolean() ? 1 : -1) * Random.Range(0.1f * xMax, 0.9f * xMax),
                            (Randomizer.RandomBoolean() ? 1 : -1) * Random.Range(0.1f * yMax, 0.9f * yMax));
            float a        = Ruler.GetUnityDistances();
            float distance = edge.Length == 0 ? Random.Range(3, 9) : edge.Length;

            float x;
            float y;
            if (Randomizer.RandomBoolean())
            {
                // x random
                x = toLocate.transform.position.x;
                float f1 = distance * a;
                float f2 = x - otherPosition.x;
                while (Mathf.Abs(f1) < Mathf.Abs(f2))
                {
                    if (edge.Length != 0)
                    {
/*
 *                      throw new Exception("Edge have to b resized but the node is located");
 */
                    }
                    if (distance < 10)
                    {
                        distance++;
                    }
                    else
                    {
                        toLocate.transform.localPosition =
                            new Vector2((Randomizer.RandomBoolean() ? 1 : -1) * Random.Range(0, x), 0);

                        x  = toLocate.transform.position.x;
                        f2 = x - otherPosition.x;
                    }
                    f1 = distance * a;
                }
                float sqrt = Mathf.Sqrt(Mathf.Pow(f1, 2) - Mathf.Pow(f2, 2));
                y = -sqrt + otherPosition.y;
            }
            else
            {
                // y random
                y = toLocate.transform.position.y;

                float f1 = distance * a;
                float f2 = y - otherPosition.y;
                while (Mathf.Abs(f1) < Mathf.Abs(f2))
                {
                    if (edge.Length != 0)
                    {
/*
 *                      throw new Exception("Edge have to b resized but the node is located");
 */
                    }

                    if (distance < 10)
                    {
                        distance++;
                    }
                    else
                    {
                        toLocate.transform.localPosition = new Vector2(0,
                                                                       (Randomizer.RandomBoolean() ? 1 : -1) * Random.Range(0, yMax));

                        y  = toLocate.transform.position.y;
                        f2 = y - otherPosition.y;
                    }
                    f1 = distance * a;
                }
                float sqrt = Mathf.Sqrt(Mathf.Pow(f1, 2) - Mathf.Pow(f2, 2));
                x = -sqrt + otherPosition.x;
            }


            toLocate.transform.position = new Vector2(x, y);
            var referenceDistance = Vector2.Distance(toLocate.transform.position, otherPosition);

            f = referenceDistance / a;
        } while (EdgeIsIncorrect(toLocate, locatedPlaces, xMax, yMax, distanceMin, f));

        edge.Length = (int)f;
    }