Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #define s_point struct point
- #define s_equation struct equation
- #define POINT 1
- #define INF 2
- #define EMPTY_SET 3
- struct point {
- float x;
- float y;
- };
- struct equation {
- float a;
- float b;
- float c;
- };
- float determinant(float x1, float y1, float x2, float y2){
- // float kk = x1*y2 - y1*x2;
- // printf("[inside determinant] x1=%f, y1=%f, x2=%f, y2=%f\n", x1, y1, x2, y2);
- // printf("[inside determinant] kk = %f\n", (double) x1*y2 - y1*x2);
- return x1*y2 - y1*x2;
- }
- s_point* intersection(s_equation* eq1, s_equation* eq2, int *type) {
- float a1, a2, b1, b2, c1, c2;
- float x;
- float y;
- a1 = eq1->a;
- a2 = eq2->a;
- b1 = eq1->b;
- b2 = eq2->b;
- c1 = eq1->c;
- c2 = eq2->c;
- // printf("we are inside intersection a1=%f b1=%f a2=%f b2=%f c1=%f c2=%f\n", a1, a2, b1, b2, c1, c2);
- s_point *new_p;
- float m = determinant(a1, b1, a2, b2);
- if ( m == 0){
- if (a1 == 0 && a2 == 0){
- if (determinant(c1, b1, c2, b2) == 0){
- // совпадение
- *type = INF;
- } else {
- // параллельность
- *type = EMPTY_SET;
- }
- } else
- if (b1 == 0 && b2 == 0){
- if (determinant(c1, a1, c2, a2) == 0){
- // совпадение
- *type = INF;
- } else {
- // параллельность
- *type = EMPTY_SET;
- }
- } else {//TODO:check for it
- if (determinant(c1, b1, c2, b2) == 0){
- // совпадение
- *type = INF;
- } else {
- // параллельность
- *type = EMPTY_SET;
- }
- }
- return NULL;
- } else { //пересечение
- float ssss = determinant(a1, b1, a2, b2);
- x = determinant(c1, b1, c2, b2)/ssss;
- y = determinant(a1, c1, a2, c2)/ssss;
- new_p = (s_point *) malloc(sizeof(s_point));
- new_p->x = x;
- new_p->y = y;
- *type = POINT;
- return new_p;
- }
- }
- int main() {
- int n;
- int res_type;
- FILE* fin;
- float f1, f2;
- s_point* tmp;
- char c;
- int inf_flag = 0;
- int size = 10;
- int cur_pos = 0;
- int should_add;
- s_point *res_points = (s_point *) calloc(size, sizeof(s_point));
- fin = fopen("input.txt", "r");
- fscanf(fin, "%d", &n);
- s_equation* input_data = (s_equation*)malloc(sizeof(s_equation)*n);
- // fscanf(fin, "%f", &f1);
- for (int i = 0; i < n; i++)
- fscanf(fin, "%f%f%f", &(input_data[i].a), &(input_data[i].b), &(input_data[i].c));
- // fscanf(fin, "%f", &f2);
- /*
- for (int i = 0; i < n; i++){
- printf("OK, i've read a=%f b=%f c=%f\n", input_data[i].a, input_data[i].b, input_data[i].c);
- }
- */
- for (int i = 0; i < n-1; i++){
- for (int j = i + 1; j < n; j++) {
- // printf("j = %d, input_data[j].c = %f\n", j, input_data[j].c);
- tmp = intersection(&(input_data[i]), &(input_data[j]), &res_type);
- // printf("%f %f\n", tmp->x, tmp->y);
- switch (res_type){
- case INF: inf_flag = 1; break;
- case POINT: //добавим в массив точку
- should_add = 1;
- for (int k = 0; k < cur_pos; k++){
- if (res_points[k].x == tmp->x &&
- res_points[k].y == tmp->y)
- {
- should_add = 0;
- break;
- }
- }
- if (should_add){
- //если массив забит, то увеличим количество места
- if (cur_pos == size){
- size += 10;
- res_points = realloc(res_points, sizeof(s_point)*size);
- }
- res_points[cur_pos++] = *tmp;
- if (tmp != NULL) free(tmp);
- }
- break;
- }
- }
- }
- if (inf_flag) printf("Infinity");
- else printf("%d", cur_pos);
- fclose(fin);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement