Example #1
0
Line getLineinRect(Line& sLine, Rect& area) {
    if (!intersectLineRect(sLine, area))
        return {Point(0,0), Point(0,0)};
    Point A(area.x, area.y);
    Point B(area.x+area.width, area.y);
    Point C(area.x+area.width, area.y+area.height);
    Point D(area.x, area.y+area.height);
    Line segAB = {A, B};
    Line segBC = {B, C};
    Line segCD = {C, D};
    Line segDA = {D, A};
    if (sLine.end.x == sLine.begin.x)
        return {{sLine.begin.x, A.y}, {sLine.begin.x, D.y}};
    double k = (double)(sLine.end.y - sLine.begin.y)/(sLine.end.x - sLine.begin.x);
    if (k == 0)
        return {{A.x, sLine.begin.y}, {B.x, sLine.begin.y}};
    
    double b = (double)(sLine.begin.x*sLine.end.y - sLine.begin.y*sLine.end.x)/(sLine.begin.x - sLine.end.x);
    
    vector<Point> cache;
    if (ifLineCrossSeg(sLine, segAB))
        cache.push_back(Point((A.y-b)/k, A.y));
    if (ifLineCrossSeg(sLine, segBC))
        cache.push_back(Point(B.x, k*B.x+b));
    if (ifLineCrossSeg(sLine, segCD))
        cache.push_back(Point((C.y-b)/k, C.y));
    if (ifLineCrossSeg(sLine, segDA))
        cache.push_back(Point(D.x, k*D.x+b));
    return {cache[0], cache[1]};
}
bool UIRectangleMouseTransformFunctor::viewportToRenderingSurface(const Pnt2f& ViewportPoint,
                                                                  const Viewport* TheViewport,
                                                                  Pnt2f& Result) const
{
    //Get Viewport to View Space line
    Line l;
    if( !TheViewport->getCamera()->calcViewRay( l, ViewportPoint.x(), ViewportPoint.y(), *TheViewport ) )
    {
        return false;
    }

    //Transform Line to UIRectangle Space
    Matrix m ;
    getParent()->accumulateMatrix(m);

    m.invert();

    Pnt3f pos;
    Vec3f dir;

    m.multFull(l.getPosition (), pos);
    m.mult    (l.getDirection(), dir);

    l.setValue(pos, dir);
    //ia->scale(dir.length());

    //Intersect the Line with the UIRectangle quad
    Real32 t;
    if(!intersectLineRect(l,getParent()->getPoint(),
                          getParent()->getPoint() + Vec3f(getParent()->getWidth(),0,0),
                          getParent()->getPoint() + Vec3f(getParent()->getWidth(),getParent()->getHeight(),0),
                          getParent()->getPoint() + Vec3f(0,getParent()->getHeight(),0)
                          ,t))
    {
        return false;
    }

    //Return the point on the quad of the intersection if there was one
    Result.setValues(l.getPosition().x() + t*l.getDirection().x() - getParent()->getPoint().x(), 
                     getParent()->getHeight() - l.getPosition().y() - t*l.getDirection().y() + getParent()->getPoint().y());
    return true;
}