/** * Starts traversing with given distance and method from the starting point of the path * * @param distance Distance along the path from the start point. * @param method Method of traversal. Use method constants. * @return returns TraversePointInfo for the landing point on the Path */ public TraversePointInfo startTraverse(float distance, int method) { try { float totalDistance = 0; totalDistance = 0; if (nodes == null) return null; if (nodes.size() == 0) return null; PathNode t = this.nodes.get(0); Point ret = t.getLocation(); while (true) { if (t.getNextNode() == null) { TraversePointInfo info = new TraversePointInfo(); info.initialize( t.getLocation(), t, this.totalDistance, this.totalDistance, this, this.totalDistance); return info; } if ((totalDistance + t.getDistance(method)) > distance) { ret = t.getPointAtDistance(distance - totalDistance, method); break; } else totalDistance += t.getDistance(method); t = t.getNextNode(); } TraversePointInfo info = new TraversePointInfo(); info.initialize(ret, t, distance - totalDistance, distance, this, distance); return info; } catch (Exception e) { e.printStackTrace(); return null; } }
public TraversePointInfo startTraverse(Point hitPoint, float maxDistance) { float distance = 9999, smallestDistance = 9999; nodes = renderNodes; PathNode t = null; for (int i = 0; i < nodes.size(); i++) { distance = nodes.get(i).getLocation().distancePoint2Point(hitPoint); // Helper.printKeyVal("PathNode: ", nodes.get(i).getLocation().toString()); // Helper.printKeyVal("Distance", distance); if (distance < smallestDistance) { // Helper.printKeyVal("New smallest point: ", nodes.get(i).getLocation().toString()); // Helper.printKeyVal("New smallest Distance", distance); smallestDistance = distance; t = nodes.get(i); } } if (smallestDistance > maxDistance) return null; TraversePointInfo info = new TraversePointInfo(); info.setPathNode(t); info.setTraverseLocation(t.getLocation()); info.setDistance(0); distance = 0; for (int i = 0; i < nodes.size(); i++) { if (nodes.get(i).equals(t)) break; distance += nodes.get(i).getRightDistance(); } info.setTotalDistanceInPath(distance); // Helper.printKeyVal("Total Distance Set: ", info.getTotalDistanceInPath()); info.setPath(this); return info; }
/** * This will traverse from current point to another given "distance" amount using the given * method. * * @param info Current traversal info. * @param distanceToTraverse How much to traverse on the path from current point * @param method Method of traversal. Use method constants. * @param affectRender Shall traversing this mark the points as traversed? If yes then all the * pathnodes being traversed would not be drawn next time. If no, then everying stays the same * for rendering. */ public void traverse( TraversePointInfo info, float distanceFromCurPoint, int method, boolean affectRender) { if (distanceFromCurPoint == 0) info.setLastTraversedDistance(0); if (info == null || nodes.size() == 0) { return; } if (method == METHOD_LEFT) { traverseLeft(info, distanceFromCurPoint, method, affectRender); } else if (method == METHOD_RIGHT) { traverseRight(info, distanceFromCurPoint, method, affectRender); } // updateRenderingInfo(distanceFromCurPoint); // if(nodes.getLast() == info.getPathNode() && ) // info.initialize(nodes.getLast().getLocation(), nodes.getLast(), 0, 0, this, 0); }
/** * Starts traversing with given hit point and maxDistance from the hit. It returns closest point * on the closest segment. * * @param hitPoint Any point on the screen. * @param maxDistance Maximum distance from the hit-point to the landing point on the comparing * path segment. Needed for detection. * @return Using the given hit-point, returns TraversePointInfo for the landing point on the Path */ public TraversePointInfo startTraverse0(Point hitPoint, float maxDistance) { Helper.println("\n\n\nStarting: "); float distance, d; for (int i = 0; i < nodes.size() - 1; i++) { // if(!insideCheck(nodes.get(i).getLocation(), nodes.get(i+1).getLocation(), hitPoint)) // continue; distance = (float) Helper.pointToLineDistance( nodes.get(i).getLocation(), nodes.get(i + 1).getLocation(), hitPoint); // Helper.printKeyVal("node: " + nodes.get(i)+ " to " + nodes.get(i+1), distance); if (distance <= maxDistance) { // Helper.printKeyVal("Node selected: ", nodes.get(i).getLocation().toString()); TraversePointInfo info = new TraversePointInfo(); info.setPathNode(nodes.get(i)); d = (float) Math.pow(info.getPathNode().getLocation().distancePoint2Point(hitPoint), 2); distance = distance * distance; // Helper.printKeyVal("Distance measured: ", Math.sqrt(d-distance)); distance = (float) Math.sqrt(d - distance); info.setTraverseLocation(info.getPathNode().getPointAtDistance(distance)); // Helper.printKeyVal("Point Set: ", info.getTraverseLocation().toString()); info.setDistance(distance); // Helper.printKeyVal("Distance Set: ", info.getDistance()); for (int j = 0; j <= i; j++) { distance += nodes.get(j).getLeftDistance(); } info.setTotalDistanceInPath(distance); // Helper.printKeyVal("Total Distance Set: ", info.getTotalDistanceInPath()); info.setPath(this); return info; } } return null; }
/** * Starts traversing from the starting point of the path * * @return returns TraversePointInfo for the landing point on the Path */ public TraversePointInfo startTraverse() { TraversePointInfo info = new TraversePointInfo(); if (nodes == null) return null; if (nodes.size() == 0) return null; info.setPathNode(nodes.get(0)); info.setTraverseLocation(nodes.get(0).getLocation()); info.setDistance(0); info.setTotalDistanceInPath(0); info.setPath(this); return info; }
public void traverseRight( TraversePointInfo info, float distanceFromCurPoint, int method, boolean affectRender) { // if info.pathnode is null, then according to mehtod get one of the end path nodes if (info.getPathNode() == null) { PathNode pt = nodes.getFirst(); info.initialize(pt.getLocation(), pt, 0, 0, this, 0); if (affectRender) pt.setActive(false); // Helper.printKeyVal("info pathnode in null check: ", info.getPathNode().toString()); return; } // info.pathnode is not null // Helper.printKeyVal("\n\nCurrent distance: ", info.getDistance()); // Helper.println("distance from current point: " + distanceFromCurPoint); try { float traversedDistance = 0; float distanceToTraverse = distanceFromCurPoint + info.getDistance(); float distance = info.getDistance(); PathNode t = info.getPathNode(); while (true) { nextNode = t.getNextTraversingNode(method); // current is the last node if (nextNode == null) { if (affectRender) t.setActive(false); info.initialize( t.getLocation(), t, this.totalDistance, this.totalDistance, this, traversedDistance); // Helper.println("2 Right Return last"); return; } if (!nextNode.isActive()) { info.setLastTraversedDistance(0); return; } // Helper.printKeyVal("2 t.linkDistance: ", t.getDistance(method)); // Helper.printKeyVal("2 out of distanceToTraverse: ", distanceToTraverse); linkDistance = t.getDistance(method); if (linkDistance >= distanceToTraverse) { traversedDistance += distanceToTraverse; // Helper.printKeyVal("\n3 traveresed distance: ", "" + traversedDistance); // Helper.printKeyVal("3 out of : ", distanceToTraverse); Point retP = t.getPointAtDistance(distanceToTraverse, method); if (affectRender) t.setActive(false); // Helper.printKeyVal("getPointAtDistance() returned point: ", retP.toString()); // Helper.printKeyVal("Right traverse Last distance: ", traversedDistance + // distanceToTraverse-info.getDistance()); info.initialize( retP, t, distanceToTraverse, info.getTotalDistanceInPath() + distanceFromCurPoint, this, distanceFromCurPoint); // Helper.println("2 Return good"); return; } traversedDistance += linkDistance; distanceToTraverse = distanceToTraverse - linkDistance; distance = 0; t = nextNode; // Helper.printKeyVal("Switching to new node: ", t.toString()); } } catch (Exception e) { e.printStackTrace(); } }
public void traverseLeft( TraversePointInfo info, float distanceFromCurPoint, int method, boolean affectRender) { PathNode t; float dtv; float dis; /** * ************************************************** * * <p><-----dtv-----> ********************* <-- t * * <p>************************************************* */ t = info.getPathNode(); if (t == null) { t = info.getPath().nodes.getLast(); info.initialize(t.getLocation(), t, t.getLeftDistance(), totalDistance, this, 0); if (affectRender) t.setActive(false); // Helper.println("Return from: " + t.getLocation().toString()); return; } // Helper.printKeyVal("Step 1: DFCP", distanceFromCurPoint); // Helper.printKeyVal("Step 1: current distance", info.getDistance()); if (t.getNextNode() == null) dtv = distanceFromCurPoint; else { if (info.getDistance() > 0) { dtv = t.getRightDistance() - info.getDistance() + distanceFromCurPoint; // if(affectRender) // t.setActive(false); t = t.getNextNode(); } else { dtv = distanceFromCurPoint; } } // Helper.printKeyVal("Step 1: DTV", dtv); // Helper.printKeyVal("Step 1: t.getLeftDistance()", t.getLeftDistance()); while (true) { if (dtv > t.getLeftDistance()) { // Helper.printKeyVal("\nStep 2: DTV", dtv); // Helper.printKeyVal("Step 2: t.getLeftDistance(): ", t.getLeftDistance()); if (t.getPrevNode() == null) { if (affectRender) t.setActive(false); info.initialize( t.getLocation(), t, 0, info.getTotalDistanceInPath() - (distanceFromCurPoint - dtv), this, distanceFromCurPoint - dtv); // Helper.printKeyVal("Retrun: 1: ", info.getDistance()); return; } dtv = dtv - t.getLeftDistance(); if (affectRender) t.setActive(false); t = t.getPrevNode(); if (!t.isActive()) { info.setLastTraversedDistance(0); return; } // Helper.printKeyVal("Step 2: New DTV", dtv); // Helper.printKeyVal("Step 2: Switched to new Node: ", t.getLocation().toString()); // Helper.printKeyVal("Step 2: New Node index: ", info.getPath().nodes.indexOf(t)); } else break; } /** ************************************************* */ Point p = t.getPointAtDistance(dtv, Path.METHOD_LEFT); info.initialize( p, t.getPrevNode(), t.getLeftDistance() - dtv, info.getTotalDistanceInPath() - distanceFromCurPoint, this, distanceFromCurPoint); // t.setActive(false); // Helper.printKeyVal("Step 2: New Node index: ", info.getPath().nodes.indexOf(t)); // Helper.printKeyVal("Step 2: New Node index: ", // info.getPath().nodes.indexOf(t.getPrevNode())); // Helper.printKeyVal("Retruning point: ", p.toString()); // Helper.printKeyVal("Retrun: 2", info.getDistance()); }