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
69297b1
commit d4a4819
Showing
6 changed files
with
272 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,39 @@ | ||
|
||
public class Calculator { | ||
static double DrawX = 0, DrawY = 0, t; | ||
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 ViewToPoint = new Vector(x - ViewFrom[0], y - ViewFrom[1], z - ViewFrom[2]); | ||
|
||
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 = PlaneVector2.x * x + PlaneVector2.y * y + PlaneVector2.z * z; | ||
DrawY = PlaneVector1.x * x + PlaneVector1.y * y + PlaneVector1.z * z; | ||
} | ||
} | ||
} |
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,20 @@ | ||
import java.awt.Toolkit; | ||
import javax.swing.JFrame; | ||
|
||
public class DDDTutorial extends JFrame{ | ||
static JFrame F = new DDDTutorial(); | ||
Screen ScreenObject = new Screen(); | ||
|
||
public DDDTutorial() | ||
{ | ||
add(ScreenObject); | ||
setUndecorated(true); | ||
setSize(Toolkit.getDefaultToolkit().getScreenSize()); | ||
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,52 @@ | ||
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() | ||
{ | ||
double total = 0; | ||
double[] newX = new double[x.length]; | ||
double [] newY = new double[x.length]; | ||
|
||
for(int i = 0; i < x.length; i++) | ||
{ | ||
newX[i] = 500 + 50 * Calculator.CalculatePositionX(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]); | ||
newY[i] = 500 + 50 * Calculator.CalculatePositionY(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]); | ||
total += Calculator.t; | ||
} | ||
|
||
poly = Screen.NumberOfPolygons; | ||
Screen.DrawablePolygons[Screen.NumberOfPolygons] = new PolygonObject(newX, newY, c); | ||
Screen.DrawablePolygons[poly].AvgDist = total / x.length; | ||
} | ||
|
||
void updatePolygon() | ||
{ | ||
double[] newX = new double[x.length]; | ||
double [] newY = new double[x.length]; | ||
|
||
for(int i = 0; i < x.length; i++) | ||
{ | ||
newX[i] = 500 + 50 * Calculator.CalculatePositionX(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]); | ||
newY[i] = 500 + 50 * Calculator.CalculatePositionY(Screen.ViewFrom, Screen.ViewTo, x[i], y[i], z[i]); | ||
} | ||
|
||
Screen.DrawablePolygons[poly] = new PolygonObject(newX, newY, c); | ||
Screen.NumberOfPolygons --; | ||
} | ||
} |
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,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[] {5, 0, 0}; | ||
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) { | ||
} | ||
} |
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; | ||
} | ||
} |