-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexp1.html
More file actions
190 lines (169 loc) · 6.84 KB
/
exp1.html
File metadata and controls
190 lines (169 loc) · 6.84 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
<html>
<head>
<meta charset="utf-8"/>
<title>经过两点的弧线</title>
<style>
#map {
width: 100%;
height: 400px;
}
</style>
</head>
<body>
<div class="container">
<div>
<input type="text" id="x1" placeholder="x1">
<input type="text" id="y1" placeholder="y1">
<input type="text" id="x2" placeholder="x2">
<input type="text" id="y2" placeholder="y2">
<button id="crossPoint" onclick="on_PB_CrossPoint()">cross point</button>
<p id="result2"></p>
</div>
<div>
<input type="text" id="x3" placeholder="x1" value="15.427083333333334">
<input type="text" id="y3" placeholder="y1" value="93.43229166666667">
<input type="text" id="x4" placeholder="x2" value="14.833333333333334">
<input type="text" id="y4" placeholder="y2" value="92.75520833333333">
<input type="text" id="offset" placeholder="offset" value="0.3466198478593559">
<button id="offsetPoint2" onclick="on_PB_OffsetPoint()">offset point</button>
<p id="result3"></p>
</div>
<div>
<input type="text" id="x5" placeholder="x1" value="15.427083333333334">
<input type="text" id="y5" placeholder="y1" value="93.43229166666667">
<input type="text" id="x6" placeholder="x2" value="13.8125">
<input type="text" id="y6" placeholder="y2" value="95.05338541666667">
<input type="text" id="offset2" placeholder="offset" value="0.8806415411141064">
<button id="offsetPoint" onclick="on_PB_OffsetPoint2()">offset point</button>
<p id="result4"></p>
</div>
<div class="row">
<div id="map"></div>
</div>
</div>
<script>
var map = L.map('map').setView([28.735308, 77.52496], 9);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
// 获得中垂线交叉点的坐标
function PB_CrossPoint(x1, y1, x2, y2) {
x1 = parseFloat(x1);
x2 = parseFloat(x2);
y1 = parseFloat(y1);
y2 = parseFloat(y2);
const x = Math.abs(x1 - x2);
const y = Math.abs(y1 - y2);
let x3 = x1 < x2 ? (x1 + x / 2) : (x1 - x / 2);
let y3 = y1 < y2 ? (y1 + y / 2) : (y1 - y / 2);
return [x3, y3];
}
function PB_OffsetPoint(x1, y1, x2, y2, offset) {
x1 = parseFloat(x1);
x2 = parseFloat(x2);
y1 = parseFloat(y1);
y2 = parseFloat(y2);
offset = parseFloat(offset);
const crossPoint = PB_CrossPoint(x1, y1, x2, y2);
const x3 = crossPoint[0];
const y3 = crossPoint[1];
const xLen = Math.abs(x1 - x2);
const yLen = Math.abs(y1 - y2);
const r = Math.sqrt(Math.pow(xLen, 2) + Math.pow(yLen, 2));
const xOffset = yLen / r * offset;
const yOffset = xLen / r * offset;
let newX = 0;
let newY = 0;
if ((x1 < x2 && y1 < y2) || (x1 > x2 && y1 > y2)) {
newX = x3 + xOffset;
newY = y3 - yOffset;
} else if ((x1 < x2 && y1 > y2) && (x1 > x2 && y1 < y2)) {
newX = x3 + xOffset;
newY = y3 + yOffset;
} else {
newX = x3 + xOffset;
newY = y3 + yOffset;
}
return [newX, newY];
}
function on_PB_CrossPoint() {
const x1 = document.getElementById('x1').value;
const y1 = document.getElementById('y1').value;
const x2 = document.getElementById('x2').value;
const y2 = document.getElementById('y2').value;
const rlt = PB_CrossPoint(parseFloat(x1), parseFloat(y1), parseFloat(x2), parseFloat(y2));
document.getElementById('result2').innerText = rlt.join(',');
}
function on_PB_OffsetPoint() {
const x1 = document.getElementById('x3').value;
const y1 = document.getElementById('y3').value;
const x2 = document.getElementById('x4').value;
const y2 = document.getElementById('y4').value;
const offset = document.getElementById('offset').value;
const crossPoint = PB_CrossPoint(x1, y1, x2, y2);
const offsetPoint = PB_OffsetPoint(x1, y1, x2, y2, offset);
const offsetPoint2 = PB_OffsetPoint(x1, y1, x2, y2, offset * 4 / 3);
document.getElementById('result3').innerText = crossPoint.join(',');
document.getElementById('result3').innerText += ' ~ ';
document.getElementById('result3').innerText += offsetPoint.join(',');
const latlngs1 = [
[x1, y1],
[x2, y2]
];
const polyline1 = L.polyline(latlngs1, {color: 'green'}).addTo(map);
const latlngs2 = [
[crossPoint[0], crossPoint[1]],
[offsetPoint[0], offsetPoint[1]]
];
L.polyline(latlngs2, {color: 'red'}).addTo(map);
const latlngs3 = [
'M',
[x1, y1],
'C',
[offsetPoint2[0], offsetPoint2[1]],
[offsetPoint2[0], offsetPoint2[1]],
[x2, y2]
];
L.curve(latlngs3, {color: 'yellow'}).addTo(map);
map.fitBounds(polyline1.getBounds());
}
function on_PB_OffsetPoint2() {
const x1 = document.getElementById('x5').value;
const y1 = document.getElementById('y5').value;
const x2 = document.getElementById('x6').value;
const y2 = document.getElementById('y6').value;
const offset = document.getElementById('offset2').value;
const crossPoint = PB_CrossPoint(x1, y1, x2, y2);
const offsetPoint = PB_OffsetPoint(x1, y1, x2, y2, offset);
const offsetPoint2 = PB_OffsetPoint(x1, y1, x2, y2, offset * 4 / 3);
document.getElementById('result4').innerText = crossPoint.join(',');
document.getElementById('result4').innerText += ' ~ ';
document.getElementById('result4').innerText += offsetPoint.join(',');
const latlngs1 = [
[x1, y1],
[x2, y2]
];
const polyline1 = L.polyline(latlngs1, {color: 'green'}).addTo(map);
const latlngs2 = [
[crossPoint[0], crossPoint[1]],
[offsetPoint[0], offsetPoint[1]]
];
L.polyline(latlngs2, {color: 'red'}).addTo(map);
const latlngs3 = [
'M',
[x1, y1],
'C',
[offsetPoint2[0], offsetPoint2[1]],
[offsetPoint2[0], offsetPoint2[1]],
[x2, y2]
];
L.curve(latlngs3, {color: 'yellow'}).addTo(map);
map.fitBounds(polyline1.getBounds());
}
</script>
</body>
</html>