forked from javatutorials101/Tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
02bee62
commit 040f756
Showing
6 changed files
with
384 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
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; | ||
boolean[] Keys = new boolean[8]; | ||
|
||
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); | ||
for(int i = -4; i < 5; i++) | ||
for(int j = -4; j < 5; j++) | ||
DPolygons[NumberOf3DPolygons] = new DPolygon(new double[]{i, i, i + 1, i + 1}, new double[]{j, j + 1, j + 1, j}, new double[]{0, 0, 0, 0}, Color.green ); | ||
} | ||
|
||
public void paintComponent(Graphics g) | ||
{ | ||
Controls(); | ||
|
||
g.clearRect(0, 0, 2000, 1200); | ||
|
||
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) | ||
{ | ||
|
||
} | ||
} | ||
} | ||
} | ||
|
||
void Controls() | ||
{ | ||
Vector ViewVector = new Vector(ViewTo[0] - ViewFrom[0], ViewTo[1] - ViewFrom[1], ViewTo[2] - ViewFrom[2]); | ||
if(Keys[4]) | ||
{ | ||
ViewFrom[0] += ViewVector.x; | ||
ViewFrom[1] += ViewVector.y; | ||
ViewFrom[2] += ViewVector.z; | ||
ViewTo[0] += ViewVector.x; | ||
ViewTo[1] += ViewVector.y; | ||
ViewTo[2] += ViewVector.z; | ||
} | ||
|
||
if(Keys[6]) | ||
{ | ||
ViewFrom[0] -= ViewVector.x; | ||
ViewFrom[1] -= ViewVector.y; | ||
ViewFrom[2] -= ViewVector.z; | ||
ViewTo[0] -= ViewVector.x; | ||
ViewTo[1] -= ViewVector.y; | ||
ViewTo[2] -= ViewVector.z; | ||
} | ||
|
||
Vector VerticalVector = new Vector(0, 0, 1); | ||
Vector SideViewVector = ViewVector.CrossProduct(VerticalVector); | ||
|
||
if(Keys[5]) | ||
{ | ||
ViewFrom[0] += SideViewVector.x; | ||
ViewFrom[1] += SideViewVector.y; | ||
ViewFrom[2] += SideViewVector.z; | ||
ViewTo[0] += SideViewVector.x; | ||
ViewTo[1] += SideViewVector.y; | ||
ViewTo[2] += SideViewVector.z; | ||
} | ||
|
||
if(Keys[7]) | ||
{ | ||
ViewFrom[0] -= SideViewVector.x; | ||
ViewFrom[1] -= SideViewVector.y; | ||
ViewFrom[2] -= SideViewVector.z; | ||
ViewTo[0] -= SideViewVector.x; | ||
ViewTo[1] -= SideViewVector.y; | ||
ViewTo[2] -= SideViewVector.z; | ||
} | ||
} | ||
|
||
public void keyPressed(KeyEvent e) { | ||
if(e.getKeyCode() == KeyEvent.VK_LEFT) | ||
Keys[0] = true; | ||
if(e.getKeyCode() == KeyEvent.VK_RIGHT) | ||
Keys[1] = true; | ||
if(e.getKeyCode() == KeyEvent.VK_UP) | ||
Keys[2] = true; | ||
if(e.getKeyCode() == KeyEvent.VK_DOWN) | ||
Keys[3] = true; | ||
if(e.getKeyCode() == KeyEvent.VK_W) | ||
Keys[4] = true; | ||
if(e.getKeyCode() == KeyEvent.VK_A) | ||
Keys[5] = true; | ||
if(e.getKeyCode() == KeyEvent.VK_S) | ||
Keys[6] = true; | ||
if(e.getKeyCode() == KeyEvent.VK_D) | ||
Keys[7] = true; | ||
} | ||
|
||
public void keyReleased(KeyEvent e) { | ||
if(e.getKeyCode() == KeyEvent.VK_LEFT) | ||
Keys[0] = false; | ||
if(e.getKeyCode() == KeyEvent.VK_RIGHT) | ||
Keys[1] = false; | ||
if(e.getKeyCode() == KeyEvent.VK_UP) | ||
Keys[2] = false; | ||
if(e.getKeyCode() == KeyEvent.VK_DOWN) | ||
Keys[3] = false; | ||
if(e.getKeyCode() == KeyEvent.VK_W) | ||
Keys[4] = false; | ||
if(e.getKeyCode() == KeyEvent.VK_A) | ||
Keys[5] = false; | ||
if(e.getKeyCode() == KeyEvent.VK_S) | ||
Keys[6] = false; | ||
if(e.getKeyCode() == KeyEvent.VK_D) | ||
Keys[7] = false; | ||
|
||
} | ||
|
||
public void keyTyped(KeyEvent e) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |