-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution_triangle.js
More file actions
213 lines (185 loc) · 7.45 KB
/
solution_triangle.js
File metadata and controls
213 lines (185 loc) · 7.45 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
function intersects_tr(fig1, fig2) { // return arr with poly's
'use strict'
var dots = [],
sections = [],
triangulation = [];
for (var x in fig1) {
dots.push({
x: fig1[x].x,
y: fig1[x].y
})
};
for (var x in fig2) {
dots.push({
x: fig2[x].x,
y: fig2[x].y
})
};
//dotsOfCrossingF1F2
for (var i = dots.length - 1; i >= 0; i--) {
for (var j = dots.length - 1; j >= 0; j--) {
if (i == j) continue;
sections.push({
start: {
x: dots[i].x,
y: dots[i].y
},
end: {
x: dots[j].x,
y: dots[j].y
},
length: distanceBetween2Dots(dots[i].x, dots[i].y, dots[j].x, dots[j].y)
});
};
};
sections.sort(sortByLength);
getStructuredLines(fig1, fig2);
//doNotCrossLines(fig1, fig2);
function pushStructuredLines() {
}
function sortByLength(a, b) {
return a.length - b.length;
}
// console.dir(sections);
/*sections = [{x: ,y:}, {x: ,y:}]*/
function distanceBetween2Dots(aX, aY, bX, bY) {
return Math.sqrt(Math.pow((bX - aX), 2) + Math.pow((bY - aY), 2));
};
function getPairOfCrossLines(line1, line2) { //line1[0]=a1, line1[1]=a2, line2[0]=b1, line2[1]=b2\\\повернемо масив з лініями такої ж структури
line1 = normilizeLine(line1[0], line1[1]);
line2 = normilizeLine(line2[0], line2[1]);
if (line1[0].x == line2[0].x && line1[0].y == line2[0].y && line1[1].x == line2[1].x && line1[1].y == line2[1].y) {
return false; //[line1];
} //line1==line2
if (line1[0].x == line2[0].x && line1[0].y == line2[0].y || line1[1].x == line2[1].x && line1[1].y == line2[1].y) {
return false; //[line1];
} //line1&line2 has one
var denom, a, b, num1, num2, x, y;
denom = ((line2[1].y - line2[0].y) * (line1[1].x - line1[0].x)) - ((line2[1].x - line2[0].x) * (line1[1].y - line1[0].y));
if (denom == 0) {
if (isBoxesIntersected(line1, line2)) { //на одній прямій =>на вихід 2, 3 або 4 відрізки.
return getPartsOfLinesThatBoxesIntersected(line1, line2);
}
return [line1]; //значить паралельні
}
a = line1[0].y - line2[0].y;
b = line1[0].x - line2[0].x;
num1 = ((line2[1].x - line2[0].x) * a) - ((line2[1].y - line2[0].y) * b);
num2 = ((line1[1].x - line1[0].x) * a) - ((line1[1].y - line1[0].y) * b);
a = num1 / denom;
b = num2 / denom;
x = line1[0].x + (a * (line1[1].x - line1[0].x));
y = line1[0].y + (a * (line1[1].y - line1[0].y));
var xy = {
x: x,
y: y
};
//a==0 &&b==0 when some line has vertice in oth
if (a == 0) return [line1, [line2[0], xy],
[xy, line2[1]]
];
if (b == 0) return [line2, [line1[0], xy],
[xy, line1[1]]
];
if ((a > 0 && a < 1) && (b > 0 && b < 1)) { //so there are 4 lines
return [
[line1[0], xy],
[xy, line1[1]],
[line2[0], xy],
[xy, line2[1]]
]
} else {
return [line1]; //there are no lines that are crossing
};
function normilizeLine(start, end) { //x1<x2,if x1=x2=>y1<y2
if (start.x < end.x) return [start, end];
if (start.x > end.x) return [end, start];
if (start.y < end.y) return [start, end];
return [end, start]
};
function isBoxesIntersected(line1, line2) {
return !(line2[0].x >= line1[1].x || line2[1].x <= line1[0].x || line2[1].y <= line1[0].y || line2[0].y >= line1[1].y);
//return (Math.abs(line1[0].x - line2[0].x) * 2 < (line1[1].x - line1[0].x + line2[1].x - line2[0].x)) &&
// (Math.abs(line1[0].y - line2[0].y) * 2 < (line1[1].y - line1[0].y + line2[1].y - line2[0].y));
};
function getLinesIfOneVerticleAtOtherLine(line1, line2) {
return [line1, [line2[0], xy],
[xy, line2[1]]
];
}
function getPartsOfLinesThatBoxesIntersected(line1, line2) { //group the dots in obj than return smaller lines that they create
var obj = {},
previous,
arrWithLines = [];
line1.concat(line2).sort(sortArr).forEach(function(xy) {
obj[xy.x + 'x' + xy.y] = xy;
});
for (var dot in obj) {
if (previous) {
arrWithLines.push([previous, obj[dot]]);
}
previous = obj[dot];
};
return arrWithLines;
function sortArr(a, b) {
return a.x - b.x
};
};
};
//smash the poly's to have not crossing lines
function getStructuredLines(fig1, fig2) {//#todo якийсь кривий список получається, треба вдосконалити getPairOfCrossLines і пуш в арей
var arr = [];
/*= [{
start: fig1[0],
end: fig1[1]
}];*/
fig1.push(fig1[0]);
fig2.push(fig2[0]);
pushFigInArrSimple(fig1);
pushInArr(fig2);
function pushInArr(fig) {
var figCount = fig.length,
arrCount,
xy,
indikatorThatWePushedLine;
while (--figCount) {
arrCount = arr.length;
indikatorThatWePushedLine = false;
while (arrCount--) {
debugger;
xy = getPairOfCrossLines([fig[figCount], fig[figCount - 1]], [arr[arrCount].start, arr[arrCount].end]); //return array 1-4 length or false
if (!xy) continue; //line1==line2;
if (xy.length == 1 && !indikatorThatWePushedLine) { //parralel or not crossing
arr.push({
start: fig[figCount],
end: fig[figCount - 1]
});
indikatorThatWePushedLine = true;
} else if (xy.length > 1) { //3 одна з них вже є, 2 треба додати або одну додати і дві є. або вже взяти і просто закинути замінити з поточною лінією
//можна взяти останню, потім length-1 i forEach
arr[arrCount] = {
start: xy[0][0],
end: xy[0][1]
};
for (var i = 1; i < xy.length; i++) {
arr.push({
start: xy[i][0],
end: xy[i][1]
});
};
};
};
};
};
console.log(arr);
function pushFigInArrSimple(fig) {
var figCount = fig.length;
while (--figCount) {
arr.push({
start: fig[figCount],
end: fig[figCount - 1]
})
};
};
};
};