fork download
  1. #include <bits/stdc++.h>
  2.  
  3. #define FO(i,a,b) for (int i = (a); i < (b); i++)
  4. #define sz(v) int(v.size())
  5.  
  6. #define N 100000 // Num Points, each 1 Kg spaced equally on circle of radius 1 about origin
  7. #define G 1 // We only care about direction of force so let G = 1 should be fine
  8.  
  9. double tests[] = {0,0.5,0.8,0.9,0.99,1.1,1.2,1.3,2,3,4};
  10.  
  11. using namespace std;
  12.  
  13. double y[N], x[N];
  14.  
  15. int main() {
  16. FO(i,0,N) y[i] = sin(i*2*M_PI/N), x[i] = cos(i*2*M_PI/N);
  17. FO(Z,0,sizeof(tests)/sizeof(double)) {
  18. double py = tests[Z];
  19. // calculate direction of gravitational field at (0,py)
  20. printf("FIELD from (0,%lf)\n", py);
  21. double fy = 0;
  22. FO(i,0,N) {
  23. if (abs(y[i]-py) < 1e-6) continue;
  24.  
  25. double r2 = pow(y[i]-py,2)+pow(x[i],2);
  26. double F = G / r2;
  27. double dy = abs(y[i]-py);
  28. double DY = F * dy / sqrt(r2);
  29. if (y[i] < py) fy -= DY;
  30. else fy += DY;
  31. }
  32. if (fy > 1e-6) printf("UP\n");
  33. else if (fy < -1e-6) printf("DOWN\n");
  34. else printf("NO NET FORCE\n");
  35. printf("%lf\n", fy);
  36. }
  37. }
  38.  
  39.  
Success #stdin #stdout 0.1s 4856KB
stdin
Standard input is empty
stdout
FIELD from (0,0.000000)
NO NET FORCE
-0.000000
FIELD from (0,0.500000)
UP
34487.720615
FIELD from (0,0.800000)
UP
123354.131333
FIELD from (0,0.900000)
UP
274897.896540
FIELD from (0,0.990000)
UP
3107579.097677
FIELD from (0,1.100000)
DOWN
-351745.140272
FIELD from (0,1.200000)
DOWN
-180130.010258
FIELD from (0,1.300000)
DOWN
-120211.142783
FIELD from (0,2.000000)
DOWN
-31140.515256
FIELD from (0,3.000000)
DOWN
-12145.187191
FIELD from (0,4.000000)
DOWN
-6561.246376