My teacher asks let me design a program to find the area of the ellipse.We can not use the pi and any other libraries.
The precision and xy-axis are given.
The problem is when I enter the precision is 0.0001 and xy is 1 1, it works.Btw when I enter 0.0000001, it does not work.
In the above picture, by looking at whether the center of a small grid is within the circle or not, we have counted that 20 small squares should be considered for the area of the circle. Therefore, the estimated area of this circle is 4*20*0.2^2
I know what you are trying to do, but you've got too many squares (and you might run into floating-point round-off errors as well). 20 squares is fine. 100000000000000 squares is not.
Use numerical integration: trapezium rule, mid-ordinate rule or Simpson's rule.
if you want high precision squares using this method you can probably break it down like a fractal, where you use a large square in the middle of the ellipse and as you move toward the edges you cut your squares into 4x so when you reach the edge you have a lot of them and in the middle you have <10 or so...
it will run a little faster if you change pow to x*x .. pow is notably slower for large numbers of calls.
also remove that if statement:
say this:
squares += (int)((i*i / powa + j*j / powb <= 1))
theres only so much you can wring out of brute force, though.
also, make SURE you COMPILE with MAX SPEED optimize ON.