2011-03-12 111 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代码适用于一些凸多边形但对于其他fail.For我的那些代码失败它不绘制多边形的中间,它只绘制一个上下三角形,它从文件vertex.txt和它的格式中读取顶点:x co,y co,红色,绿色,蓝色的颜色信息,如下图所示我的代码下面的值失败。提前答复,我会生气。有什么不对我的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

当它“失败”时会发生什么? – 2011-03-12 23:12:56

+0

我提到它不画中间的多边形 – emmpati 2011-03-13 02:22:11

回答

1

没有完全调试程序,我怀疑这行,for(j = i - i%N + N +1; j< finish; j++){的。我不知道你打算做什么,但它看起来很可疑。此外,我会建议不同的算法:

  1. 跟踪围绕多边形
  2. 马克跨越所需的y值
  3. 角的情况下拨出任何边缘,如果你准确地找到两支安打这里只有一个解决方案。
  4. 计算与y值
  5. 边缘的交叉执行的x插值

此外,简洁的问题是不是,“为什么不是我的工作方案?”更好原谅我,但它感觉像一个家庭作业问题。

注意:这应该是一个评论而不是答案?我在这里是新的......

+0

我已经在做你在算法中说的了。我发现了这个问题。在凸多边形中间点的坐标不匹配。所以它没有做(j = i -i%N + N + 1)意味着转到下一个边缘(不要搜索同一个y坐标上的边缘,因为它不存在) – emmpati 2011-03-13 06:12:32

+1

对不起没有太多的帮助,但至少它的作品!冒着成为真正的混蛋的风险,我建议避免像isVisited或错综复杂的数字像i-i%N + N + 1这样的“特殊标志”,除非绝对必要。这些技巧变得越来越难以理解和无法维持。我现在正在拉我的头发(并拖延)试图清理这个程序! – Ricky 2011-03-13 06:38:36