/* Sets the edges of the viewport to the specified absolute          */
/* coordinates (mm), measured with respect to the current subpage    */
/* boundaries                                                        */

#include "plplot.h"

void plsvpa(xmin,xmax,ymin,ymax)
float xmin, xmax, ymin, ymax;
{

    int nx, ny, cs;
    float sxmin, symin;
    float spdxmi, spdxma, spdymi, spdyma;
    float vpdxmi, vpdxma, vpdymi, vpdyma;

    int level;

    glev(&level);
    if (level<1) fatal("Please call PLSTAR before calling PLSVPA.");
    if((xmin>=xmax)||(ymin>=ymax)||(xmin<0.)||(ymin<0.))
        fatal("Invalid limits in PLSVPA.");

    gsub(&nx,&ny,&cs);
    if ((cs<=0) || (cs>(nx*ny)))
        fatal("Please call PLADV or PLENV to go to a subpage.");

    gspd(&spdxmi,&spdxma,&spdymi,&spdyma);
    sxmin = dcmmx(spdxmi);
    symin = dcmmy(spdymi);

    vpdxmi = mmdcx(sxmin+xmin);
    vpdxma = mmdcx(sxmin+xmax);
    vpdymi = mmdcy(symin+ymin);
    vpdyma = mmdcy(symin+ymax);
    
    svpd(vpdxmi,vpdxma,vpdymi,vpdyma);
    svpp(dcpcx(vpdxmi),dcpcx(vpdxma),dcpcy(vpdymi),dcpcy(vpdyma));
    sclp(dcpcx(vpdxmi),dcpcx(vpdxma),dcpcy(vpdymi),dcpcy(vpdyma));
    slev(2);
}

