Skip to content

Commit

Permalink
Additional models
Browse files Browse the repository at this point in the history
  • Loading branch information
justinchang1124 authored Sep 12, 2020
1 parent 8ff51cf commit c644fb6
Show file tree
Hide file tree
Showing 6 changed files with 384 additions and 0 deletions.
67 changes: 67 additions & 0 deletions AxialStar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import java.awt.Color;

/**
* The original AxialStar model.
*
* @author Justin C
*/
public class AxialStar extends AnimatedModel
{
public static final Color[] colorsAxial =
{ Color.green, Color.red, Color.blue, Color.magenta, Color.yellow, Color.orange };

/**
* Constructs an AxialStar.
*
* @param d
* 6d is the length from the center of the star to one of its tips.
*/
public AxialStar(Vec3 o, boolean s, double d)
{
super(o, s, 20, 128);

// length of a star point, from center to tip
double l = 8 * d;
// width of a star point, from center to edge
double w = 2 * d;
// gap between parts of a star point
double g = d / 10;

double[][] coords = {
{ 0, 0, +l, +d, +g, +w, +g, +d, +w },
{ 0, 0, -l, +d, +g, -w, +g, +d, -w },
{ 0, +l, 0, +d, +w, +g, +g, +w, +d },
{ 0, -l, 0, +d, -w, +g, +g, -w, +d },
{ +l, 0, 0, +w, +d, +g, +w, +g, +d },
{ -l, 0, 0, -w, +d, +g, -w, +g, +d },
{ 0, 0, +l, -d, -g, +w, -g, -d, +w },
{ 0, 0, -l, -d, -g, -w, -g, -d, -w },
{ 0, +l, 0, -d, +w, -g, -g, +w, -d },
{ 0, -l, 0, -d, -w, -g, -g, -w, -d },
{ +l, 0, 0, +w, -d, -g, +w, -g, -d },
{ -l, 0, 0, -w, -d, -g, -w, -g, -d },
{ 0, 0, +l, +d, -g, +w, +g, -d, +w },
{ 0, 0, -l, +d, -g, -w, +g, -d, -w },
{ 0, +l, 0, +d, +w, -g, +g, +w, -d },
{ 0, -l, 0, +d, -w, -g, +g, -w, -d },
{ +l, 0, 0, +w, +d, -g, +w, +g, -d },
{ -l, 0, 0, -w, +d, -g, -w, +g, -d },
{ 0, 0, +l, -d, +g, +w, -g, +d, +w },
{ 0, 0, -l, -d, +g, -w, -g, +d, -w },
{ 0, +l, 0, -d, +w, +g, -g, +w, +d },
{ 0, -l, 0, -d, -w, +g, -g, -w, +d },
{ +l, 0, 0, +w, -d, +g, +w, -g, +d },
{ -l, 0, 0, -w, -d, +g, -w, -g, +d }
};

for (int i = 0; i < 24; i++)
tris.add(new Triangle(coords[i], colorsAxial[i % 6]));
}

public void tick() {
super.tick();
rX = Rotation.rX;
rY = Rotation.rY;
rZ = Rotation.rZ;
}
}
61 changes: 61 additions & 0 deletions Box.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import java.awt.Color;

/**
* A Model that represents a standard box with six faces.
*
* @author Justin Chang
*/
public class Box extends Model
{
/**
* Constructs a Box.
*
* @param xL
* The x-axis length of the box.
* @param yL
* The y-axis length of the box.
* @param zL
* The z-axis length of the box.
* @param c
* An array of colors used for each face of the box.
* The order of face coloring is x+, y+, z+, x-, y-, z-.
*/
public Box(Vec3 o, boolean sB,
double xL, double yL, double zL, Color[] c)
{
super(o, sB);

if (c.length != 6)
throw new IllegalArgumentException("A box must be created with 6 colors!");

if (xL < 0 || yL < 0 || zL < 0)
throw new IllegalArgumentException("A dimension of this box is negative!");

double[] orient = { xL, yL, zL };
Vec3[] v = {
new Vec3(+xL / 2, 0, 0), new Vec3(0, +yL / 2, 0), new Vec3(0, 0, +zL / 2),
new Vec3(-xL / 2, 0, 0), new Vec3(0, -yL / 2, 0), new Vec3(0, 0, -zL / 2) };

// Constructs all faces based on the positions of their centers
// (as in Vec3[] v).
for (int i = 0; i < v.length; i++)
append(new Rectangle(v[i], sB,
orient[(i + 1) % 3], orient[(i + 2) % 3], c[i], (i + 1) % 3));
}

/**
* Constructs the default version of a box: a cube.
*
* @param d
* The length of a side of the cube.
*/
public Box(Vec3 o, boolean sB, double d, Color[] c)
{
this(o, sB, d, d, d, c);
}

public void tick()
{

}
}
54 changes: 54 additions & 0 deletions Pinwheel.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import java.awt.Color;

/**
* A demonstration of how the Painter's Algorithm fails in 3D rendering, but the
* z-Buffer method does not, through a pinwheel structure.
*
* @author Justin Chang
*/
public class Pinwheel extends AnimatedModel
{
/**
* Constructs a PainterStar.
*
* @param rBase
* The radius of the base of the pinwheel.
* @param rTip
* The radius of the tips of the pinwheel.
* @param height
* The height of this pinwheel.
* @param edgeCount
* The number of edges on this pinwheel.
* @param disp
* The displacement of each tip from the starting Vec3 of each triangle.
* @param colors
* The list of colors that this pinwheel will be colored with, in alternation.
*/
public Pinwheel(Vec3 o, boolean s,
double rBase, double rTip, double height, int edgeCount, double disp, Color[] colors)
{
super(o, s, 4, 255);

// Precomputes the constant for polar coordinates.
double increment = 2 * Math.PI / edgeCount;

for (int i = 0; i < edgeCount; i++)
{
double sin1 = rBase * Math.sin(i * increment);
double cos1 = rBase * Math.cos(i * increment);
double sin2 = rBase * Math.sin((i + 0.5) * increment);
double cos2 = rBase * Math.cos((i + 0.5) * increment);
double sin3 = rTip * Math.sin((i + disp) * increment);
double cos3 = rTip * Math.cos((i + disp) * increment);

tris.add(new Triangle(
new double[] { cos1, sin1, 0, cos2, sin2, 0, cos3, sin3, height },
colors[i % colors.length]));
}
}

public void tick() {
super.tick();
rZ = -Rotation.rZ;
}
}
70 changes: 70 additions & 0 deletions Rectangle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import java.awt.Color;

/**
* Represents a rectangle aligned with the axes.
*
* @author Justin C
*/
public class Rectangle extends Model
{
/**
* Constructs a Rectangle.
*
* @param d1
* The length of one side of the Rectangle.
*
* @param d2
* The length of another adjacent side of the Rectangle.
*
* @param orientation
* orientation = 0: d1 is along x-axis, d2 is along y-axis.
* orientation = 1: d1 is along y-axis, d2 is along z-axis.
* orientation = 2: d1 is along z-axis, d2 is along x-axis.
*
* orientation = 0: parallel to the XY plane.
* orientation = 1: parallel to the YZ plane.
* orientation = 2: parallel to the ZX plane.
*/
public Rectangle(Vec3 o, boolean sB,
double d1, double d2, Color color, int orientation)
{
super(o, sB);

if (d1 < 0 || d2 < 0 || orientation < 0 || orientation > 2)
throw new IllegalArgumentException("A dimension of this rectangle is negative!");

double[][] coords = null;

if (orientation == 0)
coords = new double[][] {
{ -d1 / 2, -d2 / 2, 0, +d1 / 2, -d2 / 2, 0, -d1 / 2, +d2 / 2, 0 },
{ +d1 / 2, +d2 / 2, 0, +d1 / 2, -d2 / 2, 0, -d1 / 2, +d2 / 2, 0 } };

if (orientation == 1)
coords = new double[][] {
{ 0, -d1 / 2, -d2 / 2, 0, +d1 / 2, -d2 / 2, 0, -d1 / 2, +d2 / 2 },
{ 0, +d1 / 2, +d2 / 2, 0, +d1 / 2, -d2 / 2, 0, -d1 / 2, +d2 / 2 } };

if (orientation == 2)
coords = new double[][] {
{ -d2 / 2, 0, -d1 / 2, +d2 / 2, 0, -d1 / 2, -d2 / 2, 0, +d1 / 2 },
{ +d2 / 2, 0, +d1 / 2, +d2 / 2, 0, -d1 / 2, -d2 / 2, 0, +d1 / 2 } };

for (int i = 0; i < coords.length; i++)
tris.add(new Triangle(coords[i], color));
}

/**
* Constructs a square of length d.
*/
public Rectangle(Vec3 o, boolean sB,
double d, Color color, int orientation)
{
this(o, sB, d, d, color, orientation);
}

public void tick()
{

}
}
26 changes: 26 additions & 0 deletions RubiksCube.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.awt.Color;

/**
* A single standard Rubik's Cube.
*
* @author Justin C
*/
public class RubiksCube extends Box
{
/**
* The list of colors for the faces of the cube.
*/
public static final Color[] colorsRubiks =
{ Color.red, Color.yellow, Color.blue, Color.orange, Color.white, Color.green };

/**
* Constructs a RubiksCube.
*
* @param d
* The length of an edge of the cube.
*/
public RubiksCube(Vec3 o, double d)
{
super(o, true, d, colorsRubiks);
}
}
106 changes: 106 additions & 0 deletions Sphere.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import java.awt.Color;
import java.util.ArrayList;

/**
* A Model that represents a spherical configuration.
*
* @author Justin C
*/
public class Sphere extends AnimatedModel
{
/**
* The radius of the sphere.
*/
public final double r;

/**
* The number of inflation iterations performed.
*/
private int inflate;

/**
* Constructs a Sphere. rB and sB are binding constants and inflate is the
* number of times that the sphere is inflated.
*/
public Sphere(Vec3 o, boolean sB,
double radius, Color color, int inflate)
{
super(o, sB, 1, 128);

if (radius < 0)
throw new IllegalArgumentException("Sphere cannot have a negative radius!");

r = radius;

double[][] coords = {
{ +r, +r, +r, -r, -r, +r, -r, +r, -r },
{ +r, +r, +r, -r, -r, +r, +r, -r, -r },
{ -r, +r, -r, +r, -r, -r, +r, +r, +r },
{ -r, +r, -r, +r, -r, -r, -r, -r, +r } };

for (int i = 0; i < coords.length; i++)
tris.add(new Triangle(coords[i], color));

inflate(inflate);
}

/**
* Inflates the current tris k times.
*/
public void inflate(int k)
{
for (int i = 0; i < k; i++)
inflate();
}

public void tick()
{
super.tick();
rY = Rotation.rY;
}

/**
* Inflates the current tris once.
*/
public void inflate()
{
ArrayList<Triangle> newTris = new ArrayList<Triangle>();

for (Triangle t : tris)
{
// The midpoints of the sides.
Vec3 m1 = new Vec3(
t.v1.v[0][0] + t.v2.v[0][0], t.v1.v[1][0] + t.v2.v[1][0], t.v1.v[2][0] + t.v2.v[2][0]);
Vec3 m2 = new Vec3(
t.v2.v[0][0] + t.v3.v[0][0], t.v2.v[1][0] + t.v3.v[1][0], t.v2.v[2][0] + t.v3.v[2][0]);
Vec3 m3 = new Vec3(
t.v1.v[0][0] + t.v3.v[0][0], t.v1.v[1][0] + t.v3.v[1][0], t.v1.v[2][0] + t.v3.v[2][0]);

// Scales them so that all midpoints are "r" away from the center of the sphere.
m1 = m1.unit().dot(r);
m2 = m2.unit().dot(r);
m3 = m3.unit().dot(r);
t.v1 = t.v1.unit().dot(r);
t.v2 = t.v2.unit().dot(r);
t.v3 = t.v3.unit().dot(r);

// Adds these triangles to the "inflated" tris.
newTris.add(new Triangle(t.v1, m1, m3, t.color, false));
newTris.add(new Triangle(t.v2, m1, m2, t.color, false));
newTris.add(new Triangle(t.v3, m2, m3, t.color, false));
newTris.add(new Triangle(m1, m2, m3, t.color, false));
}

// Reassigns the tris.
tris = newTris;
inflate++;
}

/**
* Gets the number of inflations performed.
*/
public int getInflate()
{
return inflate;
}
}

0 comments on commit c644fb6

Please sign in to comment.