-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFabric.java
More file actions
114 lines (85 loc) · 2.77 KB
/
Fabric.java
File metadata and controls
114 lines (85 loc) · 2.77 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
import java.util.LinkedList;
import java.util.Random;
/* @author Sidney Durant
* This class simulates a fabric composed of a grid like arrangement of points
* each point is connected to four neighbors, and with every tick each spring
* attempts to return to its resting length
*/
public class Fabric {
LinkedList<Link> allLinks;
LinkedList<PointMass> allPointMasses;
public Fabric( int x, int y, int pointMassCountX, int pointMassCountY, int linkLength, int linkStrength, Render ren ){
// construct pointMasses in an array, and then copy into allPointMasses
PointMass[][] pointMassArray = new PointMass[pointMassCountX][pointMassCountY];
int xPos;
int yPos;
Random r = new Random();
for ( int xi = 0; xi < pointMassCountX; xi++ ){
for( int yi = 0; yi < pointMassCountY; yi++ ){
xPos = x + (linkLength*xi);
yPos = y + (linkLength*yi);
pointMassArray[xi][yi] = new PointMass( xPos, yPos, 0, 0, new Color( r.nextInt() ) );
if( yi == 0 ){
pointMassArray[xi][yi].setPin( true );
}
}
}
allPointMasses = new LinkedList<PointMass>();
for( PointMass[] subArray : pointMassArray ){
for( PointMass p : subArray ){
allPointMasses.add(p);
}
}
allLinks = new LinkedList<Link>();
// construct all of the links and add them to the list of allLinks
for ( int xi = 0; xi < pointMassCountX; xi++ ){
for ( int yi = 0; yi < pointMassCountY; yi++ ){
if( xi != 0 ){
allLinks.add( new Link( pointMassArray[xi][yi], pointMassArray[xi-1][yi], linkLength, linkStrength ) );
}
if( yi != 0 ){
allLinks.add( new Link( pointMassArray[xi][yi], pointMassArray[xi][yi-1], linkLength, linkStrength ) );
}
}
}
}
public void tick(){
for( PointMass p : allPointMasses ){
p.update();
}
LinkedList<Link> allTornLinks = new LinkedList<Link>();
int relaxationCount = 15; // number of times to relax the fabric
for( int i = 1; i <= relaxationCount; i++ ){
for( Link l : allLinks){
// relax, then if link is torn, and this is the last relaxation
// prepare to remove the link
if ( l.solve() && i == relaxationCount ){
allTornLinks.add(l);
}
}
}
// remove all torn links
for( Link tornLink : allTornLinks){
allLinks.remove( tornLink );
}
}
public void draw( Render r ){
for( Link l : allLinks){
l.draw( r );
}
}
public void drag( int x, int y, int xOld, int yOld ){
float dx = x-xOld;
float dy = y-yOld;
float distanceSquared = 0;
for( PointMass p : allPointMasses ){
// if distance between p and x,y < certain value;
distanceSquared = (x-p.x)*(x-p.x) + (y-p.y)*(y-p.y);
if( distanceSquared < 900 ){
p.translate( dx, dy );
} else if ( distanceSquared < 1600 ){
p.translate( dx/2, dy/2);
}
}
}
}