Click here to Skip to main content
15,891,529 members

Comments by LeumasD (Top 7 by date)

LeumasD 16-Dec-15 0:27am View    
Ah.... I think I misunderstood you... you want to know what the last 6 lines of code are doing... right??

Well... they are the same thing as the lines inside the loop... they calculate the length of the last side of polygon and add that to the total_perimeter and print those.

The confusion you might be having is the setting the r1,q1 to be the same as r0,q0.

Well... in the file the data are the r,q for the polar coordinates for the VERTICES (i.e. points) of a POLYGON.

A Polygon is made out of SIDES with each side joining two vertices.

The dat in the file is giving all the vertices and does not repeat the first vertex as the same as the first one.

Let's say we have a 5 sided polygon...So the points in the file would be

Point 0
Point 1
Point 2
Point 3
Point 4

Thus if one calculates the sides
0 to 1
1 to 2
2 to 3
3 to 4

We would be missing the last side which is 4 back to 0.

Since the file does not list the first point again as the last point then we must make the program think that there is an additional read point and make that be the same values as the first point.

Thus when we exit from the loop reading all the points we make the additional step of setting r1,q1 to be the values for the already saved values of the first point which we saved in r0,q0.

After that we just proceed to calculate the distance between the points just as if we have just finished reading another point from the file but in fact it was not form the file since we have finished reading the file but we just made it appear as if we have read an additional point which ought to have been there to CLOSE THE POLYGON.

I hope that explains it.

BY THE WAY... there is a better way to do all this by using functions to do the calculations and the accumulation for the perimeter.

But I was not sure if you have learned about functions already or not.

LeumasD 15-Dec-15 23:30pm View    
In regards to the while (1) the 1 means true.

I was not sure what your compiler uses for true... is it TRUE or true or what... but 1 always means true in C or C++ so that while (1) is the same as saying while(true) or while (TRUE) etc.

Using a loop that is do...while(true) is an endless loop.

So the loop will never finish UNLESS some statement within the loop BREAKS the loop and exits.

If you notice that is what I do right after trying to read the two values r and q from the file and test to see if EOF has been triggered.

So the loop keeps going round and round reading from the file until the EOF is encountered upon which the statement break is executed and thus exiting the loop.

You had it before where you where testing to see if the EOF is encountered in the while() itself.

That was not the right place since it was either too early or too late and had no effect at all.

The loop needs to be exited right after attempting to read from the file and it happens to have been the EOF not later and if EOF is not encountered then testing for that condition at the while() is of no use.... as you have seen earlier where you were getting additional spurious printouts.


LeumasD 15-Dec-15 23:22pm View    
In regards to the return 1... If you notice for the error situations I used return 0.

This makes the program terminate and return a value of 0 (i.e. false)... you could instead have returned any value indicating some error condition.

I elected to also return 1 (i.e. true) when the program ran all the way to the end and terminated correctly... you could use instead a 0 to indicate no error and any other number to indicate an error code.

LeumasD 15-Dec-15 3:36am View    
I have just noticed that your data points are in Polar Coordinates not in Cartesian coordinates... so I adjusted the naming of variables in the pseudo code I gave you above.... read it again.

I will try and see if I can give you some real code in a few minutes.

LeumasD 15-Dec-15 2:19am View    
The algorithm you are using is reading two point coordinates at a time.

You really do not need to do that... what you ought to do is

1- read the first point r,q
2- print the values if you want
3- store this point for later to be able to close the polygon r0,q0
4- P=0
5- loop through the rest of the points
______read coordinates r1,q1
______exit if EOF
______print the values if you want
______calc distance D between r,q and r1,q1 as you do already
______print if you want
______make P=P+D
______make r=r1 and q=q1 so as to make the previous point become the current point
6- Now set r1=r0 and q1=q0 which makes the current point become the very first point and thus closing the polygon
7- calculate D as before between r,q and r1,q1 and make P=P+D
8- print P


That is it.

The above assumes that the data in the file does not contain a repetition of the first point as the last point to close the polygon.

If it does then you do no need to store the first point and you do not need steps 6 and 7 since the loop will read the last point which will be the first point and the loop would have already calculated the final side.

I hope that helps.