Miscellaneous > Programming & Networking
A programming challenge all up in your face.
TheQuirk:
I foolighly left my laptop's power adapter in Houston, so I haven't been able to visit the forum much. Anyway, mobrien_12's solution seems spot on (though granted, I don't think he needs my seal of approval :)).
Edit: Saw last message. I don't think you'll be able to do anything without having to revert back to using coordinates.
mobrien_12:
OK, I figured out how to handle the problem.
Calculate the distance between the endpoint of the line and the center of the circle ("s" in the attached diagram). We already know the distance from the circle center to the line (or it's infinite extension) ("d" in the diagram). We find the projection of "s" along the line ("p" in the diagram) using simple trig (we have a right triangle). If the projection is longer than the line, the circle center is not next to the line and we discard it. If, as shown in the figure, the projection is shorter than the line, then the circle is actually next to the line and there is no problem.
But in thinking up this solution I realized that the original challenge was missing one specification. What to do if the line goes into the circle but does not come out?
In other words, what if an endpoint is located within one of the circles?
I say we should ignore the circle in that particular case because we don't have both the beginning and end points are defined on it to tell us the path to take when walking around the edge.
[verwijderd door de beheerder]
worker201:
I was thinking about this earlier. I was going to include a simple test in my version. Once I know the 2 points where the line intersects the circle, I will test the x values with relation to the x values on my line. Given that a straight line has a 1 to 1 relationship between the domain and the range. For example, let's say my line runs from (5,2) to (25,8). If one of the x values of the intersection points is > 25 or < 5, then we know that the line segment of interest enters the circle but does not leave it (even though the infinitely extended line does). At that point, I agree, the circle should be discarded.
Currently, my biggest challenge is figuring out how to separate the input. Your Octave program uses a loop inside the filestream to assign the data to the variables. I just have to figure out how to do that in C (my C-skills are kinda weak).
worker201:
Meh, having some trouble with the beginning of the program. All I have so far is the code to read the data, and then print it, to verify it is working. But it doesn't work.
--- Code: ---#include
#include
main()
{
const float pi=3.14159;
FILE *fRead;
char filename[20];
float x1, y1;
float x2, y2;
int i, numcirc;
float circlex[15], circley[15], radius[15];
fRead = fopen("file1.dat", "r");
if (fRead==NULL) {
printf("File unavailable\n");
return 1;
}
fscanf(fRead, "%f", x1);
fscanf(fRead, "%f", y1);
fscanf(fRead, "%f", x2);
fscanf(fRead, "%f", y2);
fscanf(fRead, "%d", numcirc);
if (numcirc>15) {
printf("too many circles, aborting\n");
return 1;
}
for(i=0; i
mobrien_12:
The problem is the fscanf statements.
You have this format
fscanf(fid, formatstring, variable)
You need
fscanf(fid, formatstring, &variable)
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version