Exemple #1
0
  /** @see Surface#getNormal() */
  @Override
  public Vector getNormal(Point point) {
    // TODO: return vector representing this surface's normal at this point

    Vector ab = b.sub(a);
    Vector ac = c.sub(a);

    return ab.cross(ac).normalize();
  }
Exemple #2
0
  /** @see Surface#getIntersection() */
  @Override
  public double[] getIntersection(Ray ray) {
    // TODO: return t values at which this ray intersects this surface
    Vector ab = b.sub(a);
    Vector ac = c.sub(a);

    Vector acDir = ray.getDirection().cross(ac);

    double div = acDir.dot(ab);

    Vector distance = ray.getOrigin().sub(a);

    // get first barycentric coordinate
    double bary1 = distance.dot(acDir) / div;
    if (bary1 < 0 || bary1 > 1) {
      return new double[0];
    }

    // get second barycentric coordinate
    Vector abDistCross = distance.cross(ab);
    double bary2 = ray.getDirection().dot(abDistCross) / div;
    if (bary2 < 0 || bary1 + bary2 > 1) {
      return new double[0];
    }

    // calculate t
    double t = ac.dot(abDistCross) / div;
    double[] ts = {t};
    return ts;
  }