/*public bool reach(Trajectory.Node origin, LineHandler origin_line, LineHandler destiny, Dictionary<Trajectory.Node, bool> visited_nodes, Dictionary<LineHandler, bool> visited_lines, List<Vector2> route){ * bool reached = true; * if(visited_nodes[origin] == false && !destiny.containsNode(origin)){ * visited_nodes [origin] = true; * foreach (LineHandler line in origin_line.getNeighborLines()) { * if(visited_lines[line] == false && line.containsNode(origin)){ * visited_lines[line.Key] = true; * if (!visited_nodes[line.Key.start] && reach (line.Key.start, destiny, visited_nodes, visited_lines, route)) * route.Add (new Vector2(line.Key.start.getX () / 10f, 60 - line.Key.start.getY () / 10f)); * else if (!visited_nodes[line.Key.end] && reach (line.Key.end, destiny, visited_nodes, visited_lines, route)) * route.Add (new Vector2(line.Key.end.getX () / 10f, 60 - line.Key.end.getY () / 10f)); * else * reached = false; * } * } * } * return reached; * }*/ /*public bool reach(Trajectory.Node origin, LineHandler origin_line, LineHandler destiny, Dictionary<Trajectory.Node, bool> visited_nodes, Dictionary<LineHandler, bool> visited_lines, List<Vector2> route){ * bool reached = true; * if (!destiny.containsNode (origin)) { * visited_nodes [origin] = true; * foreach (LineHandler line in origin_line.getNeighborLines()) { * if (!visited_lines [line] && !visited_nodes [line.getOtherPoint (origin)] && reach (line.getOtherPoint (origin), origin_line, destiny, visited_nodes, visited_lines, route)) { * Trajectory.Node n = line.getOtherPoint (origin); * route.Add (new Vector2(n.getX () / 10f, 60 - n.getY () / 10f)); * } * } * } * return reached; * }*/ public bool reach(LineHandler origin, LineHandler destiny, Dictionary <LineHandler, bool> visited, List <KeyValuePair <Vector2, float> > route) { bool ret = false; if (origin == destiny) { ret = true; } else if (visited[origin]) { ret = false; } else { visited[origin] = true; foreach (LineHandler neighbor in origin.getNeighborLines()) { if (reach(neighbor, destiny, visited, route)) { Trajectory.Node point = LineHandler.commonPoint(origin, neighbor); route.Add(new KeyValuePair <Vector2, float>(LineHandler.nodeToVector2(point), point.getScale())); ret = true; break; } } } return(ret); }
public KeyValuePair <Vector2, float>[] reach(LineHandler origin, LineHandler destiny) { List <KeyValuePair <Vector2, float> > route = new List <KeyValuePair <Vector2, float> >(); //Scene s = (Scene) sd; //WE STORE IN A BOOLEAN IF WE HAVE VISITED OR NOT THAT LINE // Dictionary<LineHandler, bool> visited_lines = new Dictionary<LineHandler, bool>(); // Dictionary<Trajectory.Node, bool> visited_nodes = new Dictionary<Trajectory.Node, bool>(); // // foreach (LineHandler line in lines) // visited_lines.Add (line, false); // // reach (origin, destiny, visited_lines, route); Dictionary <LineHandler, KeyValuePair <float, LineHandler> > stickered = stick(origin); LineHandler current = destiny; while (current != origin) { KeyValuePair <float, LineHandler> node = stickered[current]; Trajectory.Node tmp = LineHandler.commonPoint(current, node.Value); route.Add(new KeyValuePair <Vector2, float>(LineHandler.nodeToVector2(tmp), tmp.getScale())); current = node.Value; } route.Reverse(); return(route.ToArray()); }
public MovementPoint[] route(Vector2 origin, Vector2 destiny) { List <MovementPoint> ret = new List <MovementPoint>(); LineHandler origin_line = containingLine(origin), destiny_line = containingLine(destiny); Vector2 closest = Vector2.zero; if (origin_line == null) { closest = closestPoint(origin); origin_line = containingLine(closest); } if (destiny_line == null) { destiny = closestPoint(destiny); destiny_line = containingLine(destiny); } if (closest != Vector2.zero) { ret.Add(new MovementPoint() { destination = closest, scale = origin_line.getScaleFor(closest), distance = (closest - origin).magnitude }); } if (origin_line != null && destiny_line != null) { //###################################################### // IF ORIGIN_LINE AND DESTINY_LINE ARE THE SAME // Return only the destiny point, dont have to go // to other node //###################################################### if (origin_line == destiny_line) { ret.Add(new MovementPoint() { destination = destiny, scale = destiny_line.getScaleFor(destiny), distance = destiny_line.getSegmentDistance(origin, destiny), }); } else { /*List<KeyValuePair<Vector2, float>> tmpRoute = new List<KeyValuePair<Vector2, float>>(reach(origin_line, destiny_line)); * //tmpRoute.Reverse (); * ret.AddRange(tmpRoute); * ret.Add(new KeyValuePair<Vector2, float>(destiny, destiny_line.getScaleFor(destiny)));*/ ret = route_d(origin, destiny, origin_line, destiny_line); } } return(ret.ToArray()); }
public static Trajectory.Node commonPoint(LineHandler l1, LineHandler l2) { if (l1.containsNode(l2.start)) { return(l2.start); } else { return(l2.end); } }
public Dictionary <LineHandler, KeyValuePair <float, LineHandler> > stick(LineHandler origin) { Dictionary <LineHandler, KeyValuePair <float, LineHandler> > stickered = new Dictionary <LineHandler, KeyValuePair <float, LineHandler> >(); Dictionary <LineHandler, float> costs = new Dictionary <LineHandler, float>(); LineHandler current = origin, previous = origin; stickered.Add(origin, new KeyValuePair <float, LineHandler>(0, null)); float current_cost = 0;// TODO why? , total_cost = 0; while (stickered.Count < lines.Count) { current_cost = current.side.getLength(); //total_cost = stickered[current].Key; foreach (LineHandler line in current.getNeighborLines()) { if (!stickered.ContainsKey(line)) { if (costs.ContainsKey(line)) { if (costs[line] > current_cost) { costs[line] = current_cost; } } else { costs.Add(line, current_cost); } } } //obtenemos la mas corta float min = float.MaxValue; LineHandler selected = origin; foreach (KeyValuePair <LineHandler, float> line in costs) { if (line.Value < min) { selected = line.Key; min = line.Value; } } costs.Remove(selected); //establecemos la mas corta como principal y la aƱadimos a vecinas stickered.Add(selected, new KeyValuePair <float, LineHandler>(stickered[previous].Key + current.side.getLength(), current)); previous = current; current = selected; } return(stickered); }
public static void DivideSideByRect(Rectangle rect, Trajectory original, Trajectory.Side side, Trajectory outputTrajectory) { var startNode = original.getNodeForId(side.getIDStart()); var endNode = original.getNodeForId(side.getIDEnd()); Math3DFunc scaleFunc = new Math3DFunc( new Vector3(startNode.getX(), startNode.getY(), startNode.getScale()), new Vector3(endNode.getX(), endNode.getY(), endNode.getScale())); Math3DFunc lengthFunc = new Math3DFunc( new Vector3(startNode.getX(), startNode.getY(), 0), new Vector3(endNode.getX(), endNode.getY(), side.getLength())); var startInside = outputTrajectory.getNodeForId(side.getIDStart()) == null; var endInside = outputTrajectory.getNodeForId(side.getIDEnd()) == null; if (startInside && endInside) { return; } Vector2 start = LineHandler.nodeToVector2(startNode), end = LineHandler.nodeToVector2(endNode); Vector2[] intersections; if (LineRectangleIntersections(rect, start, end, out intersections)) { if (!startInside) { var cs = ClosestPoint(start, intersections); var csId = createRandomNodeId(cs.x, cs.y); outputTrajectory.addNode(csId, (int)cs.x, (int)cs.y, scaleFunc.getZ(cs.x, cs.y)); outputTrajectory.addSide(side.getIDStart(), csId, (int)lengthFunc.getZ(cs.x, cs.y)); } if (!endInside) { var ce = ClosestPoint(end, intersections); var ceId = createRandomNodeId(ce.x, ce.y); outputTrajectory.addNode(ceId, (int)ce.x, (int)ce.y, scaleFunc.getZ(ce.x, ce.y)); outputTrajectory.addSide(side.getIDEnd(), ceId, (int)(side.getLength() - lengthFunc.getZ(ce.x, ce.y))); } } else { outputTrajectory.addSide(side.getIDStart(), side.getIDEnd(), (int)side.getLength()); } }
public static bool TrajectoryRectangleIntersections(Rectangle rect, TrajectoryHandler trajectory, out Vector2[] intersections) { List <Vector2> intersectionsList = new List <Vector2>(); Vector2[] currentIntersections; foreach (var side in trajectory.lines) { if (LineRectangleIntersections(rect, LineHandler.nodeToVector2(side.start), LineHandler.nodeToVector2(side.end), out currentIntersections)) { intersectionsList.AddRange(currentIntersections); } } intersections = intersectionsList.ToArray(); return(intersections.Length > 0); }
public static Trajectory CreateBlockedTrajectory(Trajectory original, Rectangle blockingObject) { var trajectory = new Trajectory(); // Fist we add the nodes foreach (var node in original.getNodes()) { if (!Inside(LineHandler.nodeToVector2(node), blockingObject)) { trajectory.addNode(node.getID(), node.getX(), node.getY(), node.getScale()); } } // Then we add the sides foreach (var side in original.getSides()) { //Dividing them with the rect DivideSideByRect(blockingObject, original, side, trajectory); } return(trajectory); }
public bool isNeighbor(LineHandler line) { return(neighbours.Contains(line)); }
public void addNeighbour(LineHandler n) { this.neighbours.Add(n); }
public List <MovementPoint> route_d(Vector2 origin, Vector2 destiny, LineHandler originline, LineHandler destinyline) { Graph <string> g = new Graph <string>(); Dictionary <string, Dictionary <string, float> > d = new Dictionary <string, Dictionary <string, float> >(); Dictionary <string, Dictionary <string, MovementPoint> > ps = new Dictionary <string, Dictionary <string, MovementPoint> >(); foreach (Trajectory.Node n in this.trajectory.getNodes()) { d.Add(n.getID(), new Dictionary <string, float>()); ps.Add(n.getID(), new Dictionary <string, MovementPoint>()); } foreach (Trajectory.Side s in this.trajectory.getSides()) { var start = trajectory.getNodeForId(s.getIDStart()); var end = trajectory.getNodeForId(s.getIDEnd()); d[s.getIDStart()].Add(s.getIDEnd(), s.getLength()); d[s.getIDEnd()].Add(s.getIDStart(), s.getLength()); ps[s.getIDStart()].Add(s.getIDEnd(), new MovementPoint() { distance = s.getLength(), scale = end.getScale(), destination = new Vector2(end.getX(), end.getY()) }); ps[s.getIDEnd()].Add(s.getIDStart(), new MovementPoint() { distance = s.getLength(), scale = start.getScale(), destination = new Vector2(start.getX(), start.getY()) }); } Trajectory.Node no = new Trajectory.Node("origin", Mathf.RoundToInt(origin.x), Mathf.RoundToInt(origin.y), 1f); Trajectory.Node nd = new Trajectory.Node("destiny", Mathf.RoundToInt(destiny.x), Mathf.RoundToInt(destiny.y), 1f); Vector2 oStartV2 = LineHandler.nodeToVector2(originline.start), oEndV2 = LineHandler.nodeToVector2(originline.end), dStartV2 = LineHandler.nodeToVector2(destinyline.start), dEndV2 = LineHandler.nodeToVector2(destinyline.end); float od1 = originline.getSegmentDistance(origin, oStartV2), od2 = originline.getSegmentDistance(origin, oEndV2); float dd1 = destinyline.getSegmentDistance(destiny, dStartV2), dd2 = destinyline.getSegmentDistance(destiny, dEndV2); d[originline.start.getID()].Add(no.getID(), od1); d[originline.end.getID()].Add(no.getID(), od2); d.Add("origin", new Dictionary <string, float>() { { originline.start.getID(), od1 }, { originline.end.getID(), od2 } }); ps[originline.start.getID()].Add(no.getID(), new MovementPoint() { distance = od1, scale = originline.getScaleFor(origin), destination = origin }); ps[originline.end.getID()].Add(no.getID(), new MovementPoint() { distance = od2, scale = originline.getScaleFor(origin), destination = origin }); ps.Add("origin", new Dictionary <string, MovementPoint>() { { originline.start.getID(), new MovementPoint() { distance = od1, scale = originline.getScaleFor(LineHandler.nodeToVector2(originline.start)), destination = LineHandler.nodeToVector2(originline.start) } }, { originline.end.getID(), new MovementPoint() { distance = od2, scale = originline.getScaleFor(LineHandler.nodeToVector2(originline.end)), destination = LineHandler.nodeToVector2(originline.end) } } }); d[destinyline.start.getID()].Add(nd.getID(), dd1); d[destinyline.end.getID()].Add(nd.getID(), dd2); d.Add("destiny", new Dictionary <string, float>() { { destinyline.start.getID(), dd1 }, { destinyline.end.getID(), dd2 } }); ps[destinyline.start.getID()].Add(nd.getID(), new MovementPoint() { distance = dd1, scale = destinyline.getScaleFor(destiny), destination = destiny }); ps[destinyline.end.getID()].Add(nd.getID(), new MovementPoint() { distance = dd2, scale = destinyline.getScaleFor(destiny), destination = destiny }); ps.Add("destiny", new Dictionary <string, MovementPoint>() { { destinyline.start.getID(), new MovementPoint() { distance = dd1, scale = destinyline.getScaleFor(LineHandler.nodeToVector2(destinyline.start)), destination = LineHandler.nodeToVector2(destinyline.start) } }, { destinyline.end.getID(), new MovementPoint() { distance = dd2, scale = destinyline.getScaleFor(LineHandler.nodeToVector2(destinyline.end)), destination = LineHandler.nodeToVector2(destinyline.end) } } }); g.set_vertices(d); List <string> l = g.shortest_path("origin", "destiny"); l.Reverse(); List <MovementPoint> ret = new List <MovementPoint>(); string last = "origin"; foreach (string n in l) { ret.Add(ps[last][n]); last = n; } return(ret); }
public KeyValuePair <Vector2, float>[] route(Vector2 origin, Vector2 destiny) { List <KeyValuePair <Vector2, float> > ret = new List <KeyValuePair <Vector2, float> >(); LineHandler origin_line = null, destiny_line = null; foreach (LineHandler handler in lines) { if (origin_line == null && handler.contains(origin)) { origin_line = handler; } if (destiny_line == null && handler.contains(destiny)) { destiny_line = handler; } if (origin_line != null && destiny_line != null) { break; } } Vector2 closest = Vector2.zero; if (origin_line == null) { closest = closestPoint(PlayerMB.Instance.getPosition()); foreach (LineHandler handler in lines) { if (origin_line == null && handler.contains(closest)) { origin_line = handler; break; } } } if (closest != Vector2.zero) { ret.Add(new KeyValuePair <Vector2, float>(closest, origin_line.getScaleFor(closest))); } if (origin_line != null && destiny_line != null) { //###################################################### // IF ORIGIN_LINE AND DESTINY_LINE ARE THE SAME // Return only the destiny point, dont have to go // to other node //###################################################### if (origin_line == destiny_line) { ret.Add(new KeyValuePair <Vector2, float>(destiny, destiny_line.getScaleFor(destiny))); } else { List <KeyValuePair <Vector2, float> > tmpRoute = new List <KeyValuePair <Vector2, float> >(reach(origin_line, destiny_line)); //tmpRoute.Reverse (); ret.AddRange(tmpRoute); ret.Add(new KeyValuePair <Vector2, float>(destiny, destiny_line.getScaleFor(destiny))); } } return(ret.ToArray()); }