-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShape.pde
165 lines (126 loc) · 3.89 KB
/
Shape.pde
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Stephen Aldriedge, 2013
// IMPORTANT: You probably won't need to modify anything in this file!!!
// Shape center is always at (0,0)
class Shape {
Shape(PVector[] verts) {
vertCount = verts.length;
this.verts = verts;
}
private PVector[] verts;
private int vertCount;
int getVertCount() {
return vertCount;
}
PVector getVert(int i) {
return verts[i];
}
}
// helper function
float angleBetween (PVector v1, PVector v2) {
float a = atan2(v1.y, v1.x) - atan2(v2.y,v2.x);
return a;
}
class TouchShape {
Shape shape;
boolean active;
Touch[] touches;
int count;
int tolerance;
PVector pos;
PVector firstToSecond;
PVector firstToPos;
float rotAngle;
float[] distToFirst;
TouchShape(Shape s, int tol) {
shape = s;
active = false;
count = s.vertCount;
touches = new Touch[count];
tolerance = tol;
pos = new PVector(0,0);
firstToSecond = PVector.sub(shape.getVert(0), shape.getVert(1));
firstToPos = new PVector(-shape.getVert(0).x, -shape.getVert(0).y);
rotAngle = 0;
distToFirst = new float[count-1];
for(int i=1; i<count; i++) {
distToFirst[i-1] = shape.getVert(0).dist(shape.getVert(i));
}
}
void updateTransform() {
// TODO: fix so rotation is around pos
PVector curFirstToSecond = new PVector(touches[1].offsetX - touches[0].offsetX,
touches[1].offsetY - touches[0].offsetY);
rotAngle = angleBetween(curFirstToSecond, firstToSecond);
// update position
pos.x = firstToPos.x + touches[0].offsetX;
pos.y = firstToPos.y + touches[0].offsetY;
}
boolean tryPoints(ArrayList t) {
int tSize = t.size();
if(tSize < count) return false;
int[] allPtIndex = new int[30];
// for each point in t
for(int i=0; i<tSize; i++) {
ArrayList potential = new ArrayList();
potential.add((Touch)t.get(i));
ArrayList check = new ArrayList(t);
PVector curPt = new PVector(((Touch)t.get(i)).offsetX, ((Touch)t.get(i)).offsetY);
int tSize2 = tSize;
// for each distance we are looking for
for(int j=0; j<count-1; j++) {
// check all distances to the current point
for(int ptIndex=0; ptIndex < tSize2; ptIndex++) {
// no need to check distance between current point and itself
if(ptIndex == i) continue;
float dist = curPt.dist(new PVector(((Touch)check.get(ptIndex)).offsetX,
((Touch)check.get(ptIndex)).offsetY));
if((distToFirst[j] + tolerance < dist) ||
(distToFirst[j] - tolerance > dist)) continue;
potential.add((Touch)check.get(ptIndex));
check.remove(ptIndex);
ptIndex--;
tSize2--;
}
}
// if everything matched then set the touch array and return true
if(potential.size() == count) {
for(int m=0; m<count; m++) {
touches[m] = (Touch)potential.get(m);
}
return true;
}
}
return false;
}
void touchStart(ArrayList unassigned) {
if(!active) {
if(tryPoints(unassigned)) {
active = true;
}
}
}
void touchMove(TouchEvent touchEvent) {
// The only thing we need to do here (making the assumption that tangibles
// can't have separately moving points) is update the transform (object position and
// rotation)
if(active) updateTransform();
}
void touchEnd(TouchEvent touchEvent) {
if(!active) return;
for(int i=0; i<count; i++) {
for(int j=0; j<touchEvent.changedTouches.length; j++) {
if( touchEvent.changedTouches[j] == touches[i] ) {
active = false;
return;
}
}
}
}
void draw() {
resetMatrix();
translate(pos.x, pos.y);
rotate(rotAngle);
fill(0,255,255);
ellipse(0, 0, 70, 70);
}
}