public E3DViewFrustum2D( E3DEngine engine, E3DViewport viewport, E3DVector2F minMin, E3DVector2F minMax, E3DVector2F maxMax, E3DVector2F maxMin) { super(engine); setA(minMin); setB(minMax); setC(maxMax); setD(maxMin); setViewport(viewport); triOne = new E3DTriangle( getEngine(), new E3DVector3F(a.getX(), a.getY(), 0.0), new E3DVector3F(b.getX(), b.getY(), 0.0), new E3DVector3F(c.getX(), c.getY(), 0.0)); triTwo = new E3DTriangle( getEngine(), new E3DVector3F(a.getX(), a.getY(), 0.0), new E3DVector3F(c.getX(), c.getY(), 0.0), new E3DVector3F(d.getX(), d.getY(), 0.0)); }
/** * Get the intersection pt of two 2d lines made of A->B and C->D. TODO: This is a misplaced * method, but its only used currently by this frustum. * * @param ptA First point of segment1 * @param ptB Second point of segment1 * @param ptC First point of segment2 * @param ptD Second point of segment2 * @return */ public E3DVector3F get2DLineIntersectionPt( E3DVector2F ptA, E3DVector2F ptB, E3DVector3F ptC, E3DVector3F ptD) { // First, put into y = mx+b form boolean vertical1 = false; double m1 = 0.0; double b1; if (ptB.getX() != ptA.getX()) { m1 = (ptB.getY() - ptA.getY()) / (ptB.getX() - ptA.getX()); // rise b1 = ptA.getY() - (m1 * ptA.getX()); // Solve for B we now have m and b } else { vertical1 = true; b1 = ptA.getX(); } // Segment 2 in y=mx+b form boolean vertical2 = false; double m2 = 0.0; double b2; if (ptD.getX() != ptC.getX()) // denominator of 0 means vertical line { m2 = (ptD.getY() - ptC.getY()) / (ptD.getX() - ptC.getX()); // rise b2 = ptC.getY() - (m2 * ptC.getX()); } else { vertical2 = true; b2 = ptC.getX(); } // If both are vertical see if the two segments intersect if (vertical1 && vertical2) { if (b1 != b2) // parallel lines, no intersection return null; else // equal means they are the same line, see if the segments intersect { // check if either pt of segment 2 is withing segment 1. If so, return that point (since // there can be multiple intersection pts when they're in the same line, it doesn't matter // exactly what pt) if ((ptD.getY() >= ptA.getY() && ptD.getY() <= ptB.getY()) || // D is between A and B (ptD.getY() <= ptA.getY() && ptD.getY() >= ptB.getY())) return ptD; else if ((ptC.getY() >= ptA.getY() && ptC.getY() <= ptB.getY()) || // C is between A and B (ptC.getY() <= ptA.getY() && ptC.getY() >= ptB.getY())) return ptC; else // don't intersect return null; } } else if (vertical1) // if one is vertical, find out if an x value of the vertical yields a Y // value on the line is between the pts of the vertical line { double intX = b1; double intY = (m2 * intX) + b2; if (((intY >= ptD.getY() && intY <= ptC.getY()) || (intY >= ptC.getY() && intY <= ptD.getY())) && (intX >= ptD.getX() && intX <= ptC.getX()) || (intX >= ptC.getX() && intX <= ptD.getX())) return new E3DVector3F(intX, intY, 0.0); else return null; } else if (vertical2) // C->D is vertical { double intX = b2; double intY = (m1 * intX) + b1; if (((intY >= ptA.getY() && intY <= ptB.getY()) || (intY >= ptB.getY() && intY <= ptA.getY())) && (intX >= ptA.getX() && intX <= ptB.getX()) || (intX >= ptB.getX() && intX <= ptA.getX())) return new E3DVector3F(intX, intY, 0.0); else return null; } else { // Now set the Y's equal to each other to solve for X, then replace X in one of the equations // to get Y. That is the intersection pt of the lines // m1x + b1 = m2x + b2 // = m1x = m2x + b2 - b1 = // = m1x - m2x = b2 - b1 // = (m1-m2)x = (b2 - b1) // = x = (b2 - b1) / (m1 - m2) double intX = 0.0; if (m1 - m2 != 0.0) intX = (b2 - b1) / (m1 - m2); else intX = (b2 - b1); double intY = (m1 * intX) + b1; // get Y from that X if (intY == 0) intY = (m2 * intX) + b2; // Now determine if intX and intY lie on both lines ptA->ptB and ptC->ptD if (((intX >= ptA.getX() && intX <= ptB.getX()) || (intX >= ptB.getX() && intX <= ptA.getX())) && ((intX >= ptC.getX() && intX <= ptD.getX()) || (intX >= ptD.getX() && intX <= ptC.getX())) && ((intY >= ptA.getY() && intY <= ptB.getY()) || (intY >= ptB.getY() && intY <= ptA.getY())) && ((intY >= ptC.getY() && intY <= ptD.getY()) || (intY >= ptD.getY() && intY <= ptC.getY()))) return new E3DVector3F(intX, intY, 0.0); else return null; } }