What is wrong with my 2D Interpolation C code?

#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

“);
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&lt;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 

",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 &lt; vertex_Count;i++){
    int m = i+1;
    if(m==vertex_Count)
        m = 0;
          
    borderLine = borderLines + i*N;
     
    for(j = 0;j &lt; 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-&gt;x = v[i].x*landa + v[m].x*k;
        temp-&gt;y = v[i].y*landa + v[m].y*k;
        temp-&gt;r = v[i].r*landa + v[m].r*k;
        temp-&gt;g = v[i].g*landa + v[m].g*k;
        temp-&gt;b = v[i].b*landa + v[m].b*k;
        temp-&gt;isVisited = 'n'; // I didn't visit this point yet
        //fprintf(g,"%f %f %f %f %f

",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&lt; start ;i++){
       
    searcherBorder = i + borderLines;
       
    for(j = i - i%N + N +1; j&lt; finish; j++){
             
        wantedBorder = j + borderLines;
        if((searcherBorder-&gt;y)==(wantedBorder-&gt;y) && searcherBorder-&gt;isVisited=='n' && wantedBorder-&gt;isVisited=='n'){
            //these points have been visited                                            
            searcherBorder-&gt;isVisited = 'y';
            wantedBorder-&gt;isVisited = 'y';
                 
            interPolationLine = interPolationLines + counter*N;
            //counter variable counts the points have same y coordinates.
            counter++;
            //printf("%d %d %d

",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-&gt;x = (wantedBorder-&gt;x)*landa + (searcherBorder-&gt;x)*k;
                temp-&gt;y = (wantedBorder-&gt;y)*landa + (searcherBorder-&gt;y)*k;
                temp-&gt;r = (wantedBorder-&gt;r)*landa + (searcherBorder-&gt;r)*k;
                temp-&gt;g = (wantedBorder-&gt;g)*landa + (searcherBorder-&gt;g)*k;
                /*if(temp-&gt;x==temp-&gt;y)
                    printf("%f  %f 

",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&lt; counter;i++){
    interPol = interPolationLines + i*N;
    for(j = 0;j&lt; N;j++){
        temp = interPol + j;
        glColor3f((temp)-&gt;r,(temp)-&gt;g,(temp)-&gt;b);
        //fprintf(g,"%f %f 

“,(temp)->x,(temp)->y);
glVertex2f ((temp)->x,(temp)->y);
}
}
//printf(”%d
",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;
}
I am implementing 2D Interpolation of a convex polygon and my code does not care about concav.my code works for some convex polygons but for others fail.For those my code fails it does not draw middle of the polygon.it only draws an upper and lower triangle.it reads vertexes from file vertex.txt and its format:x co,y co,red,green,blue color info of that point like below and for the values below my code fails.Thanks for replies in advance.I will get mad.
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

Concav polygons almost everytime need to be tesselated (splitted into triangles) to be drawn correctly!

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.