-
Notifications
You must be signed in to change notification settings - Fork 0
/
IntersectingLines.js
149 lines (123 loc) · 4.13 KB
/
IntersectingLines.js
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
/*
Have the function IntersectingLines(strArr) take strArr which will be an array of 4 coordinates in the form: (x,y). Your program should take these points where the first 2 form a line and the last 2 form a line, and determine whether the lines intersect, and if they do, at what point. For example: if strArr is ["(3,0)","(1,4)","(0,-3)","(2,3)"], then the line created by (3,0) and (1,4) and the line created by (0,-3) (2,3) intersect at (9/5,12/5). Your output should therefore be the 2 points in fraction form in the following format: (9/5,12/5). If there is no denominator for the resulting points, then the output should just be the integers like so: (12,3). If any of the resulting points is negative, add the negative sign to the numerator like so: (-491/63,-491/67). If there is no intersection, your output should return the string "no intersection". The input points and the resulting points can be positive or negative integers.
Hard challenges are worth 15 points and you are not timed for them. Use the Parameter Testing feature in the box below to test your code with different arguments.
*/
function IntersectingLines(strArr) {
function decimal_places(num) {
for (var i=0; i<num.length; i++) {
if (num[i]=='.') {
return num.length-1-i;
}
}
return '0';
}
function to_fraction(num) {
var fraction = [];
fraction.push(num*Math.pow(10,decimal_places(num)+1));
fraction.push(Math.pow(10,decimal_places(num)+1));
if (fraction[0]>fraction[1]) {
var lower = fraction[1];
}
else if (fraction[1]>fraction[0]) {
var lower = fraction[0];
}
for (var i=lower-1; i>1; i--) {
if (fraction[0]%i==0 && fraction[1]%i==0) {
fraction[0] = fraction[0]/i;
fraction[1] = fraction[1]/i;
}
}
if (fraction[1] == 1) {
return fraction[0];
}
return fraction[0]+'/'+fraction[1];
}
var points = [];
for (var i=0; i<strArr.length; i++) {
points.push(strArr[i].match(/[0-9\-]{1,}/g));
}
for (var i=0; i<3; i++) {
for (var j=0; j<2; j++) {
points[i][j] = Number(points[i][j]);
}
}
var m1 = (points[0][1]-points[1][1])/(points[0][0]-points[1][0]);
var m2 = (points[2][1]-points[3][1])/(points[2][0]-points[3][0]);
if (m1 == m2) {
return 'no intersection';
}
var intercept = [];
if (m1 == 'Infinity') {
intercept[0] = points[0][0];
if (m2 == 0) {
intercept[1] = points[2][1];
}
else {
var c2 = (points[2][1]-m2*points[2][0]);
intercept[1] = m2*intercept[0] + c2;
}
}
else if (m2 == 'Infinity') {
intercept[0] = points[2][0];
if (m1 == 0) {
intercept[1] = points[0][1];
}
else {
var c1 = (points[0][1]-m1*points[0][0]);
intercept[1] = m1*intercept[0] + c1;
}
}
else {
var c1 = (points[0][1]-m1*points[0][0]);
var c2 = (points[2][1]-m2*points[2][0]);
intercept[0] = (c2-c1)/(m1-m2);
intercept[1] = m1*intercept[0]+c1;
}
// limits1[lowx, highx, lowy, highy]
// limits2[lowx, highx, lowy, highy]
var limits1 = [];
var limits2 = [];
if (points[0][0] > points[1][0]) {
limits1[0] = points[1][0];
limits1[1] = points[0][0];
}
else if (points[1][0] > points[0][0]) {
limits1[0] = points[0][0];
limits1[1] = points[1][0];
}
if (points[0][1] > points[1][1]) {
limits1[2] = points[1][1];
limits1[3] = points[0][1];
}
else if (points[1][1] > points[0][1]) {
limits1[2] = points[0][1];
limits1[3] = points[1][1];
}
if (points[2][0] > points[3][0]) {
limits2[0] = points[3][0];
limits2[1] = points[2][0];
}
else if (points[3][0] > points[2][0]) {
limits2[0] = points[2][0];
limits2[1] = points[3][0];
}
if (points[2][1] > points[3][1]) {
limits2[2] = points[3][1];
limits2[3] = points[2][1];
}
else if (points[3][1] > points[2][1]) {
limits2[2] = points[2][1];
limits2[3] = points[3][1];
}
if (intercept[0] < limits1[0] || intercept[0] > limits1[1] || intercept[0] < limits2[0] || intercept[0] > limits2[1] || intercept[1] < limits1[2] || intercept[1] > limits1[3] || intercept[1] < limits2[2] || intercept[1] > limits2[3]) {
return 'no intersection';
}
for (var i=0; i<intercept.length; i++) {
intercept[i] = to_fraction(intercept[i]);
}
var results = '(' + intercept.join(',') + ')';
return results;
}
// keep this function call here
// to see how to enter arguments in JavaScript scroll down
IntersectingLines(readline());