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;  
 } 
 
result:
 
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]);  
   printf("\n");  
  }  
  printf("\n--\n");  
   
  //derivative  
  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  
      c[i][axs]=c[i][axs]*(i-(drv-1));  
  //derivative  
   
  for(int i=0; i<ELE; i++)  
  {  
   for(int j=0; j<3; j++)  
     printf("%.1f ", c[i][j]);  
   printf("\n");  
  }  
   

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]);  
    printf("\n");  
 }  
   
 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  
   showme(hd);  
   
   //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);  
 }