Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- case C_INFO_LINEINTERSECTION:
- {
- // Parse input
- char *input = par[2];
- int input_length = strlen(input);
- float point[8] = {0};
- int point_index = 0;
- int bracket_level = 0;
- int number_start = -1;
- int input_index = 0;
- for (int i=0; i<input_length && point_index<8; i++) {
- if (bracket_level==2 && isdigit(input[i]) && number_start==-1)
- number_start = i;
- if (number_start>=0 && (input[i]==',' || input[i]==']')) {
- if (input_index++ < 2) {
- char save = input[i];
- input[i] = '\0';
- point[point_index++] = (float)atof(input+number_start);
- input[i] = save;
- }
- number_start = -1;
- }
- if (input[i]=='[')
- bracket_level++;
- if (input[i]==']') {
- bracket_level--;
- input_index = 0;
- }
- }
- // Calculate intersection
- float a = point[3] - point[1];
- float b = point[0] - point[2];
- float c = a * point[0] + b * point[1];
- float a1 = point[7] - point[5];
- float b1 = point[4] - point[6];
- float c1 = a1 * point[4] + b1 * point[5];
- double det = a*b1 - a1*b;
- if (det == 0) {
- QWrite("[0,0]", out);
- } else {
- char tmp[32] = "";
- sprintf(tmp, "[%.6f,%.6f]", (b1*c-b*c1)/det, (a*c1-a1*c)/det);
- QWrite(tmp, out);
- }
- }
- break;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement