-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathdistance_dot.html
119 lines (102 loc) · 4.38 KB
/
distance_dot.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dot Product and Distance Relationship</title>
<style>
.vector {
margin: 20px;
}
.credit {
margin-top: 20px;
font-size: 14px;
text-align: center;
}
</style>
</head>
<body>
<div class="vector">
<label for="angleA">Angle of Vector A (degrees): </label>
<input type="range" id="angleA" min="0" max="360" value="0">
<span id="angleAValue">0</span>
</div>
<div class="vector">
<label for="angleB">Angle of Vector B (degrees): </label>
<input type="range" id="angleB" min="0" max="360" value="90">
<span id="angleBValue">90</span>
</div>
<div>
<p>Distance: <span id="distance"></span></p>
<p>Dot Product: <span id="dotProduct"></span></p>
<p>Polynomial Kernel: <span id="polyKernel"></span></p>
<p>RBF Kernel: <span id="rbfKernel"></span></p>
</div>
<canvas id="vectorCanvas" width="400" height="400" style="border:1px solid black;"></canvas>
<div class="credit">(c) Fayyaz Minhas</div>
<script>
const angleAInput = document.getElementById('angleA');
const angleBInput = document.getElementById('angleB');
const angleAValue = document.getElementById('angleAValue');
const angleBValue = document.getElementById('angleBValue');
const dotProductElement = document.getElementById('dotProduct');
const distanceElement = document.getElementById('distance');
const polyKernelElement = document.getElementById('polyKernel');
const rbfKernelElement = document.getElementById('rbfKernel');
const canvas = document.getElementById('vectorCanvas');
const ctx = canvas.getContext('2d');
function updateVectors() {
const angleA = parseInt(angleAInput.value);
const angleB = parseInt(angleBInput.value);
angleAValue.textContent = angleA;
angleBValue.textContent = angleB;
const radA = angleA * (Math.PI / 180);
const radB = angleB * (Math.PI / 180);
const vectorA = { x: Math.cos(radA), y: Math.sin(radA) };
const vectorB = { x: Math.cos(radB), y: Math.sin(radB) };
const dotProduct = vectorA.x * vectorB.x + vectorA.y * vectorB.y;
const distance = Math.sqrt((vectorA.x - vectorB.x) ** 2 + (vectorA.y - vectorB.y) ** 2);
const polyKernel = Math.pow(dotProduct, 2);
const rbfKernel = Math.exp(-Math.pow(distance, 2));
dotProductElement.textContent = dotProduct.toFixed(2);
distanceElement.textContent = distance.toFixed(2);
polyKernelElement.textContent = polyKernel.toFixed(2);
rbfKernelElement.textContent = rbfKernel.toFixed(2);
drawVectors(vectorA, vectorB);
}
function drawVectors(vectorA, vectorB) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw circle
ctx.beginPath();
ctx.arc(200, 200, 100, 0, 2 * Math.PI);
ctx.strokeStyle = 'black';
ctx.stroke();
// Draw vector A
ctx.beginPath();
ctx.moveTo(200, 200);
ctx.lineTo(200 + vectorA.x * 100, 200 - vectorA.y * 100);
ctx.strokeStyle = 'red';
ctx.stroke();
// Draw point for vector A
ctx.beginPath();
ctx.arc(200 + vectorA.x * 100, 200 - vectorA.y * 100, 5, 0, 2 * Math.PI);
ctx.fillStyle = 'red';
ctx.fill();
// Draw vector B
ctx.beginPath();
ctx.moveTo(200, 200);
ctx.lineTo(200 + vectorB.x * 100, 200 - vectorB.y * 100);
ctx.strokeStyle = 'blue';
ctx.stroke();
// Draw point for vector B
ctx.beginPath();
ctx.arc(200 + vectorB.x * 100, 200 - vectorB.y * 100, 5, 0, 2 * Math.PI);
ctx.fillStyle = 'blue';
ctx.fill();
}
angleAInput.addEventListener('input', updateVectors);
angleBInput.addEventListener('input', updateVectors);
updateVectors();
</script>
</body>
</html>