/* * Find where (x1, y1) -> (x2, y2) hits one of the lines in xvec, yvec. * (*x3, *y3) is the point where it hits. * */ void oglFindEndForPolyline(double n, double xvec[], double yvec[], double x1, double y1, double x2, double y2, double *x3, double *y3) { int i; double lastx = xvec[0]; double lasty = yvec[0]; double min_ratio = 1.0; double line_ratio; double other_ratio; for (i = 1; i < n; i++) { oglCheckLineIntersection(x1, y1, x2, y2, lastx, lasty, xvec[i], yvec[i], &line_ratio, &other_ratio); lastx = xvec[i]; lasty = yvec[i]; if (line_ratio < min_ratio) min_ratio = line_ratio; } // Do last (implicit) line if last and first doubles are not identical if (!(xvec[0] == lastx && yvec[0] == lasty)) { oglCheckLineIntersection(x1, y1, x2, y2, lastx, lasty, xvec[0], yvec[0], &line_ratio, &other_ratio); if (line_ratio < min_ratio) min_ratio = line_ratio; } *x3 = (x1 + (x2 - x1)*min_ratio); *y3 = (y1 + (y2 - y1)*min_ratio); }
bool PolylineHitTest(double n, double xvec[], double yvec[], double x1, double y1, double x2, double y2) { bool isAHit = FALSE; int i; double lastx = xvec[0]; double lasty = yvec[0]; double min_ratio = 1.0; double line_ratio; double other_ratio; for (i = 1; i < n; i++) { oglCheckLineIntersection(x1, y1, x2, y2, lastx, lasty, xvec[i], yvec[i], &line_ratio, &other_ratio); if (line_ratio != 1.0) isAHit = TRUE; lastx = xvec[i]; lasty = yvec[i]; if (line_ratio < min_ratio) min_ratio = line_ratio; } // Do last (implicit) line if last and first doubles are not identical if (!(xvec[0] == lastx && yvec[0] == lasty)) { oglCheckLineIntersection(x1, y1, x2, y2, lastx, lasty, xvec[0], yvec[0], &line_ratio, &other_ratio); if (line_ratio != 1.0) isAHit = TRUE; } return isAHit; }
void wxLineCrossings::FindCrossings(wxDiagram& diagram) { ClearCrossings(); wxNode* node1 = diagram.GetShapeList()->GetFirst(); while (node1) { wxShape* shape1 = (wxShape*) node1->GetData(); if (shape1->IsKindOf(CLASSINFO(wxLineShape))) { wxLineShape* lineShape1 = (wxLineShape*) shape1; // Iterate through the segments wxList* pts1 = lineShape1->GetLineControlPoints(); size_t i; for (i = 0; i < (pts1->GetCount() - 1); i++) { wxRealPoint* pt1_a = (wxRealPoint*) (pts1->Item(i)->GetData()); wxRealPoint* pt1_b = (wxRealPoint*) (pts1->Item(i+1)->GetData()); // Now we iterate through the segments again wxNode* node2 = diagram.GetShapeList()->GetFirst(); while (node2) { wxShape* shape2 = (wxShape*) node2->GetData(); // Assume that the same line doesn't cross itself if (shape2->IsKindOf(CLASSINFO(wxLineShape)) && (shape1 != shape2)) { wxLineShape* lineShape2 = (wxLineShape*) shape2; // Iterate through the segments wxList* pts2 = lineShape2->GetLineControlPoints(); int j; for (j = 0; j < (int) (pts2->GetCount() - 1); j++) { wxRealPoint* pt2_a = (wxRealPoint*) (pts2->Item(j)->GetData()); wxRealPoint* pt2_b = (wxRealPoint*) (pts2->Item(j+1)->GetData()); // Now let's see if these two segments cross. double ratio1, ratio2; oglCheckLineIntersection(pt1_a->x, pt1_a->y, pt1_b->x, pt1_b->y, pt2_a->x, pt2_a->y, pt2_b->x, pt2_b->y, & ratio1, & ratio2); if ((ratio1 < 1.0) && (ratio1 > -1.0)) { // Intersection! wxLineCrossing* crossing = new wxLineCrossing; crossing->m_intersect.x = (pt1_a->x + (pt1_b->x - pt1_a->x)*ratio1); crossing->m_intersect.y = (pt1_a->y + (pt1_b->y - pt1_a->y)*ratio1); crossing->m_pt1 = * pt1_a; crossing->m_pt2 = * pt1_b; crossing->m_pt3 = * pt2_a; crossing->m_pt4 = * pt2_b; crossing->m_lineShape1 = lineShape1; crossing->m_lineShape2 = lineShape2; m_crossings.Append(crossing); } } } node2 = node2->GetNext(); } } } node1 = node1->GetNext(); } }