Thursday, November 2, 2017

centripetal parametrization for curve fitting or interpolation

very useful to compute parameter for curve fitting or interpolation
the detail explanation is written here

and here is the code for three dimensional parametric curve

 void GetCentripetal_Param(  
       int ncp,                                                  //number of control point  
       double point[][3],                                        //control points of curve  
       double t[]                                                //array of t parameter (output) 
      double *dcp = (double*)malloc(sizeof(double) * (ncp-1));   //CP0-CP1, CP2-CP1, ..., CP[ncp-1][ncp]  
      double *sum = (double*)malloc(sizeof(double) * ncp);  
      int i, j;  
      double dcptotal = 0.0;  
      for(i=0; i<ncp-1; i++)  
           dcp[i] = 0.0;  
      sum[0] = 0.0;  
      for(i=0; i<ncp-1; i++)  
        for(j=0; j<3; j++)  
          dcp[i] += (point[i+1][j]-point[i][j]) * (point[i+1][j]-point[i][j]);        
        dcp[i] = sqrt(sqrt(dcp[i]));  
        dcptotal += dcp[i];  
        sum[i+1] = dcptotal;  
      sum[0] = 0.0;  
      double inv_dcptotal = 1 / dcptotal;  
      for(i=0; i<ncp; i++)  
         t[i] = (sum[i] * inv_dcptotal);  
      if(dcp) free(dcp);  
      if(sum) free(sum);  

Tuesday, October 24, 2017

simple example of using operator overloading in c++

 #include <stdio.h>  
 #include <stdlib.h>  
 //3d point  
 struct Pt3d  
      double x,y,z;  
      //define operator "+" for Pt3d  
      Pt3d operator+(Pt3d &p)  
           Pt3d temp = {  
                this->x + p.x,   
                this->y + p.y,   
                this->z + p.z,   
           return temp;  
 int _tmain(int argc, _TCHAR* argv[])  
      Pt3d a, b, c;  
      a.x = 1.0;  
      a.y = 1.1;  
      a.z = 1.2;  
      b.x = 2.0;  
      b.y = 2.1;  
      b.z = 2.2;  
      //by only using this, you can get addition of point a and b  
      c = a + b;  
      //let's check it  
      //result should be 3.00, 3.20, 3.40  
      printf("coordinate of c in a, y, z is as follow:\n");  
      printf("%.2f, %.2f, %.2f \n", c.x, c.y, c.z);  
      return 0;  
3.00, 3.20, 3.40 

Thursday, June 22, 2017




Friday, June 16, 2017

compute derivative coefficient of n-th degree polynomial equation

 #include <stdio.h>  
 #define ELE 4  
 int main()  
  int i;  
  int g = 3; //degree  
  double c[ELE][3] = {{1, 5, 9},   
                      {2, 6, 10},  
                      {3, 7, 11},  
                      {4, 8, 12}};   
  //ELE coefficient of parametric equation  
  //eg ELE == 0 4x^3 + 3x^2 + 2x + 1 2d element of array is x, y, z  
  for(int i=0; i<ELE; i++)  
   for(int j=0; j<3; j++)  
     printf("%.1f ", c[i][j]);  
  int n = 2; //n-th derivative   
  for(int drv=1; drv<=n; drv++)  
   for(i=g; i>=0; i--)  
     for(int axs=0; axs<3; axs++) //if we want the derivative not be 3 dimensional parametric equation delete this loop  
  for(int i=0; i<ELE; i++)  
   for(int j=0; j<3; j++)  
     printf("%.1f ", c[i][j]);  

Friday, June 9, 2017

2d dynamic array with known number of column element

how to make dynamic array with known number of column element
for example for a 3d coordinate.

suppose i need to make 2d dynamic array for a 3d coordinate, because the element of 3d coordinate is x, y, z so the number column is 3.
let's say i need to have
double point[some_number][3];
and that number is suppose unknown until we read the file, lets say ptscount.

one simple way to allocate the dynamic 2d array is as follow. 

double (*point)[3] = (double (*)[3])malloc(sizeof(double) * ptscount*3);

brief explanation
- double (*point)[3]: double variable of  "point" that has 3 array element, this will make point[ptscount][3]

- (double (*)[3]): casting to double of pointer that has 3 array element

- malloc(sizeof(double) * ptscount*3): memory allocation for (the size of double variable) times(×) (ptscount (number or row of the array) with 3 column)

after using it you have to free the memory
free (point);

Thursday, June 1, 2017

use malloc for struct

 #include <stdio.h>  
 #include <stdlib.h>  
 struct Healdirect  
    double m_tors;  
    double curvedir[3];  
 void showme(struct Healdirect *healdeci)  
    struct Healdirect test;  
    test.m_tors = healdeci->m_tors;  
    test.curvedir[0] = healdeci->curvedir[0];  
    test.curvedir[1] = healdeci->curvedir[1];  
    test.curvedir[2] = healdeci->curvedir[2];  
    printf("torsion: %.2f \n", test.m_tors);  
    printf("curvature direction: ");  
    for(int i=0; i<3; i++)  
       printf("%.2f ", test.curvedir[i]);  
 void main()  
    //example of struct using pointer and malloc  
    struct Healdirect *hd = malloc(sizeof(hd));  
    //sample data   
    hd->m_tors = 0.24;  
   hd->curvedir[0] = 0.99;  
   hd->curvedir[1] = 0.88;  
   hd->curvedir[2] = 0.77;  
   //show sample data after the memory is filled  
   //free the memory  
   if(hd) free(hd);  

Tuesday, May 30, 2017

dynimcally allocated array 2

 #include <stdio.h>  
 #include <stdlib.h>  
 void main()  
    double *ary;   //will be your dynamic 1d array  
    int num;       //your array element  
    //input your number of array element  
    printf("num: ");  
    scanf("%d", &num);  
    //syntax: number of block address times byte needed (sizeof) for double variable  
    ary = malloc(num * sizeof(double));     
    //fill the array (can be changed to any number) and display them  
    for(int i=0; i<num; i++)  
       ary[i] = i * i * 2.8;  
       printf("%.2f\n", ary[i]);  
    //must free the memory allocation to not make memory leak  
    if(ary) free(ary);