2011-03-12 4 views
1
#include <GL/glut.h> 
#include <GL/gl.h> 
#include <stdio.h> 
#include <stdlib.h> 
#include <math.h> 
#define N 200 
typedef struct vertex{ 

    float x;     // x position of the point 
    float y;     // y position of the point 
    float r;     // red color component of the point 
    float g;     // green color component of the point 
    float b;     // blue color component of the point 
    char isVisited; 
}Vertex; 

Vertex *borderLines,*interPolationLines; 
int vertex_Count;// total vertex 
int counter;//counts matched y coordinates 
FILE *f,*g; 

void readTotalVertexCount(){ 

    if((f = fopen("vertex.txt","r"))==NULL){ 
     printf("File could not been read\n"); 
     return ; 
    } 
    fscanf(f,"%d",&vertex_Count); 

    /*if((g = fopen("points.txt","w"))==NULL){ 

     return ; 
    }*/ 
} 

void readVertexCoordinatesFromFile(){ 

    Vertex v[vertex_Count]; 
    borderLines = (Vertex *)calloc(N*vertex_Count,sizeof(Vertex)); 
    interPolationLines = (Vertex *)calloc(N*N*(vertex_Count-1),sizeof(Vertex)); 

    int i = 0;int j; 
    //read vertexes from file 
    while(i<vertex_Count){ 
     fscanf(f,"%f",&(v[i].x)); 
     fscanf(f,"%f",&(v[i].y)); 
     fscanf(f,"%f",&(v[i].r)); 
     fscanf(f,"%f",&(v[i].g)); 
     fscanf(f,"%f",&(v[i].b)); 
     //printf("%f %f \n",v[i].x,v[i].y); 
     i++; 
    } 

    Vertex *borderLine,*temp; 
    float k,landa; 

    // draw border line actually I am doing 1D Interpolation with coordinates of my vertexes 
    for (i = 0;i < vertex_Count;i++){ 
     int m = i+1; 
     if(m==vertex_Count) 
      m = 0; 

     borderLine = borderLines + i*N; 

     for(j = 0;j < N; j++){ 
      k = (float)j/(N - 1); 
      temp = borderLine + j; 
      landa = 1-k; 
      //finding 1D interpolation coord. actually they are borders of my convex polygon 
      temp->x = v[i].x*landa + v[m].x*k; 
      temp->y = v[i].y*landa + v[m].y*k; 
      temp->r = v[i].r*landa + v[m].r*k; 
      temp->g = v[i].g*landa + v[m].g*k; 
      temp->b = v[i].b*landa + v[m].b*k; 
      temp->isVisited = 'n'; // I didn't visit this point yet 
      //fprintf(g,"%f %f %f %f %f\n",temp->x,temp->y,temp->r,temp->g,temp->b); 
     } 
    } 
    /* here is actual place I am doing 2D Interpolation 
     I am traversing along the border of the convex polygon and finding the points have the same y coordinates 
     Between those two points have same y coord. I am doing 1D Interpolation*/ 
    int a;counter = 0; 
    Vertex *searcherBorder,*wantedBorder,*interPolationLine; 
    int start = N*(vertex_Count); int finish = N*vertex_Count; 

    for(i = 0;i< start ;i++){ 

     searcherBorder = i + borderLines; 

     for(j = i - i%N + N +1; j< finish; j++){ 

      wantedBorder = j + borderLines; 
      if((searcherBorder->y)==(wantedBorder->y) && searcherBorder->isVisited=='n' && wantedBorder->isVisited=='n'){ 
       //these points have been visited            
       searcherBorder->isVisited = 'y'; 
       wantedBorder->isVisited = 'y'; 

       interPolationLine = interPolationLines + counter*N; 
       //counter variable counts the points have same y coordinates. 
       counter++; 
       //printf("%d %d %d\n",i,j,counter); 
       //same as 1D ınterpolation  
       for(a= 0;a< N;a++){ 

        k = (float)a/(N - 1); 
        temp = interPolationLine + a; 
        landa = 1-k; 
        temp->x = (wantedBorder->x)*landa + (searcherBorder->x)*k; 
        temp->y = (wantedBorder->y)*landa + (searcherBorder->y)*k; 
        temp->r = (wantedBorder->r)*landa + (searcherBorder->r)*k; 
        temp->g = (wantedBorder->g)*landa + (searcherBorder->g)*k; 
        /*if(temp->x==temp->y) 
         printf("%f %f \n",wantedBorder->x,searcherBorder->x);*/ 
        temp->b = (wantedBorder->b)*landa + (searcherBorder->b)*k; 

       } 
      } 
     } 
    } 

    fclose(f); 
} 

void display(void){ 

    glClear(GL_COLOR_BUFFER_BIT); 
    glColor3f(1.0,1.0,1.0); 

    int i,j; 
    Vertex *interPol,*temp; 

    glBegin (GL_POINTS); 

    for(i = 0;i< counter;i++){ 
     interPol = interPolationLines + i*N; 
     for(j = 0;j< N;j++){ 
      temp = interPol + j; 
      glColor3f((temp)->r,(temp)->g,(temp)->b); 
      //fprintf(g,"%f %f \n",(temp)->x,(temp)->y); 
      glVertex2f ((temp)->x,(temp)->y); 
     } 
    } 
    //printf("%d\n",counter); 
    fclose(g); 
    glEnd(); 
    glFlush(); 
} 

void init(void){ 
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE); 
    glutInitWindowSize(900,500); 
    glutInitWindowPosition(200,100); 
    glutCreateWindow("2D InterPolation"); 
    glClearColor(0.0, 0.0, 0.0, 0.0); 
    glClear(GL_COLOR_BUFFER_BIT); 
    glShadeModel(GL_SMOOTH); 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); 
} 

int main(int argc, char** argv) 
{ 
    readTotalVertexCount(); 
    readVertexCoordinatesFromFile(); 
    glutInit(&argc,argv); 
    init(); 
    glutDisplayFunc(display); 
    glutMainLoop(); 
    return 0; 
} 

볼록 다각형의 2D 보간을 구현하고 있습니다. 내 코드는 볼록한 다각형에 대해 concav.my 코드가 작동하지 않지만 다른 코드에서는 작동하지 않습니다. 그것은 폴리곤의 가운데를 그리지 않습니다. 꼭 꼭지점과 꼭지점을 그립니다. 꼭지점은 파일 vertex.txt와 그 형식에서 읽습니다 : x co, y co, red, green, blue 내 코드 아래의 값은 실패합니다. 미리 응답 해 주셔서 감사합니다. 나는 화를 낼 것입니다.내 2D 보간 C 코드에 문제가 있습니까

7 
0.9 0.4 1.0 0.0 1.0 
0.8 0.2 1.0 0.0 1.0 
0.5 0.1 1.0 0.0 0.0 
0.3 0.3 0.0 0.0 1.0 
0.3 0.35 0.0 0.0 1.0 
0.4 0.4 0.0 1.0 0.0 
0.6 0.5 1.0 1.0 1.0 
+0

"실패"하면 어떻게됩니까? –

+0

나는 다각형의 중앙을 그려 내지 않는다고 언급했다 – emmpati

답변

1

프로그램을 완전히 디버깅하지 않으면 나는 for(j = i - i%N + N +1; j< finish; j++){이라는 줄을 의심 스럽습니다. 나는 네가하려는 일을 정확하게 모르지만 의심스러워 보인다. 또한, 내가 다른 알고리즘 추천 할 것입니다 : 당신이 정확히 두 개의 안타를 발견하면 다각형

  • 마크 옆 원하는 y 값을
  • 코너 케이스에 걸쳐 모든 가장자리

    1. 추적을 만 해결 방법이 있습니다.
    2. 수행의 x 보간 또한

    를 y 값

  • 와 가장자리의 교차점을 계산하고 간결한 질문은 "왜하지 않습니다 내 프로그램 작업?"보다는 더 낫다 용서해주세요.하지만 숙제 문제 같아요.

    참고 : 이것은 대답이 아닌 대답이어야합니까? 나는 여기 새로운 ...

  • +0

    나는 당신이 당신의 알고리즘에서 말한 것을 이미하고있다. 문제를 발견했다. 볼록 다각형의 중앙에있는 점의 좌표는 일치하지 않는다. 그렇게하지 않는다. x- 보간을하지 않고 stack.for (j = i -i % N + N +1)을 밀지 않는다는 것은 다음 가장자리로 넘어가는 것을 의미합니다 (존재하지 않기 때문에 같은 y 좌표에있는 가장자리를 찾지 마십시오). – emmpati

    +1

    미안 해요 그다지 도움이되지는 않았지만 적어도 작동합니다! 진짜 멍청이가 될 위험에 처해있어, 절대적으로 필요한 경우를 제외하고는 isVisited 나 i-i % N + N + 1과 같은 회선 번호와 같은 '특수 플래그'는 사용하지 않는 것이 좋습니다. 더 큰 프로그램이되면, 더 이상 이해할 수없고 유지할 수없는 이러한 트릭이됩니다. 나는 바로 이것의 프로그램을 정화하려고 노력하면서 (그리고 지연시키는) 내 머리카락을 당기고있다! – Ricky

    관련 문제