-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinecision.js
More file actions
295 lines (243 loc) · 9.26 KB
/
Linecision.js
File metadata and controls
295 lines (243 loc) · 9.26 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
const verifyNumber = (event) => {
if (event.target.value.indexOf(',') >= 0)
event.target.value = event.target.value.replace(/,/gi, '')
if (isNaN(parseFloat(event.target.value)))
event.target.setCustomValidity(getStringForJs('error.input.nan'));
else
event.target.setCustomValidity('');
}
const setNumFields = (element, fields) => {
while (element.children.length > fields)
element.removeChild(element.lastChild);
while (element.children.length < fields) {
const n = document.createElement("input");
n.type = 'text';
n.onchange = verifyNumber;
element.appendChild(n);
}
};
const decisionTree = {
function: (line1, line2) => line1.isParallel(line2),
flag: 'parallel',
true: {
function: (line1, line2) => line1.hasPoint(line2.start),
flag: 'same',
true: null,
false: null,
},
false: {
function: (line1, line2) => !!line1.cuttingPoint(line2),
flag: 'cuttingpoint',
true: {
function: (line1, line2) => line1.direction.scalarProduct(line2.direction) === 0,
flag: 'perpendicular',
true: null,
false: null,
},
false: null,
},
}
console.log('decisionTree:', decisionTree);
class Linecision {
constructor(line1, line2, dimensions = 3) {
// validate lines
if (Object.keys(line1).indexOf('start') < 0 || Object.keys(line1).indexOf('direction') < 0)
throw new Error("Line 1 is missing either a 'start' or a 'direction'.");
if (Object.keys(line2).indexOf('start') < 0 || Object.keys(line2).indexOf('direction') < 0)
throw new Error("Line 2 is missing either a 'start' or a 'direction'.");
this.line1 = line1;
this.line2 = line2;
this.lines = [line1, line2];
this.dimensions = dimensions;
this.updateVectors();
console.log('Linecision initialised with', this.dimensions, 'dimensions and the following vectors:', this.lines);
}
updateVectors() {
this.lines.forEach(line => {
setNumFields(line.start, this.dimensions);
setNumFields(line.direction, this.dimensions);
});
}
calculate() {
console.log('calculating now!');
const start1 = new Vector(...[...this.line1.start.children].map(e => parseFloat(e.value) || 0));
const direction1 = new Vector(...[...this.line1.direction.children].map(e => parseFloat(e.value) || 0));
const start2 = new Vector(...[...this.line2.start.children].map(e => parseFloat(e.value) || 0));
const direction2 = new Vector(...[...this.line2.direction.children].map(e => parseFloat(e.value) || 0));
const line1 = new Line(start1, direction1);
const line2 = new Line(start2, direction2);
const flags = this.calculateRec(line1, line2, decisionTree);
console.log('flags', flags);
return flags;
}
calculateRec(line1, line2, tree, flags = {}) {
const result = tree.function(line1, line2);
flags[tree.flag] = result;
const newTree = result ? tree.true : tree.false;
if (newTree)
return this.calculateRec(line1, line2, newTree, flags);
return flags;
}
setDimensons(dimensions) {
this.dimensions = dimensions;
this.updateVectors();
}
}
class Line {
constructor(start, direction) {
if (start.getDimensions() != direction.getDimensions())
throw new Error("start and direction have different dimensions!");
this.start = start;
this.direction = direction;
}
getDimensions() {
if (this.start.getDimensions() != this.direction.getDimensions())
throw new Error("start and direction have different dimensions!");
return this.start.getDimensions();
}
isSame(other) {
return this.isParallel(other) && this.hasPoint(other.start);
}
isParallel(other) {
return !isNaN(this.direction.linearDependence(other.direction));
}
hasPoint(point) {
// A = B + m * r
// A - B = m * r
return !isNaN(point.minus(this.start).linearDependence(this.direction));
}
cuttingPoint(other) {
// if one dimensional, every line is the same
if (this.getDimensions() <= 1)
return new Vector(undefined);
const equations = this.getEquations(other);
for (let eq1idx = 0; eq1idx < this.getDimensions(); eq1idx++) {
for (let eq2idx = eq1idx + 1; eq2idx < this.getDimensions(); eq2idx++) {
var eq1 = equations[eq1idx];
var eq2 = equations[eq2idx];
var eq = eq1.minus(eq2.multiply(eq1.d / eq2.d));
if (eq.b === 0) {
// l is gone too!
if (eq.a === eq.c) // equation correct
continue; // switch equations and try again
else // equation incorrect
return undefined; // no cutting point
}
var l = eq.getL(0); // 0, because value of u doesn't matter because of calculations above (d = 0)
var u = eq1.getU(l);
// check if correct for everyone
for (let checkeq of equations)
if (!checkeq.check(l, u))
return undefined; // no cutting point
return this.start.plus(this.direction.multiply(l)); // cutting point
}
}
}
getEquations(other) {
const equations = [];
for (let i = 0; i < this.getDimensions(); i++)
equations.push(new Equation(this.start.get(i), this.direction.get(i), other.start.get(i), other.direction.get(i)));
return equations;
}
}
// technically a one dimensional line, but this has some additional methods
class Equation {
// equation: a + l * b = c + u * d ; l, u in R
constructor(a, b, c, d) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
multiply(factor) {
return new Equation(this.a * factor, this.b * factor, this.c * factor, this.d * factor);
}
minus(other) {
return new Equation(this.a - other.a, this.b - other.b, this.c - other.c, this.d - other.d);
}
getL(u) {
// a + l * b = c + u * d
// l * b = c + u * d - a
// l = (c + u * d - a) / b
return (this.c + u * this.d - this.a) / this.b;
}
getU(l) {
// a + l * b = c + u * d
// a + l * b - c = u * d
// (a + l * b - c) / d = u
return (this.a + l * this.b - this.c) / this.d;
}
check(l, u) {
// a + l * b = c + u * d
return this.a + l * this.b === this.c + u * this.d;
}
}
class Vector {
constructor(...values) {
if (values.length <= 0)
throw new Error("0-dimensional vector is not allowed!");
this.values = values;
}
get(i) {
return this.values[i];
}
getDimensions() {
return this.values.length;
}
isAllZeros() {
for (let value of this.values)
if (value !== 0)
return false;
return true;
}
linearDependence(other) {
this.verifySameDimension(other);
if (this.isAllZeros() || other.isAllZeros())
return 0;
let fac = 0;
let setFac = false;
for (let i = 0; i < this.values.length; i++) {
if (this.values[i] === 0 && other.values[i] === 0)
continue;
if (this.values[i] === 0 || other.values[i] === 0)
return NaN;
if (setFac) {
if (this.values[i] / other.values[i] !== fac)
return NaN;
} else {
fac = this.values[i] / other.values[i];
setFac = true;
}
}
return fac;
}
minus(other) {
this.verifySameDimension(other);
return new Vector(...new Array(this.getDimensions()).fill(undefined).map((e, i) => this.values[i] - other.values[i]));
}
plus(other) {
this.verifySameDimension(other);
return new Vector(...new Array(this.getDimensions()).fill(undefined).map((e, i) => this.values[i] + other.values[i]));
}
// ONLY MULTIPLY WITH NUMBER!
multiply(number) {
return new Vector(...new Array(this.getDimensions()).fill(undefined).map((e, i) => this.values[i] * number));
}
scalarProduct(other) {
this.verifySameDimension(other);
let result = 0;
for (let i = 0; i < this.getDimensions(); i++)
result += this.values[i] * other.values[i];
return result;
}
verifySameDimension(other) {
if (other.getDimensions() !== this.getDimensions())
throw new Error("Tried to perform calculation on two Vectors with different dimensionality!");
}
static fromDimensions(dimensions) {
const arr = [];
for (let i = 0; i < dimensions; i++)
arr.push(0);
return new Vector(...arr);
}
}