Exemplo n.º 1
0
double          tore_equation(t_ray *ray, t_obj *obj)
{
  double        result[4];
  double        n[11];
  int           nbroots;
  t_tor         tor;

  nbroots = 0;
  init_tab(result, 0, 4);
  n[5]= ray->v[0] * ray->v[0] + ray->v[1] * ray->v[1] + ray->v[2] * ray->v[2];
  n[6] = 2.0 * (ray->v[0] * ray->eye[0] + ray->v[1]
		* ray->eye[1] + ray->v[2] * ray->eye[2]);
  n[7] = ray->eye[0] * ray->eye[0] + ray->eye[1] * ray->eye[1] + ray->eye[2] *
    ray->eye[2] + obj->rayon * obj->rayon - pow((obj->angle / PI * 180), 2);
  n[8] = 4.0 * obj->rayon * obj->rayon;
  n[9] = n[8] * (ray->v[0] * ray->v[0] + ray->v[2] * ray->v[2]);
  n[10] = n[8] * 2 * (ray->v[0] * ray->eye[0] + ray->v[2] * ray->eye[2]);
  n[11] = n[8] * (ray->eye[0] * ray->eye[0] + ray->eye[2] * ray->eye[2]);
  n[0] = n[5] * n[5];
  n[1] = 2.0 * n[5] * n[6];
  n[2] = 2.0 * n[5] * n[7] + n[6] * n[6] - n[9];
  n[3] = 2.0 * n[6] * n[7] - n[10];
  n[4] = n[7] * n[7] - n[11];
  fill_tor_struct_normal(&tor, n);
  nbroots = solve_quartic(result, &tor);
  return (mini_k(result, nbroots));
}
Exemplo n.º 2
0
int Solve_Polynomial(int n, DBL *c0, DBL *r, int sturm, DBL epsilon)
{
  int roots, i;
  DBL *c;

  Increase_Counter(stats[Polynomials_Tested]);

  roots = 0;

  /*
   * Determine the "real" order of the polynomial, i.e.
   * eliminate small leading coefficients.
   */

  i = 0;

  while ((fabs(c0[i]) < SMALL_ENOUGH) && (i < n))
  {
    i++;
  }

  n -= i;

  c = &c0[i];

  switch (n)
  {
    case 0:

      break;

    case 1:

      /* Solve linear polynomial. */

      if (c[0] != 0.0)
      {
        r[roots++] = -c[1] / c[0];
      }

      break;

    case 2:

      /* Solve quadratic polynomial. */

      roots = solve_quadratic(c, r);

      break;

    case 3:

      /* Root elimination? */

      if (epsilon > 0.0)
      {
        if ((c[2] != 0.0) && (fabs(c[3]/c[2]) < epsilon))
        {
          Increase_Counter(stats[Roots_Eliminated]);

          roots = solve_quadratic(c, r);

          break;
        }
      }

      /* Solve cubic polynomial. */

      if (sturm)
      {
        roots = polysolve(3, c, r);
      }
      else
      {
        roots = solve_cubic(c, r);
      }

      break;

    case 4:

      /* Root elimination? */

      if (epsilon > 0.0)
      {
        if ((c[3] != 0.0) && (fabs(c[4]/c[3]) < epsilon))
        {
          Increase_Counter(stats[Roots_Eliminated]);

          if (sturm)
          {
            roots = polysolve(3, c, r);
          }
          else
          {
            roots = solve_cubic(c, r);
          }

          break;
        }
      }

      /* Test for difficult coeffs. */

      if (difficult_coeffs(4, c))
      {
        sturm = true;
      }

      /* Solve quartic polynomial. */

      if (sturm)
      {
        roots = polysolve(4, c, r);
      }
      else
      {
        roots = solve_quartic(c, r);
      }

      break;

    default:

      if (epsilon > 0.0)
      {
        if ((c[n-1] != 0.0) && (fabs(c[n]/c[n-1]) < epsilon))
        {
          Increase_Counter(stats[Roots_Eliminated]);

          roots = polysolve(n-1, c, r);
        }
      }

      /* Solve n-th order polynomial. */

      roots = polysolve(n, c, r);

      break;
  }

  return(roots);
}