Skip to content

Commit

Permalink
Source code for 3D tutorials
Browse files Browse the repository at this point in the history
  • Loading branch information
javatutorials101 committed Jun 4, 2015
1 parent e49d07d commit 02bee62
Show file tree
Hide file tree
Showing 6 changed files with 307 additions and 0 deletions.
61 changes: 61 additions & 0 deletions 3D - Tutorial 13/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

public class Calculator {
static double DrawX = 0, DrawY = 0;
static double CalculatePositionX(double[] ViewFrom, double[] ViewTo, double x, double y, double z)
{
setStuff(ViewFrom, ViewTo, x, y, z);
return DrawX;
}

static double CalculatePositionY(double[] ViewFrom, double[] ViewTo, double x, double y, double z)
{
setStuff(ViewFrom, ViewTo, x, y, z);
return DrawY;
}

static void setStuff(double[] ViewFrom, double[] ViewTo, double x, double y, double z)
{
Vector ViewVector = new Vector(ViewTo[0] - ViewFrom[0], ViewTo[1] - ViewFrom[1], ViewTo[2] - ViewFrom[2]);
Vector DirectionVector = new Vector(1, 1, 1);
Vector PlaneVector1 = ViewVector.CrossProduct(DirectionVector);
Vector PlaneVector2 = ViewVector.CrossProduct(PlaneVector1);

Vector RotationVector = GetRotationVector(ViewFrom, ViewTo);
Vector WeirdVector1 = ViewVector.CrossProduct(RotationVector);
Vector WeirdVector2 = ViewVector.CrossProduct(WeirdVector1);

Vector ViewToPoint = new Vector(x - ViewFrom[0], y - ViewFrom[1], z - ViewFrom[2]);

double t = (ViewVector.x * ViewTo[0] + ViewVector.y*ViewTo[1] + ViewVector.z*ViewTo[2]
- (ViewVector.x * ViewFrom[0] + ViewVector.y*ViewFrom[1] + ViewVector.z*ViewFrom[2]))
/ (ViewVector.x * ViewToPoint.x + ViewVector.y*ViewToPoint.y + ViewVector.z*ViewToPoint.z);

x = ViewFrom[0] + ViewToPoint.x * t;
y = ViewFrom[1] + ViewToPoint.y * t;
z = ViewFrom[2] + ViewToPoint.z * t;

if(t > 0)
{
DrawX = WeirdVector2.x * x + WeirdVector2.y * y + WeirdVector2.z * z;
DrawY = WeirdVector1.x * x + WeirdVector1.y * y + WeirdVector1.z * z;
}
}

static Vector GetRotationVector(double[] ViewFrom, double[] ViewTo)
{
double dx = Math.abs(ViewFrom[0]-ViewTo[0]);
double dy = Math.abs(ViewFrom[1]-ViewTo[1]);
double xRot, yRot;

xRot=dy/(dx+dy);
yRot=dx/(dx+dy);

if(ViewFrom[1]>ViewTo[1])
xRot = -xRot;
if(ViewFrom[0]<ViewTo[0])
yRot = -yRot;

Vector V = new Vector(xRot, yRot, 0);
return V;
}
}
24 changes: 24 additions & 0 deletions 3D - Tutorial 13/DDDTutorial.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;

public class DDDTutorial extends JFrame{

static Dimension ScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
static JFrame F = new DDDTutorial();
Screen ScreenObject = new Screen();

public DDDTutorial()
{
add(ScreenObject);
setUndecorated(true);
setSize(ScreenSize);
setVisible(true);
}

public static void main(String[] args)
{

}
}
61 changes: 61 additions & 0 deletions 3D - Tutorial 13/DPolygon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import java.awt.Color;
import java.awt.Polygon;


public class DPolygon {
Color c;
double[] x, y, z;
int poly = 0;

public DPolygon(double[] x, double[] y, double[] z, Color c)
{
Screen.NumberOf3DPolygons++;
this.x = x;
this.y = y;
this.z = z;
this.c = c;
createPolygon();
}

void createPolygon()
{
poly = Screen.NumberOfPolygons;
Screen.DrawablePolygons[Screen.NumberOfPolygons] = new PolygonObject(new double[]{}, new double[]{}, c);
updatePolygon();
}

void updatePolygon()
{
double dx = - 50 * Calculator.CalculatePositionX(Screen.ViewFrom, Screen.ViewTo, Screen.ViewTo[0], Screen.ViewTo[1], Screen.ViewTo[2]);
double dy = - 50 * Calculator.CalculatePositionY(Screen.ViewFrom, Screen.ViewTo, Screen.ViewTo[0], Screen.ViewTo[1], Screen.ViewTo[2]);
double[] newX = new double[x.length];
double[] newY = new double[x.length];

for(int i = 0; i < x.length; i++)
{
newX[i] = dx + DDDTutorial.ScreenSize.getWidth()/2 + 50 * Calculator.CalculatePositionX(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]);
newY[i] = dy + DDDTutorial.ScreenSize.getHeight()/2 + 50 * Calculator.CalculatePositionY(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]);
}

Screen.DrawablePolygons[poly] = new PolygonObject(newX, newY, c);
Screen.DrawablePolygons[poly].AvgDist = GetDist();
Screen.NumberOfPolygons --;
}

double GetDist()
{
double total = 0;
for(int i = 0; i < x.length; i++)
total += GetDistanceToP(i);
return total / x.length;
}

double GetDistanceToP(int i)
{

return Math.sqrt(
(Screen.ViewFrom[0] - x[i])*(Screen.ViewFrom[0] - x[i]) +
(Screen.ViewFrom[1] - y[i])*(Screen.ViewFrom[1] - y[i]) +
(Screen.ViewFrom[2] - z[i])*(Screen.ViewFrom[2] - z[i]));
}
}
27 changes: 27 additions & 0 deletions 3D - Tutorial 13/PolygonObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;


public class PolygonObject {
Polygon P;
Color c;
double AvgDist = 0;

public PolygonObject(double[] x, double[] y, Color c)
{
Screen.NumberOfPolygons++;
P = new Polygon();
for(int i = 0; i < x.length; i++)
P.addPoint((int)x[i], (int)y[i]);
this.c = c;
}

void drawPolygon(Graphics g)
{
g.setColor(c);
g.fillPolygon(P);
g.setColor(Color.black);
g.drawPolygon(P);
}
}
111 changes: 111 additions & 0 deletions 3D - Tutorial 13/Screen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;

public class Screen extends JPanel implements KeyListener{
double SleepTime = 1000/30, lastRefresh = 0;
static double[] ViewFrom = new double[] {10, 10, 10};
static double[] ViewTo = new double[] {1, 1, 1.5};
static int NumberOfPolygons = 0, NumberOf3DPolygons = 0;
static PolygonObject[] DrawablePolygons = new PolygonObject[100];
static DPolygon[] DPolygons = new DPolygon[100];
int[] NewOrder;

public Screen()
{
addKeyListener(this);
setFocusable(true);
DPolygons[NumberOf3DPolygons] = new DPolygon(new double[]{0, 2, 2, 0}, new double[]{0, 0, 2, 2}, new double[]{0, 0, 0, 0}, Color.gray);
DPolygons[NumberOf3DPolygons] = new DPolygon(new double[]{0, 2, 2, 0}, new double[]{0, 0, 2, 2}, new double[]{3, 3, 3, 3}, Color.gray);
DPolygons[NumberOf3DPolygons] = new DPolygon(new double[]{0, 2, 2, 0}, new double[]{0, 0, 0, 0}, new double[]{0, 0, 3, 3}, Color.gray);
DPolygons[NumberOf3DPolygons] = new DPolygon(new double[]{0, 2, 2, 0}, new double[]{2, 2, 2, 2}, new double[]{0, 0, 3, 3}, Color.gray);
DPolygons[NumberOf3DPolygons] = new DPolygon(new double[]{0, 0, 0, 0}, new double[]{0, 2, 2, 0}, new double[]{0, 0, 3, 3}, Color.gray);
DPolygons[NumberOf3DPolygons] = new DPolygon(new double[]{2, 2, 2, 2}, new double[]{0, 2, 2, 0}, new double[]{0, 0, 3, 3}, Color.gray);
}

public void paintComponent(Graphics g)
{
g.clearRect(0, 0, 2000, 1200);
g.drawString(System.currentTimeMillis() + "", 20, 20);

for(int i = 0; i < NumberOf3DPolygons; i++)
DPolygons[i].updatePolygon();

setOrder();

for(int i = 0; i < NumberOfPolygons; i++)
DrawablePolygons[NewOrder[i]].drawPolygon(g);
SleepAndRefresh();
}

void setOrder()
{
double[] k = new double[NumberOfPolygons];
NewOrder = new int[NumberOfPolygons];

for(int i = 0; i < NumberOfPolygons; i++)
{
k[i] = DrawablePolygons[i].AvgDist;
NewOrder[i] = i;
}

double temp;
int tempr;
for (int a = 0; a < k.length-1; a++)
for (int b = 0; b < k.length-1; b++)
if(k[b] < k[b + 1])
{
temp = k[b];
tempr = NewOrder[b];
NewOrder[b] = NewOrder[b + 1];
k[b] = k[b + 1];

NewOrder[b + 1] = tempr;
k[b + 1] = temp;
}
}

void SleepAndRefresh()
{
while(true)
{
if(System.currentTimeMillis() - lastRefresh > SleepTime)
{
lastRefresh = System.currentTimeMillis();
repaint();
break;
}
else
{
try
{
Thread.sleep((long)(System.currentTimeMillis() - lastRefresh));
}
catch (Exception e)
{

}
}
}
}

public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_LEFT)
ViewFrom[0] --;
if(e.getKeyCode() == KeyEvent.VK_RIGHT)
ViewFrom[0] ++;
if(e.getKeyCode() == KeyEvent.VK_UP)
ViewFrom[1] --;
if(e.getKeyCode() == KeyEvent.VK_DOWN)
ViewFrom[0] ++;
}

public void keyReleased(KeyEvent e) {
}

public void keyTyped(KeyEvent e) {
}
}
23 changes: 23 additions & 0 deletions 3D - Tutorial 13/Vector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

public class Vector {
double x = 0, y = 0, z = 0;
public Vector(double x, double y, double z)
{
double length = Math.sqrt(x * x + y * y + z * z);
if(length>0)
{
this.x = x/length;
this.y = y/length;
this.z = z/length;
}
}

Vector CrossProduct(Vector V)
{
Vector CrossVector = new Vector(
y * V.z - z * V.y,
z * V.x - x * V.z,
x * V.y - y * V.x);
return CrossVector;
}
}

0 comments on commit 02bee62

Please sign in to comment.