Problem p01134 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;

#define INF 1000000000
#define EPS 1e-10

double a[100], b[100], c[100], d[100];

#define EQ(x, y) fabs((x) - (y)) < (EPS)

inline double dist(double x1, double y1, double x2, double y2) {
  return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

int main() {
  int n;
  int cnt = 1;
  while (cin >> n, n) {
    /*
    if(cnt==49){
      fprintf(stderr, "%d\\n", n);
      rep(i,n){
        scanf("%lf %lf %lf %lf", &a[i], &b[i], &c[i], &d[i]);
        fprintf(stderr, "%lf %lf %lf %lf\\n", a[i], b[i], c[i], d[i]);
      }

    } else {
    */
    rep(i, n) { scanf("%lf %lf %lf %lf", &a[i], &b[i], &c[i], &d[i]); }
    /*
    }cnt++;
    */

    int res = 1;
    rep(i, n) {
      // new line idx=i
      vector<pair<double, double>> cross;
      int adds = 1;
      rep(j, i) {
        // check if i and j have intersection point
        // => not paralell and inner square

        // is paralell ?
        if (EQ((a[i] - c[i]) * (b[j] - d[j]), (a[j] - c[j]) * (b[i] - d[i])))
          continue;

        // calc intersection point
        double nx;
        double ny;
        if (EQ(a[i], c[i])) {
          double dj = (b[j] - d[j]) / (a[j] - c[j]);
          nx = a[i];
          ny = dj * (nx - a[j]) + b[j];
        } else if (EQ(a[j], c[j])) {
          double di = (b[i] - d[i]) / (a[i] - c[i]);
          nx = a[j];
          ny = di * (nx - a[i]) + b[i];
        } else {
          double di = (b[i] - d[i]) / (a[i] - c[i]);
          double dj = (b[j] - d[j]) / (a[j] - c[j]);
          nx = (b[j] - dj * a[j] - (b[i] - di * a[i])) / (di - dj);
          ny = di * (nx - a[i]) + b[i];
          assert(EQ(ny, dj * (nx - a[j]) + b[j]));
        }
        //      dbg(nx);dbg(ny);

        if (fabs(nx) - 100.0 <= -EPS && fabs(ny) - 100.0 <= -EPS) {
          int reps = cross.size();
          bool flg = true;
          rep(k, reps) {
            // new point is already added or not
            // if(EQ(cross[k].first, nx) && EQ(cross[k].second, ny)){
            if (dist(cross[k].first, cross[k].second, nx, ny) < EPS) {
              flg = false;
              break;
            }
          }
          if (flg) {
            cross.pb(mp(nx, ny));
            adds++;
          }
        }

      } // rep  j
      //      dbg(adds);
      res += adds;
    } // rep i

    cout << res << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;

#define INF 1000000000
#define EPS 1e-10

double a[100], b[100], c[100], d[100];

#define EQ(x, y) fabs((x) - (y)) < (EPS)

inline double dist(double x1, double y1, double x2, double y2) {
  return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

int main() {
  int n;
  int cnt = 1;
  while (cin >> n, n) {
    if (cnt == 49) {
      fprintf(stderr, "%d\\n", n);
      rep(i, n) {
        scanf("%lf %lf %lf %lf", &a[i], &b[i], &c[i], &d[i]);
        fprintf(stderr, "%lf %lf %lf %lf\\n", a[i], b[i], c[i], d[i]);
      }

    } else {
      rep(i, n) { scanf("%lf %lf %lf %lf", &a[i], &b[i], &c[i], &d[i]); }
    }
    cnt++;

    int res = 1;
    rep(i, n) {
      // new line idx=i
      vector<pair<double, double>> cross;
      int adds = 1;
      rep(j, i) {
        // check if i and j have intersection point
        // => not paralell and inner square

        // is paralell ?
        if (EQ((a[i] - c[i]) * (b[j] - d[j]), (a[j] - c[j]) * (b[i] - d[i])))
          continue;

        // calc intersection point
        double nx;
        double ny;
        if (EQ(a[i], c[i])) {
          double dj = (b[j] - d[j]) / (a[j] - c[j]);
          nx = a[i];
          ny = dj * (nx - a[j]) + b[j];
        } else if (EQ(a[j], c[j])) {
          double di = (b[i] - d[i]) / (a[i] - c[i]);
          nx = a[j];
          ny = di * (nx - a[i]) + b[i];
        } else {
          double di = (b[i] - d[i]) / (a[i] - c[i]);
          double dj = (b[j] - d[j]) / (a[j] - c[j]);
          nx = (b[j] - dj * a[j] - (b[i] - di * a[i])) / (di - dj);
          ny = di * (nx - a[i]) + b[i];
          assert(EQ(ny, dj * (nx - a[j]) + b[j]));
        }
        //      dbg(nx);dbg(ny);

        if (fabs(nx) - 100.0 <= -EPS && fabs(ny) - 100.0 <= -EPS) {
          int reps = cross.size();
          bool flg = true;
          rep(k, reps) {
            // new point is already added or not
            // if(EQ(cross[k].first, nx) && EQ(cross[k].second, ny)){
            if (dist(cross[k].first, cross[k].second, nx, ny) < EPS) {
              flg = false;
              break;
            }
          }
          if (flg) {
            cross.pb(mp(nx, ny));
            adds++;
          }
        }

      } // rep  j
      //      dbg(adds);
      res += adds;
    } // rep i

    cout << res << endl;
  }
  return 0;
}