/*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 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 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); }