-
Notifications
You must be signed in to change notification settings - Fork 4
/
Square3D.java
103 lines (94 loc) · 3.4 KB
/
Square3D.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package environment;
/** BlocksWorld.java, version 1.11, December 9, 1998.
Applet for interactive blocks world.
Copyright 1998 by Rick Wagner, all rights reserved.
Downloaded from http://rjwagner49.com/Science/ComputerScience/CS480/java/blocks/blocks.htm
* Java source code is for educational purposes only. Viewing or downloading the
* source implies your consent to obey the restrictions:
* <p>
* <ol>
* <li>Use the source code for educational purposes only.
* <li>Give appropriate attribution in all executables and listings.
* <li>Reproduce these restrictions and conditions.
* </ol>
* @author Rick Wagner 1998
* @author W.Pasman cleanup and modifications separating model from renderer
*/
/**
* A square in 3D is built up out of four corner points going around
* counterclockwise as you look at the square from the outside of a solid it
* might be a face of. A square can have any position and orientation in
* 3-space. The center point of the square is used to compute average distance
* for the perspective rendering painter's algorithm and is itself computed as
* the average of its corner points.
*/
public class Square3D {
private Point3D V[]; // Vertex array, zeroeth element is the average
public Square3D() // Default constructor
{
V = new Point3D[5];
V[0] = new Point3D(0, 0, 0); // Centered at the origin
V[1] = new Point3D(50, 50, 0);
V[2] = new Point3D(-50, 50, 0);
V[3] = new Point3D(-50, -50, 0);
V[4] = new Point3D(50, -50, 0);
}
public Square3D(Point3D a, Point3D b, Point3D c, Point3D d) // 4 point
// constructor
{
float x = (a.getX() + b.getX() + c.getX() + d.getX()) / 4; // Average
// x
float y = (a.getY() + b.getY() + c.getY() + d.getY()) / 4; // Average
// y
float z = (a.getZ() + b.getZ() + c.getZ() + d.getZ()) / 4; // Average
// z
V = new Point3D[5];
V[0] = new Point3D(x, y, z);
V[1] = new Point3D(a);
V[2] = new Point3D(b);
V[3] = new Point3D(c);
V[4] = new Point3D(d);
}
public Square3D(Point3D a[]) // Point array constructor
{
float x = (a[1].getX() + a[2].getX() + a[3].getX() + a[4].getX()) / 4; // Average
// x
float y = (a[1].getY() + a[2].getY() + a[3].getY() + a[4].getY()) / 4; // Average
// y
float z = (a[1].getZ() + a[2].getZ() + a[3].getZ() + a[4].getZ()) / 4; // Average
// z
V = new Point3D[5];
V[0] = new Point3D(x, y, z);
V[1] = new Point3D(a[1]);
V[2] = new Point3D(a[2]);
V[3] = new Point3D(a[3]);
V[4] = new Point3D(a[4]);
}
public Square3D(Square3D s) // Copy constructor
{
V = new Point3D[5];
V[0] = new Point3D(s.V[0]);
V[1] = new Point3D(s.V[1]);
V[2] = new Point3D(s.V[2]);
V[3] = new Point3D(s.V[3]);
V[4] = new Point3D(s.V[4]);
}
public Point3D getPoint(int i) {
return new Point3D(V[i]); // Makes a copy of the point
}
public void transform(HMatrix3D m) // Transform the square
{
int i = 0;
for (i = 0; i <= 4; i++) {
V[i].transform(m); // Transform all the points in the square
}
}
// Get the distance squared to some point:
public float getDSquared(Point3D p) {
float sfDSquared;
sfDSquared = (V[0].getX() - p.getX()) * (V[0].getX() - p.getX())
+ (V[0].getY() - p.getY()) * (V[0].getY() - p.getY())
+ (V[0].getZ() - p.getZ()) * (V[0].getZ() - p.getZ());
return sfDSquared; // Distance squared from the square to the point
}
} // End of class Square3D