bool VoxelProjectedPolygon::pointInside(const glm::vec2& point) const { // first check the bounding boxes, the point must be fully within the boounding box of this shadow if ((point.x > getMaxX()) || (point.y > getMaxY()) || (point.x < getMinX()) || (point.y < getMinY())) { return false; } float e = (getMaxX() - getMinX()) / 100.0f; // some epsilon // We need to have one ray that goes from a known outside position to the point in question. We'll pick a // start point just outside of our min X glm::vec2 r1p1(getMinX() - e, point.y); glm::vec2 r1p2(point); glm::vec2 r2p1(getVertex(getVertexCount()-1)); // start with last vertex to first vertex glm::vec2 r2p2; // Test the ray against all sides int intersections = 0; for (int i = 0; i < getVertexCount(); i++) { r2p2 = getVertex(i); if (doLineSegmentsIntersect(r1p1, r1p2, r2p1, r2p2)) { intersections++; } r2p1 = r2p2; // set up for next side } // If odd number of intersections, we're inside return ((intersections & 1) == 1); }
// returns true if the given line intersects any side of the given rectangle bool checkLineToRect(double point1x, double point1y, double point2x, double point2y, Rectangle rect) { if (doLineSegmentsIntersect(point1x, point1y, point2x, point2y, rect.x, rect.y, rect.x, rect.y + rect.height)) return true; else if (doLineSegmentsIntersect(point1x, point1y, point2x, point2y, rect.x, rect.y, rect.x + rect.width, rect.y)) return true; else if (doLineSegmentsIntersect(point1x, point1y, point2x, point2y, rect.x, rect.y + rect.height, rect.x + rect.width, rect.y + rect.height)) return true; else if (doLineSegmentsIntersect(point1x, point1y, point2x, point2y, rect.x + rect.width, rect.y, rect.x + rect.width, rect.y + rect.height)) return true; return false; }
QLineF Branch::moveBranchToEdgeOfRect(QPointF currPoint, QLineF newBranch, float posOnParent) { QLineF tempLine = QLineF(branchLine.p1(), lv.getMaxPoint()); tempLine.setLength(tempLine.length() * posOnParent); currPoint = tempLine.p2(); QPointF differenceVec = newBranch.p1() - currPoint; newBranch.setP1(currPoint); newBranch.setP2(newBranch.p2() - differenceVec); QLineF tempNewBranch = newBranch; tempNewBranch.setLength(1000); QLineF sideLine1 = QLineF(maxRect[0], maxRect[3]); QLineF sideLine1Temp = sideLine1; sideLine1Temp.setLength(1000); sideLine1Temp = QLineF(sideLine1Temp.p2(), sideLine1Temp.p1()); sideLine1Temp.setLength(1000); QLineF sideLine2 = QLineF(maxRect[1], maxRect[2]); QLineF topLine = QLineF(maxRect[2], maxRect[3]); QLineF finalLine; if (posOnParent < 0.95) { bool isSide1 = doLineSegmentsIntersect(sideLine1Temp, tempNewBranch); // find relative position of point on branchLine //float fullDistance = findDistance(branchLine.p1(), branchLine.p2()); //float segDistance = findDistance(branchLine.p1(), currPoint); //float posOnParent = segDistance / fullDistance; // transpose to relative position on intersected rectangle line float outerRectLength; if (isSide1) outerRectLength = sideLine1.length(); else outerRectLength = sideLine2.length(); outerRectLength *= posOnParent; QPointF newStartPos; if (isSide1) { sideLine1.setLength(outerRectLength); newStartPos = sideLine1.p2(); } else { sideLine2.setLength(outerRectLength); newStartPos = sideLine2.p2(); } // find vector from point to newPoint QPointF changeVector = currPoint - newStartPos; // create newLine which moves newBranch by newPointVector finalLine.setP1(newStartPos); finalLine.setP2(newBranch.p2() - changeVector); } else { QPointF midPoint; midPoint.setX((topLine.p1().x() + topLine.p2().x())/2); midPoint.setY((topLine.p1().y() + topLine.p2().y())/2); QPointF diffVec = currPoint - midPoint; finalLine.setP1(midPoint); finalLine.setP2(newBranch.p2() - diffVec); } return finalLine; }