Simple basic spiral code implementation 2 with tolerance 0.1

Simple basic spiral code implementation 2 with tolerance 0.1

This is the sequel of the previous code implementation of spiral, that time I made a very simple archimedean spiral with the same step over. But sometime, we want more than a step over, a distance between each point of a spiral (tolerance).

Here is my code implementation in c.

 /**********************************************************************  
  *  
  *  
  *    @par    Copyright(C)  
  *    2018     Tokyo University of Agriculture, Saville Lab.  
  *    All rights reserved.  
  *  
  *    @par    History:  
  *    - 2018/07/15    Saville Ramadhona  
  *        - first code.  
  *  
  ***********************************************************************/  
 /*  
  * Program to make spiral curve and put the point on spiral  
  * based on tollerance (point distance).  
  * Not based on parameter of spiral curve.  
  * eg. When tollerance is o.1, distance between adjacent points on spiral   
  * must not exceed the tollerance.   
  *  
  */  
   
   
 #include<stdio.h>  
 #include<math.h>  
   
 #define M_PI 3.14159265358979323846  
   
 /**  
  *    function to evaluate/compute point on spiral  
  *  
  *    @param[in]    param    spiral parameter  
  *    @param[in]    theta    angle in computed point (radians)  
  *    @param[out]    pt        array coordinate of computed point on spiral (x,y)   
  *    @return                radius of center point of spiral to computed point  
  */  
 double evalSpiralpt(double param, double theta, double pt[2])  
 {  
     double r = param*theta;  
     pt[0] = r * cos(theta);  
     pt[1] = r * sin(theta);  
     return r;  
 }  
   
 /**  
  *    function to check distance square between adjacent points  
  *    we do not use sqrt to make computation faster  
  *  
  *    @param[in]    point        newly computed point on spiral   
  *    @param[in]    prevpoint    previously computed point on spiral   
  *    @return                    distance square obetween adjacent points  
  */  
 double checkDistpt2(double point[2], double prevpoint[2])  
 {  
     return (point[0]-prevpoint[0])*(point[0]-prevpoint[0])   
             + (point[1]-prevpoint[1])*(point[1]-prevpoint[1]);  
 }  
   
 //spiral definition r = a + bθ  
 void main()  
 {  
     //remove file if exist  
   int status;  
   status = remove("C:\\Users\\BA17655\\Documents\\spiral.csv");  
     
   //prepare csv file to see the computation result  
   FILE *fp;  
   fp = fopen("C:\\Users\\BA17655\\Documents\\spiral.csv", "a");  
   
   fprintf(fp, "rad, theta, r, x, y, d\n");  
   
     double pi_2 = 2* M_PI;  
     int numTurns = 5;                                      //automatically determine by detecting outer edge, later!  
     double stepover = 0.1;                                 //tool path stepover (USER INPUT)  
     double distanceBetweenTurns = stepover * (1 / (pi_2)); //b  
     double theta = 0.0;                                    //starting angle  
     double offset = 0.0;                                   //a (offset of starting point)  
   
     /*  
     double pointsPerTurn = 20; //to be modified  
     for(int i=0; i<(pointsPerTurn*numTurns+1); i++)  
     {  
         double r = offset + (distanceBetweenTurns*theta);  
         double point[2];  
         point[0] = r * cos(theta);  
         point[1] = r * sin(theta);  
   
         printf("%f %f %f %f \n", theta*(180.0/M_PI), r, point[0], point[1]);  
         theta += pi_2 / pointsPerTurn;  
     }  
     */  
   
     double dist_thres2 = 0.01;          //distance threshold(control point tollerance - USER INPUT)  
     int cnt = 0;  
     double point[2];                    //x, y coordinate control point on spiral  
     double prevpoint[2];  
     double prevtheta;  
     double degree;  
     double sizelimit = numTurns * 360.0; //to sizelimit the size of spiral  
     do  
     {  
         //evaluate point in spiral   
         double r = evalSpiralpt(distanceBetweenTurns, theta, point);  
   
         if( cnt < 1 )  
         {  
             fprintf(fp, "%f, %f, %f, %f, %f\n", theta, theta*(180.0/M_PI), r, point[0], point[1]);  
             theta += pi_2 * dist_thres2;  
         }  
         if( cnt > 0 )  
         {  
             //check distance of control point  
             double dist2 = checkDistpt2(point, prevpoint);  
   
             //check if point distance exceed tolerance  
             if( dist2 > dist_thres2 )   
             { //if exeec tollerance, compute new control point  
                 do   
                 {  
                     //make the new point to be located in the middle of previous and current point  
                     theta = (theta + prevtheta) * 0.5;  
                     r = evalSpiralpt(distanceBetweenTurns, theta, point);  
                     dist2 = checkDistpt2(point, prevpoint);  
   
                     //when above is not enough, do it one more time!  
                     if( dist2 > dist_thres2 )  
                     {  
                         theta = (theta + prevtheta) * 0.5;  
                         r = evalSpiralpt(distanceBetweenTurns, theta, point);  
                         dist2 = checkDistpt2(point, prevpoint);  
                     }  
                 }while( dist2 > dist_thres2 ); //when point distance lies under the tollerance, exit loop  
             }  
   
             fprintf(fp, "%f, %f, %f, %f, %f, %f\n", theta, theta*(180.0/M_PI), r, point[0], point[1], dist2);  
             prevtheta = theta;  
             theta += pi_2 * dist_thres2;  
         }          
           
         cnt++;  
                   
         prevpoint[0] = point[0];  
         prevpoint[1] = point[1];  
         degree = theta*(180.0/M_PI);  
   
     }while( degree <= sizelimit );  
   
 }  

After running the code, it will print out the angle (radian), angle (degree), r of each point(interpreted as 1/curvature), x, y coordinate of each point, and the distance of each point.
The plotted spiral from the code in excel is as shown below.

Pretty simple right!



spiral 2

BB 情報システム論 第6回目 ビジネス戦略と情報システム1

バイオビジネス 情報システム論 第6回目 ビジネス戦略と情報システム1

PDFスライド

東京農業大学
国際バイオビジネス学科

サフィル ラマドナ



BB 情報システム論 第5回目 生活基盤としての情報システム

バイオビジネス 情報システム論 第5回目 生活基盤としての情報システム

PDFスライド

東京農業大学
国際バイオビジネス学科

サフィル ラマドナ

BB 情報システム論 第4回目 社会基盤としての情報システム

バイオビジネス 情報システム論 第4回目 社会基盤としての情報システム

PDFスライド

東京農業大学
国際バイオビジネス学科

サフィル ラマドナ

BB 情報システム論 第3回目 情報システムとコンピュータ

バイオビジネス 情報システム論 第3回目 情報システムとコンピュータ

PDFスライド

東京農業大学
国際バイオビジネス学科

サフィル ラマドナ





BB 情報システム論 第2回目 情報システムとは

バイオビジネス 情報システム論 第2回目 情報システムとは

PDFスライド

東京農業大学
国際バイオビジネス学科

サフィル ラマドナ

BB 情報システム論 第1回目 イントロダクション

バイオビジネス 情報システム論 第1回目 イントロダクション

PDFスライド

東京農業大学
国際バイオビジネス学科

サフィル ラマドナ