-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSphere.java
More file actions
125 lines (115 loc) · 3.33 KB
/
Sphere.java
File metadata and controls
125 lines (115 loc) · 3.33 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import java.util.*;
import java.awt.*;
public class Sphere extends Polygon
{
public int numSides;
public Sphere(Point3D p, double r, int n)
{
super();
numSides=n;
id="Sphere";
ArrayList<Point3D> points1 = new ArrayList<Point3D>();
RegularPolygon rp = new RegularPolygon(p,r,n*2);
Point3D center = rp.getCenter();
for(int i=0;i<n;i++)
{
rp.pan(new Point3D(0,0,0));
rp.rotate(center,0,0,Math.PI/n);
ArrayList<Line3D> points2=rp.lines;
for(Line3D pt : points2)
lines.add(pt);
}
for(int i=1;i<=n;i++)
{
double xv=i*r*2/(n+1)-r;
rp = new RegularPolygon(new Point3D(p.x+xv,p.y,p.z),Math.sqrt(Math.pow(r,2)-Math.pow(xv,2)),n*2);
rp.pan(new Point3D(0,0,0));
rp.rotate(rp.center,0,Math.PI/2,0);
ArrayList<Line3D> points2=rp.lines;
for(Line3D pt : points2)
lines.add(pt);
}
lines.add(new Line3D(new Point3D(p.x-r,p.y,p.z),new Point3D(p.x-r,p.y,p.z)));
lines.add(new Line3D(new Point3D(p.x+r,p.y,p.z),new Point3D(p.x+r,p.y,p.z)));
// lines.add(new Line3D(points1.get(0),points1.get(points1.size()-1)));
pan(new Point3D(0,0,0));
}
public Sphere(Sphere s)
{
super(s);
pan(new Point3D(0,0,0));
}
public ArrayList<Plane> getPlanes()
{
Plane plane;
ArrayList<Point3D> add;
ArrayList<Plane> out= new ArrayList<Plane>();
int i0=numSides*numSides*2;
for(int i=-1;i<numSides;i++)
{
for(int j=0;j<numSides*2-1;j++)
{
add=new ArrayList<Point3D>();
if(i==-1)
{
i++;
add.add(lines.get(i0+i*numSides*2+j).point1);
add.add(lines.get(i0+i*numSides*2+j).point2);
add.add(lines.get(lines.size()-2).point1);
plane=new Plane(add);
plane.color=color;
out.add(plane);
i--;
}
else if(i==numSides-1)
{
add.add(lines.get(i0+i*numSides*2+j).point1);
add.add(lines.get(i0+i*numSides*2+j).point2);
add.add(lines.get(lines.size()-1).point1);
plane=new Plane(add);
plane.color=color;
out.add(plane);
}
else
{
add.add(lines.get(i0+i*numSides*2+j).point1);
add.add(lines.get(i0+i*numSides*2+j).point2);
add.add(lines.get(i0+(i+1)*numSides*2+j).point2);
add.add(lines.get(i0+(i+1)*numSides*2+j).point1);
plane=new Plane(add);
plane.color=color;
out.add(plane);
}
}
add=new ArrayList<Point3D>();
if(i==-1)
{
add.add(lines.get(i0).point1);
add.add(lines.get(i0+numSides*2-1).point2);
add.add(lines.get(lines.size()-2).point1);
plane=new Plane(add);
plane.color=color;
out.add(plane);
}
else if(i==numSides-1)
{
add.add(lines.get(i0+i*numSides*2).point1);
add.add(lines.get(i0+(i+1)*numSides*2-1).point2);
add.add(lines.get(lines.size()-1).point1);
plane=new Plane(add);
plane.color=color;
out.add(plane);
}
else {
add.add(lines.get(i0+i*numSides*2).point1);
add.add(lines.get(i0+(i+1)*numSides*2).point1);
add.add(lines.get(i0+(i+1)*numSides*2+numSides*2-1).point2);
add.add(lines.get(i0+(i)*numSides*2+numSides*2-1).point2);
plane=new Plane(add);
plane.color=color;
out.add(plane);
}
}
return out;
}
}