Advertisement
Faguss

fwatch line intersection

Aug 26th, 2020
931
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.24 KB | None | 0 0
  1. case C_INFO_LINEINTERSECTION:
  2. {
  3.     // Parse input
  4.     char *input       = par[2];
  5.     int input_length  = strlen(input);
  6.     float point[8]   = {0};
  7.     int point_index   = 0;
  8.     int bracket_level = 0;
  9.     int number_start  = -1;
  10.     int input_index   = 0;
  11.  
  12.     for (int i=0; i<input_length && point_index<8; i++) {
  13.         if (bracket_level==2 && isdigit(input[i]) && number_start==-1)
  14.             number_start = i;
  15.  
  16.         if (number_start>=0 && (input[i]==',' || input[i]==']')) {
  17.             if (input_index++ < 2) {
  18.                 char save            = input[i];
  19.                 input[i]             = '\0';
  20.                 point[point_index++] = (float)atof(input+number_start);
  21.                 input[i]             = save;
  22.             }
  23.  
  24.             number_start = -1;
  25.         }
  26.  
  27.         if (input[i]=='[')
  28.             bracket_level++;
  29.  
  30.         if (input[i]==']') {
  31.             bracket_level--;
  32.             input_index = 0;
  33.         }
  34.     }
  35.  
  36.  
  37.     // Calculate intersection
  38.     float a = point[3] - point[1];
  39.     float b = point[0] - point[2];
  40.     float c = a * point[0] + b * point[1];
  41.  
  42.     float a1 = point[7] - point[5];
  43.     float b1 = point[4] - point[6];
  44.     float c1 = a1 * point[4] + b1 * point[5];
  45.  
  46.     double det = a*b1 - a1*b;
  47.  
  48.     if (det == 0) {
  49.         QWrite("[0,0]", out);
  50.     } else {
  51.         char tmp[32] = "";
  52.         sprintf(tmp, "[%.6f,%.6f]", (b1*c-b*c1)/det, (a*c1-a1*c)/det);
  53.         QWrite(tmp, out);
  54.     }
  55. }
  56. break;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement