public bool FindIntersect(Point start, Point end, ref Point intersect, ref int index) { bool found = false; double A1 = end.Y - start.Y; double B1 = end.X - start.X; double C1 = A1 * start.X + B1 * start.Y; double path_dist = Get_Dist(start, end); double i_dist = double.MaxValue; //find which border get intersected by our line //return the point of intersection and the index of the side intersected for (int i = 0; i < PointList.Count; i++) { Point left = (Point)PointList[i]; Point right = (Point)PointList[Get_Right_Point(i)]; double A2 = right.Y - left.Y; double B2 = right.X - left.X; double C2 = A2 * left.X + B2 * left.Y; double det = A1 * B2 - A2 * B1; double c_x = (B2 * C1 - B1 * C2) / det; double c_y = (A1 * C2 - A2 * C1) / det; if ((c_x == left.X && c_y == left.Y) || (c_x == right.X && c_y == right.Y)) { //ignore collision on the end points } else { //need to determine if this collision is between start <-> end if (Util.MIN(left.X, right.X) <= c_x && c_x <= Util.MAX(left.X, right.X) && Util.MIN(left.Y, right.Y) <= c_y && c_y <= Util.MAX(left.Y, right.Y)) { //if so... get the dist from start to the collision // set intersect and i_dist double n_dist = Math.Sqrt(Math.Pow(start.X - c_x, 2) + Math.Pow(start.Y - c_y, 2)); if (n_dist < i_dist) { i_dist = n_dist; intersect.X = (float)c_x; intersect.Y = (float)c_y; index = i;//left point found = true; } } else { //not inside our line segment } } } return(found); }
public bool IsPointInside(Point p) { if (PointList.Count < 3) { return(true); } int counter = 0; //need to run rays and check for collision, float xinters; Point p1, p2; int i; try { p1 = (Point)PointList[0]; for (i = 1; i <= PointList.Count; i++) { p2 = (Point)PointList[i % PointList.Count]; if (p.Y > Util.MIN(p1.Y, p2.Y)) { if (p.Y <= Util.MAX(p1.Y, p2.Y)) { if (p.X <= Util.MAX(p1.X, p2.X)) { if (p1.Y != p2.Y) { xinters = (p.Y - p1.Y) * (p2.X - p1.X) / (p2.Y - p1.Y) + p1.X; if (p1.X == p2.X || p.X <= xinters) { counter++; } } } } } p1 = p2; } //next we do a modulus and return out result if (counter % 2 == 0) { return(false); } return(true); } catch { Globals.l2net_home.Add_Error("border check failed... get those mexicans out of here!"); return(false); } }
/* Fringe Search * Fringe search is a memory enchanced version of IDA* */ public bool fringeSearch() { //initialize: ArrayList nowList = new ArrayList(); ArrayList laterList = new ArrayList(); ArrayList rejectedList = new ArrayList(); int limit = calcHvalue(startNode); #if DEBUG Globals.l2net_home.Add_Debug("start limit:" + limit); #endif bool found = false; Globals.debugPath = nowList; nowList.Add(startNode); while (!found) { // Globals.l2net_home.Add_Debug("big loop..."); int fmin = INFINITY; while (nowList.Count != 0) { AstarNode head = (AstarNode)nowList[0]; head.fvalue = calcFvalue(head); #region check for goal if (isNodeTarget(head, targetNode.x, targetNode.y)) { found = true; break; } #endregion //check if head is over the limit if (head.fvalue > limit) { //transfer head from nowlist to laterlist. nowList.Remove(head); laterList.Add(head); //find the minimum of the nodes we will look at 'later' fmin = Util.MIN(fmin, head.fvalue); } else { #region expand head's children on to now list expand(head); //nodes are sorted by insert sort in this function bool addedChildren = false; foreach (AstarNode child in head.adjacentNodes) { //dont allow children already on path or adjacent to path or walls... if (!isNodeIn(nowList, child) && !isNodeIn(laterList, child) && !isNodeIn(rejectedList, child)) { if (child.passable == true) { //add child of head to front of nowlist nowList.Insert(0, child); addedChildren = true; } } } if (!addedChildren) { nowList.Remove(head); rejectedList.Add(head); } #endregion } } if (found == true) { break; } //set new limit // Globals.l2net_home.Add_Debug("new limit:" + fmin); limit = fmin; //set now list to later list. nowList = (ArrayList)laterList.Clone(); nowList.Sort(); Globals.debugPath = nowList; laterList.Clear(); } if (found == true) { #if DEBUG Globals.l2net_home.Add_Debug("found a path... building..."); #endif buildPathFromParents((AstarNode)nowList[0]); return(true); } return(false); }