Skip to content

Commit

Permalink
Merge pull request #6 from abrahammurciano/main
Browse files Browse the repository at this point in the history
update ex2
  • Loading branch information
abrahammurciano authored Apr 20, 2021
2 parents 13079c3 + c20aab2 commit c0e0fa6
Show file tree
Hide file tree
Showing 21 changed files with 152 additions and 143 deletions.
7 changes: 3 additions & 4 deletions src/main/geometries/Cylinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class Cylinder implements Geometry {
/**
* This constructs a Cylinder.
*
* @param ray The ray that makes up the center of the Cylinder.
* @param ray The ray that makes up the center of the Cylinder.
* @param radius A positive double that represents the radius.
* @param height A positive double that represents the height of the Cylinder.
* @throws IllegalArgumentException if the radius is zero or the height is not positive.
Expand All @@ -32,7 +32,7 @@ public Cylinder(Ray ray, double radius, double height) {
throw new IllegalArgumentException("Error: Height must be a positive number.");
}
this.height = height;
base = new Plane(ray.source, direction().reversed());
base = new Plane(ray.source, direction());
lid = new Plane(ray.source.add(direction().scale(height)), direction());
}

Expand All @@ -55,8 +55,7 @@ public NormalizedVector direction() {
public NormalizedVector normal(Point p) {
if (lid.contains(p) || base.contains(p)) {
return tube.direction();
}
else {
} else {
return tube.normal(p);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/geometries/Geometry.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
*/
public interface Geometry {
/**
* Calculates the normal to the {@link Geometry} at the given {@link Point}. If the given {@link Point} is not on the
* surface of the {@link Geometry} the resulting behaviour is undefined.
* Calculates the normal to the {@link Geometry} at the given {@link Point}. If the given {@link Point} is not on
* the surface of the {@link Geometry} the resulting behaviour is undefined.
*
* @param p The {@link Point} at which to calculate the normal.
* @return A {@link NormalizedVector} perpendicular to the surface of the shape at the given {@link Point}.
Expand Down
6 changes: 3 additions & 3 deletions src/main/geometries/Plane.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public class Plane implements Geometry {
private NormalizedVector normal;

/**
* This constructor accepts a point on the plane and a vector perpendicular to the plane. {@link #normal} will return a
* normalized vector in the same direction as the given {@link Vector}.
* This constructor accepts a point on the plane and a vector perpendicular to the plane. {@link #normal} will
* return a normalized vector in the same direction as the given {@link Vector}.
*
* @param point A point on the plane.
* @param point A point on the plane.
* @param normal A vector perpendicular to the plane.
*/
public Plane(Point point, Vector normal) {
Expand Down
11 changes: 5 additions & 6 deletions src/main/geometries/Polygon.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ public class Polygon implements Geometry {
* This constructor accepts a list of the vertices of the polygon.
*
* @param vertices A list of the vertices of the polygon, in order.
* @throws IllegalArgumentException if there are less than three significant vertices, any of the vertices are not on
* the same plane as the rest, the vertices are out of order and thus form a non-convex
* polygon, consecutive vertices are repeated, or the last point is equal to the first
* point.
* @throws IllegalArgumentException if there are less than three significant vertices, any of the vertices are not
* on the same plane as the rest, the vertices are out of order and thus form a non-convex polygon,
* consecutive vertices are repeated, or the last point is equal to the first point.
*/
public Polygon(Point... vertices) {
int size = vertices.length;
Expand Down Expand Up @@ -79,8 +78,8 @@ public NormalizedVector normal(Point p) {
}

/**
* Calculates a % b but for negative inputs will still give a result between 0 and b (similar to how Python implements
* mod).
* Calculates a % b but for negative inputs will still give a result between 0 and b (similar to how Python
* implements mod).
*
* @param a The dividend
* @param b The divisor
Expand Down
7 changes: 4 additions & 3 deletions src/main/geometries/Sphere.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public class Sphere implements Geometry {
* Constructs a sphere from a given center point and a radius.
*
* @param center A {@link Point} representing the center of the circle.
* @param radius A positive number representing the radius. If given a negative number will be assumed to be positive.
* @param radius A positive number representing the radius. If given a negative number will be assumed to be
* positive.
* @throws IllegalArgumentException if the radius is zero.
*/
public Sphere(Point center, double radius) {
Expand All @@ -33,8 +34,8 @@ public Sphere(Point center, double radius) {
/**
* Returns normal to the sphere at given point.
*
* @param p The point to get normal at. This point is assumed to be on the surface of the circle. If it's not then it is
* undefined behavior.
* @param p The point to get normal at. This point is assumed to be on the surface of the circle. If it's not then
* it is undefined behavior.
* @throws ZeroVectorException if p is the center of the sphere.
*/
@Override
Expand Down
6 changes: 3 additions & 3 deletions src/main/geometries/Tube.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class Tube implements Geometry {
/**
* Constructs a {@link Tube} with the source at the same source and direction as the given axis {@link Ray}.
*
* @param axis The {@link Ray} from which to get the source and direction.
* @param axis The {@link Ray} from which to get the source and direction.
* @param radius The distance from the axis to the surface.
* @throws IllegalArgumentException if the radius is zero.
*/
Expand All @@ -42,8 +42,8 @@ public NormalizedVector direction() {
}

/**
* This function returns the normal to the tube at the given point. If the point doesn't lie on the surface of the tube,
* the behavior is undefined.
* This function returns the normal to the tube at the given point. If the point doesn't lie on the surface of the
* tube, the behavior is undefined.
*
* @param p The {@link Point} to get the normal at.
* @return The normalized normal {@link Vector}
Expand Down
8 changes: 4 additions & 4 deletions src/main/primitives/NormalizedVector.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ public NormalizedVector(Vector v) {
}

/**
* This private constructor does not normalize the vector. Make sure that the head is on the unit sphere before calling
* this.
* This private constructor does not normalize the vector. Make sure that the head is on the unit sphere before
* calling this.
*
* @param head The head of the vector. It will be stored exactly as passed so make sure it makes a vector with length
* equal to one.
* @param head The head of the vector. It will be stored exactly as passed so make sure it makes a vector with
* length equal to one.
* @throws ZeroVectorException if the given {@link Point} is the origin.
*/
private NormalizedVector(Point head) {
Expand Down
12 changes: 6 additions & 6 deletions src/main/primitives/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public Point(double x, double y, double z) {
}

/**
* Creates a new {@link Point} which is a transformation of this {@link Point} by applying the given transformation to
* each of the coordinates.
* Creates a new {@link Point} which is a transformation of this {@link Point} by applying the given transformation
* to each of the coordinates.
*
* @param transformation A function which receives two coordinates and returns another coordinate.
* @param aux An auxiliary {@link Point} whose corresponding coordinate may (or may not) be used in the
* transformation function in order to calculate each of the new coordinates.
* @param aux An auxiliary {@link Point} whose corresponding coordinate may (or may not) be used in the
* transformation function in order to calculate each of the new coordinates.
* @return The {@link Point} made up of applying the transformation to each of the three coordinates.
*/
public Point transform(DoubleBinaryOperator transformation, Point aux) {
Expand All @@ -60,8 +60,8 @@ public Point transform(DoubleBinaryOperator transformation, Point aux) {
}

/**
* Similar to {@link #transform(DoubleBinaryOperator, Point)} but does not require an auxiliary {@link Point}, since the
* transformation when called in this way does not depend on a second coordinate.
* Similar to {@link #transform(DoubleBinaryOperator, Point)} but does not require an auxiliary {@link Point}, since
* the transformation when called in this way does not depend on a second coordinate.
*
* @param transformation A function which receives a single coordinate and returns another coordinate.
* @return The {@link Point} made up of applying the transformation to each of the three coordinates.
Expand Down
2 changes: 1 addition & 1 deletion src/main/primitives/Ray.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class Ray {
/**
* Constructs a {@link Ray} from a source {@link Point} and a direction {@link Vector}.
*
* @param source The {@link Point} at which the {@link Ray} starts.
* @param source The {@link Point} at which the {@link Ray} starts.
* @param direction The {@link Vector} in which the {@link Ray} is directed.
*/
public Ray(Point source, Vector direction) {
Expand Down
53 changes: 20 additions & 33 deletions src/main/primitives/Vector.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import java.util.function.DoubleUnaryOperator;

/**
* The {@link Vector} class represents a {@link Vector} with it's base at the
* origin and it's head at the {@link Point} 'head'.
* The {@link Vector} class represents a {@link Vector} with it's base at the origin and it's head at the {@link Point}
* 'head'.
*
* @author Abraham Murciano
* @author Eli Levin
Expand All @@ -29,11 +29,9 @@ public Vector(double x, double y, double z) {
}

/**
* This constructor accepts a {@link Point} and returns the appropriate
* {@link Vector}
* This constructor accepts a {@link Point} and returns the appropriate {@link Vector}
*
* @param head The {@link Point} which this {@link Vector} would point to if its
* base was at the origin.
* @param head The {@link Point} which this {@link Vector} would point to if its base was at the origin.
* @throws ZeroVectorException if this {@link Vector} is the zero vector.
*/
public Vector(Point head) {
Expand All @@ -44,32 +42,25 @@ public Vector(Point head) {
}

/**
* Creates a new {@link Vector} which is a transformation of this {@link Vector}
* by applying the given transformation to each of the coordinates.
* Creates a new {@link Vector} which is a transformation of this {@link Vector} by applying the given
* transformation to each of the coordinates.
*
* @param transformation A function which receives two doubles and returns
* another double.
* @param aux An auxiliary {@link Vector} whose corresponding
* coordinate may (or may not) be used in the
* transformation function in order to calculate each of
* the new coordinates.
* @return The {@link Vector} made up of applying the transformation to each of
* the three coordinates.
* @param transformation A function which receives two doubles and returns another double.
* @param aux An auxiliary {@link Vector} whose corresponding coordinate may (or may not) be used in the
* transformation function in order to calculate each of the new coordinates.
* @return The {@link Vector} made up of applying the transformation to each of the three coordinates.
* @throws ZeroVectorException if the transformation results in the zero vector.
*/
public Vector transform(DoubleBinaryOperator transformation, Vector aux) {
return new Vector(head.transform(transformation, aux.head));
}

/**
* Similar to {@link #transform(DoubleBinaryOperator, Vector)} but does not
* require an auxiliary {@link Vector}, since the transformation when called in
* this way does not depend on a second coordinate.
* Similar to {@link #transform(DoubleBinaryOperator, Vector)} but does not require an auxiliary {@link Vector},
* since the transformation when called in this way does not depend on a second coordinate.
*
* @param transformation A function which receives a dingle double and returns
* another double.
* @return The {@link Vector} made up of applying the transformation to each of
* the three coordinates.
* @param transformation A function which receives a dingle double and returns another double.
* @return The {@link Vector} made up of applying the transformation to each of the three coordinates.
* @throws ZeroVectorException if the transformation results in the zero vector.
*/
public Vector transform(DoubleUnaryOperator transformation) {
Expand All @@ -91,17 +82,15 @@ public Vector add(Vector v) {
* Subtracts two {@link Vector}s and returns a new {@link Vector}.
*
* @param vector The {@link Vector} to be subtracted from this {@link Vector}.
* @return The sum of this {@link Vector} and the negation of the given
* {@link Vector}.
* @return The sum of this {@link Vector} and the negation of the given {@link Vector}.
* @throws ZeroVectorException if a {@link Vector} is subtracted from itself.
*/
public Vector subtract(Vector vector) {
return add(vector.reversed());
}

/**
* Constructs a new {@link Vector} which is a scalar multiplication of this
* {@link Vector} by a scalar.
* Constructs a new {@link Vector} which is a scalar multiplication of this {@link Vector} by a scalar.
*
* @param factor The scalar by which to multiply this {@link Vector}
* @return New scaled {@link Vector}
Expand All @@ -124,8 +113,7 @@ public Vector reversed() {
* Calculates the cross product of two {@link Vector}s.
*
* @param v The {@link Vector} by which to multiply this {@link Vector}
* @return The resulting {@link Vector} which is the cross product of the two
* {@link Vector}s
* @return The resulting {@link Vector} which is the cross product of the two {@link Vector}s
* @throws ZeroVectorException if the result vector is the zero vector.
*/
public Vector cross(Vector v) {
Expand Down Expand Up @@ -167,8 +155,7 @@ public double squareLength() {
}

/**
* Creates a new {@link Vector} with the same direction as this one but with a
* magnitude of one.
* Creates a new {@link Vector} with the same direction as this one but with a magnitude of one.
*
* @return new {@link Vector}
*/
Expand All @@ -177,8 +164,8 @@ public NormalizedVector normalized() {
}

/**
* Calculates the angle in radians between this vector and the given vector. The
* angle is normalized between zero and Pi.
* Calculates the angle in radians between this vector and the given vector. The angle is normalized between zero
* and Pi.
*
* @param v The other vector to be used to calculate the angle.
* @return The angle in radians between the vectors between zero and Pi.
Expand Down
3 changes: 1 addition & 2 deletions src/main/util/DoubleCompare.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
* @author Eli Levin
*/
public class DoubleCompare {
private DoubleCompare() {
}
private DoubleCompare() {}

/**
* Check if two doubles are approximately equal.
Expand Down
37 changes: 20 additions & 17 deletions src/test/geometries/CylinderTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,47 @@
* @author Eli Levin
*/
public class CylinderTests {
public final Ray ray = new Ray(new Point(3,2,1), new Vector(6,5,4));
public final Ray ray = new Ray(new Point(3, 2, 1), new Vector(6, 5, 4));
public final Cylinder base = new Cylinder(ray, 5, 10);

@Test
@Test
public void normal() {
NormalizedVector calc, actual1, actual2;

// test on round side
calc = base.normal(new Point(30.973500981126143, 23, 13.639748528310783));
actual1 = new NormalizedVector(2,0,-3);
actual1 = new NormalizedVector(2, 0, -3);
Assert.assertTrue("Normalized vectors should be equal", NormalCompare.eq(calc, actual1));

// test on flat side side
calc = base.normal(new Point(7.79639313525896, 11.780511727620528, 3.517181606066203));
actual1 = new NormalizedVector(6,5,4);
calc = base.normal(new Point(7.79639313525896, 11.780511727620528, 3.517181606066203));
actual1 = new NormalizedVector(6, 5, 4);
Assert.assertTrue("Normalized vectors should be equal", NormalCompare.eq(calc, actual1));

// test on corner
calc = base.normal(new Point(13.920117492216907, -0.46693698629536406, 9.64090596302415));
actual1 = new NormalizedVector(6,5,4);
actual2 = new NormalizedVector(1,-2,1);
actual1 = new NormalizedVector(6, 5, 4);
actual2 = new NormalizedVector(1, -2, 1);
// in practice always ends up being normal to the flat side
Assert.assertTrue("Vectors should be equal", NormalCompare.eq(calc, actual1) || NormalCompare.eq(calc, actual2));
String notEqErrMsg = "Vectors should be equal";
Assert.assertTrue(notEqErrMsg,
NormalCompare.eq(calc, actual1) || NormalCompare.eq(calc, actual2));

// base side
calc = base.normal(new Point(5.182178902359924, 6.364357804719848, -7.728715609439696));
actual1 = new NormalizedVector(6,5,4);
actual2 = new NormalizedVector(1,-2,1);
Assert.assertTrue("Vectors should be equal", NormalCompare.eq(calc, actual1) || NormalCompare.eq(calc, actual2));
actual1 = new NormalizedVector(6, 5, 4);
actual2 = new NormalizedVector(1, -2, 1);
Assert.assertTrue(notEqErrMsg,
NormalCompare.eq(calc, actual1) || NormalCompare.eq(calc, actual2));

// center of base
calc = base.normal(new Point(3,2,1));
actual1 = new NormalizedVector(6,5,4);
Assert.assertTrue("Vectors should be equal", NormalCompare.eq(calc, actual1));
calc = base.normal(new Point(3, 2, 1));
actual1 = new NormalizedVector(6, 5, 4);
Assert.assertTrue(notEqErrMsg, NormalCompare.eq(calc, actual1));

// opposite center
calc = base. normal(new Point(9.837634587578275,7.698028822981897,5.558423058385518));
actual1 = new NormalizedVector(6,5,4);
Assert.assertTrue("Vectors should be equal", NormalCompare.eq(calc, actual1));
calc = base.normal(new Point(9.837634587578275, 7.698028822981897, 5.558423058385518));
actual1 = new NormalizedVector(6, 5, 4);
Assert.assertTrue(notEqErrMsg, NormalCompare.eq(calc, actual1));
}
}
4 changes: 2 additions & 2 deletions src/test/geometries/PlaneTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import primitives.NormalizedVector;
import primitives.Point;
import primitives.Vector;
import util.NormalCompare;

/**
* Tests the methods of the Plane class.
Expand Down Expand Up @@ -55,7 +56,6 @@ public void normal() {
Plane plane = new Plane(new Point(0, 0, 0), new Point(2, -1, 0), new Point(1, 1, 0));
NormalizedVector normal = plane.normal(new Point(0, 0, 0));
NormalizedVector expected_normal = new NormalizedVector(0, 0, 1);
Assert.assertTrue("Wrong normal for Polygon.",
normal.equals(expected_normal) || normal.equals(expected_normal.reversed()));
Assert.assertTrue("Wrong normal for Polygon.", NormalCompare.eq(normal, expected_normal));
}
}
4 changes: 2 additions & 2 deletions src/test/geometries/PolygonTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.junit.Test;
import primitives.NormalizedVector;
import primitives.Point;
import util.NormalCompare;

/**
* Tests the methods of the Polygon class.
Expand Down Expand Up @@ -101,7 +102,6 @@ public void normal() {
new Point(-1, -1, -1));
NormalizedVector normal = polygon.normal(new Point(0, 0, 0));
NormalizedVector expected_normal = new NormalizedVector(1, -1, 0);
Assert.assertTrue("Wrong normal for Polygon.",
normal.equals(expected_normal) || normal.equals(expected_normal.reversed()));
Assert.assertTrue("Wrong normal for Polygon.", NormalCompare.eq(normal, expected_normal));
}
}
Loading

0 comments on commit c0e0fa6

Please sign in to comment.