Slope implements Scalar<T> { @Override public T value(final Field<T> field) { if (!new Vertical<>(this.line).resolve(field)) { final Scalar<T>[] coords = this.line.direction().coords(); return field.actual(new Division<T>(coords[1], coords[0])); } throw new IllegalStateException("Line has infinite slope."); } Slope(final Line<T> line);  Slope(final Ray<T> ray);  Slope(final Segment<T> seg); @Override T value(final Field<T> field);  }
@Test public void calculatesSlope() { final Line<Double> line = new RandomLine<>(); final double error = 1.e-6; final Decimal dec = new Decimal(); final Double dirx = line.direction().coords()[0].value(dec); final Double diry = line.direction().coords()[1].value(dec); MatcherAssert.assertThat( new Slope<Double>(line).value(dec), Matchers.closeTo(diry / dirx, error) ); }
@Test public void calculatesRaySlope() { final Line<Double> novertical = new RandomLine<>(); final Ray<Double> ray = new PtDirRay<>( novertical.point(), novertical.direction() ); final double error = 1.e-6; final Decimal dec = new Decimal(); final Double dirx = ray.direction().coords()[0].value(dec); final Double diry = ray.direction().coords()[1].value(dec); MatcherAssert.assertThat( new Slope<Double>(ray).value(dec), Matchers.closeTo(diry / dirx, error) ); }
@Test public void calculatesSegmentSlope() { final Segment<Double> seg = new RandomSegment<>(); final double error = 1.e-6; final Decimal dec = new Decimal(); final Double startx = seg.start().coords()[0].value(dec); final Double starty = seg.start().coords()[1].value(dec); final Double endx = seg.end().coords()[0].value(dec); final Double endy = seg.end().coords()[1].value(dec); MatcherAssert.assertThat( new Slope<Double>(seg).value(dec), Matchers.closeTo((endy - starty) / (endx - startx), error) ); }
@Test public void errorsWhenVerticalLine() { this.thrown.expect(IllegalStateException.class); final Line<Double> line = new VerticalLine<>(); new Slope<>(line).value(new Decimal()); }