Task: Given a file containing information about the circles (radius and center coordinates). Rewrite all circles in file G, i.e. circles that do not intersect with other circles. Implement on binary files using data structures (struct). In general, writing to the file and reading from it did not cause any difficulties. But I still think it is more rational to check the circles for intersection in the array

FILE f, g;

struct Round { double X,Y; double radius; }; Round UED(Round x1,Round x2,Round y1,Round y2,Round r1,Round r2) { Round len.radius= sqrt((x2.X - x1.X) * (x2.X - x1.X) + (y2.Y - y1.Y) * (y2.Y -y1.Y)); if (len.radius > r1.radius + r2.radius) return len; } void main() { system("color F0"); setlocale(LC_ALL,"Russian"); int n; printf("Введите количество окружностей: "); scanf("%d",&n); if(n<2) printf("Это не о чём не говорит\n"); Round O; f=fopen("data.okr","wb"); for(int i=0;i<n;i++) { printf("Введите координаты центра окружности: "); scanf("%lg%lg",&O.X,&O.Y); printf("Введите длину радиуса окружности: "); scanf("%lg",&O.radius); fwrite(&O,sizeof(Round),1,f); } printf("Данные внесены\n"); fclose(f); Round *okr; okr=new Round[n]; int i=0; fopen("data.okr","rb"); while(true) { fread(&okr[i],sizeof(Round),1,f); if(feof(f)) break; printf("\nкоординаты центра окружности: %lg\t %lg ",okr[i].X,okr[i].Y); printf(" \nдлина радиуса окружности: %lg",okr[i].radius); i++; } fclose(f); _getch(); } 

At the request of the teacher data in the function

Round UED (Round x1, Round x2, Round y1, Round y2, Round r1, Round r2) {Round len.radius = sqrt ((x2.X - x1.X) * (x2.X - x1.X) + ( y2.Y - y1.Y) * (y2.Y -y1.Y)); if (len.radius> r1.radius + r2.radius) return len; } Should be fed through an array, not that 3 cycles to start ???

  • Well, probably not 3, but two. for (int i = 0; i <n-1; i ++) for (int j = i + 1; j <n; j ++) .. And why do you have x1, x2, y1, y2, r1, r2, len - - circle? Are numbers not enough? Round UED (Round x1, Round x2, Round y1, Round y2, Round r1, Round r2) {Round len.radius = sqrt ((x2.X - x1.X) * (x2.X - x1.X) + ( y2.Y - y1.Y) * (y2.Y -y1.Y)); if (len.radius> r1.radius + r2.radius) return len; } - alexlz
  • @alexlz: Numbers non-semantic. Another thing is that Round x1, Round x2, Round y1, Round y2 is nonsense: in the Round structure there is both a radius and both coordinates of the center. - VladD
  • @VladD same cool - for each attribute has its own circle. And the call to this function becomes more interesting, especially if the teacher also decides to aliasing himself. - alexlz
  • one
    @avp, @VladD deep research. The distance between the centers is greater than the sum of the radii. @avp does not need intersection points. And even if they are not there, but one inside the other is also a “non-single” circle. - alexlz
  • one
    I apologize for answering the comment below, but the place ended there. @avp, when the code is small, everything is fine, but when it is large and there are many incoherent abbreviations in the body, this complicates understanding and looking at the heading every time is not good. Of course, I don’t urge to act like rubists, but I also do not advise neglecting names. Otherwise, I propose to leave this offtopic, since the format of the hashcode, as I understand it, does not allow for discussions. - Alex Krass

1 answer 1

Perhaps it is worth helping a guy, still trying;) Only in logic you need to restore order.

  1. Functions. It is better if there are 2 of them. The first is yours, to check the intersection, just note the comments about the types: it should get 2 circles (Round) and return bool (or int). The second one - with two cycles for comparing each with each (i, j = [0, n-1], i! = J). So it will receive an array, but only one - the original one, and write single circles to the file.

  2. In determining the intersection, you can not extract the root, and square the sum of the radii. And by the way, what does the UED return if the condition is not met? Such a function, so that there is no tautology and provided that you understand the parameters, it is better to implement this (and even if!) Is not necessary:

     return (x2.X - x1.X) * (x2.X - x1.X) + (y2.Y - y1.Y) * (y2.Y -y1.Y) > (r1.radius + r2.radius) * (r1.radius + r2.radius); 
  3. If, when reading from a binary file, you know in advance the number of elements, then it is easier to read, all at once. And then print the array in a separate loop.

     fread(okr,sizeof(Round),n,f); // UPD 
  4. The array output is also better to implement as a function; please the teacher;)

  • one
    @paulgri: To your point 2: you advise something wrong. In which parameter comes the first circle? Second? Why x2.X but y2.Y ? - VladD
  • 2
    @VladD, apparently he advises based on the original function of the vehicle, and not his answer in paragraph 1. And in general, do not be lazy and shorten the name (for x2 x1 y2 y1 should be punished by re-counting). - Alex Krass
  • one
    It should be like this: two parameters are passed to the function, these are circles: Round round_1 and Round round_2. The distance between the centers should be greater than the sum of the radii for non-intersecting circles: sqrt ((round_1.x - round_2.x) * (round_1.x - round_2.x) + (round_1.y - round_2.y) * (round_1.y - round_2.y))> round_1.radius + round_2.radius. It is possible without a root, having squared both sides of the equation, although I would not sacrifice my understanding of productivity in institute problems. - Alex Krass
  • @ Alexey123, thanks! @VladD, of course, this is nonsense, and when the vehicle according to claim 1 brings order, everything will be ok :) The point of paragraph 2 is that multiplying is cheaper than extracting the root, and if one number is greater than another, then their squares will be in the same attitude. - paulgri
  • one
    @alexlz: Because x1 tells the programmer that this is only the x coordinate of the first circle. That is, the name contradicts the semantics and distracts the reader. (Yes, I know about α-conversion, but nomen est omen.) - VladD