Tuesday, 10 June 2014

Lagrange Interpolation Curve Drawing



/* This routine draws Lagrange interpolation curves with 4 points.
/* It accepts four input points and finds out some other points
/* on the curve at specified parameter values.              
/* Output is presented in text mode only.                    
/******************************************************************/


#include<stdio.h>
#include<stdlib.h>

typedef struct {float x, y;} point;
point SmpPt[4], NewPt[10];

main()
{
 int i, n;
 float j0, j1, j2, j3;
 float u, v[10];

 for(i=0;i<4;i++)
   {
    printf("\nGive sample point[%d]:",i);
    scanf("%f %f", &SmpPt[i].x, &SmpPt[i].y);
   }
 printf("\nHow many new points ? ");
 scanf("%d", &n);
                                                                                                        
 for(i=0;i<n;i++)
   {
    printf("\nGive u value[%d](1.0 - 3.0): ",i);
    scanf("%f", &v[i]);
   }
                                                                                                        
 printf("u \t j0\t j1\t j2\t j3\t x\t y\n");

 for(i=0;i<n;i++)
 {
 u=v[i];
 j0=(u-1.0)*(u-2.0)*(u-3.0)/(-6.0);
 j1=u*(u-2.0)*(u-3.0)/2.0;
 j2=u*(u-1.0)*(u-3.0)/(-2.0);
 j3=u*(u-1.0)*(u-2.0)/6.0;

 NewPt[i].x = SmpPt[0].x*j0 + SmpPt[1].x*j1 +SmpPt[2].x*j2 +SmpPt[3].x*j3;
 NewPt[i].y = SmpPt[0].y*j0 + SmpPt[1].y*j1 +SmpPt[2].y*j2 +SmpPt[3].y*j3;
                                                                                                        
 printf("%4.3f   %4.3f   %4.3f   %4.3f   %4.3f   %4.3f   %4.3f\n",
                  u,j0,j1,j2,j3, NewPt[i].x,NewPt[i].y);

 }
}


No comments:

Post a Comment