Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.2 kB
2
Indexable
Never
#include<bits/stdc++.h>
#include<windows.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdlib.h>

using namespace std;

static int slices = 16;
static int stacks = 16;
map<pair<int,int> ,int> v;
struct edge{
    int e;
    float ymx;
    float ymn;
    float xmn;
    float mi;

}ed[10];

bool compare(edge p,edge q)
{
    if(p.ymn!=q.ymn)
      return p.ymn<q.ymn;
}

static void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0,1.0,1.0);
    glBegin(GL_LINES);
    int a[10],b[10];
    a[0]=0;b[0]=-4;
    a[1]=-4;b[1]=-2;
    a[2]=-4;b[2]=0;
    a[3]=-2;b[3]=2;
    a[4]=0;b[4]=0;
    a[5]=4;b[5]=4;
    a[6]=4;b[6]=-4;
    int i,n=7,j=0,k,sb=INT_MAX,se=INT_MIN;
    float z;
    for(i=0;i<n;i++)
    {
        sb=min(sb,b[i]);
        se=max(se,b[i]);
         k=(i+1)%n;
        glVertex3f(a[i],b[i],0);
        glVertex3f(a[k],b[k],0);
        if(b[i]==b[k])
            continue;
        ed[j].e=i;
        ed[j].ymx=max(b[i],b[k]);
        ed[j].ymn=min(b[i],b[k]);
        if(b[i]==ed[j].ymn)
        {
            ed[j].xmn=a[i];
        }
        else ed[j].xmn=a[k];
        z=(b[i]-b[k]);
        ed[j].mi=(a[i]-a[k])/z;
        j++;
    }

    ed[0].ymx-=1;
    ed[1].ymx-=1;
    sort(ed,ed+j,compare);
    for(i=0;i<j;i++)
    {
        cout<<ed[i].e<<"  "<<ed[i].ymn<<" "<<ed[i].ymx<<" "<<ed[i].xmn<<" "<<ed[i].mi<<endl;;
    }
    priority_queue<pair<int,int> ,
                    vector<pair<int,int> > ,
                    greater<pair<int,int> > > pq;
                //cout<<sb<<"___"<<se<<endl;
    map<int,int> mp;
    i=0;
    while(sb<=se)
    {
        for(;i<j;i++)
        {
            if(ed[i].ymn<=sb&&ed[i].ymx>=sb)
            {
                mp[i]=1;
            }
            else break;
        }
        for(k=0;k<j;k++)
        {
            if(mp[k]==0)
                continue;
            pq.push({ed[k].xmn,k});
            ed[k].xmn=ed[k].xmn+ed[k].mi;
        }
        //cout<<sb<<"+++"<<pq.size()<<endl;
        while(!pq.empty())
        {
            //cout<<pq.top().first<<" "<<pq.size();
            glVertex3f(pq.top().first,sb,0);
            pq.pop();
            glVertex3f(pq.top().first,sb,0);
            pq.pop();
        }
        for(k=0;k<j;k++)
        {
            if(ed[k].ymx<=sb)
                mp[k]=0;
        }
        sb++;

    }
    glEnd();
    glFlush();
}

void init()
{
    glClearColor(0.0,0.0,0.0,0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-20.0,20.0,-20.0,20.0,-10.0,10.0);
    /*int l;
    for(i=0;i<n;i++)
    {
       if(i==0)
        k=n-1;
       else k=i+1;
       int y1=-1,y2=-1;
       for(l=0;l<j;l++)
       {
           if(ed[i].j==i)
       }
    }*/
}

static void key(unsigned char key, int x, int y)
{
    switch (key)
    {
        case 27 :
        case 'q':
            exit(0);
            break;

        case '+':
            slices++;
            stacks++;
            break;

        case '-':
            if (slices>3 && stacks>3)
            {
                slices--;
                stacks--;
            }
            break;
    }

    glutPostRedisplay();
}

static void idle(void)
{
    glutPostRedisplay();
}

const GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };

const GLfloat mat_ambient[]    = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[]    = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[]   = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };

/* Program entry point */

int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(800,800);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE );

    glutCreateWindow("DEMO");
    init();
    glutDisplayFunc(display);


    glutMainLoop();

    return EXIT_SUCCESS;
}