-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
100 lines (72 loc) · 3 KB
/
Copy pathserver.js
File metadata and controls
100 lines (72 loc) · 3 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
const express = require("express");
const path = require("path");
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
app.use(express.static(path.join(__dirname, "public")));
const toRadians = (degrees) => (degrees * Math.PI) / 180;
function calculateAngleDegrees(xComponent, yComponent) {
if (xComponent === 0 && yComponent > 0) {
return 90;
}
if (xComponent === 0 && yComponent < 0) {
return 270;
}
if (xComponent < 0) {
return (Math.atan(yComponent / xComponent) * 180) / Math.PI + 180;
}
if (yComponent < 0 && xComponent >= 0) {
return (Math.atan(yComponent / xComponent) * 180) / Math.PI + 360;
}
return (Math.atan(yComponent / xComponent) * 180) / Math.PI;
}
function isValidNumber(value) {
return typeof value === "number" && Number.isFinite(value);
}
app.post("/api/vector-components", (request, response) => {
const { magnitude, angleDegrees } = request.body || {};
if (!isValidNumber(magnitude) || magnitude < 0 || !isValidNumber(angleDegrees)) {
return response.status(400).json({ error: "Provide a non-negative magnitude and a valid angle." });
}
const xComponent = Math.cos(toRadians(angleDegrees)) * magnitude;
const yComponent = Math.sin(toRadians(angleDegrees)) * magnitude;
return response.json({ xComponent, yComponent });
});
app.post("/api/vector-from-components", (request, response) => {
const { xComponent, yComponent } = request.body || {};
if (!isValidNumber(xComponent) || !isValidNumber(yComponent)) {
return response.status(400).json({ error: "Provide numeric values for both components." });
}
const magnitude = Math.sqrt(xComponent ** 2 + yComponent ** 2);
const angleDegrees = magnitude === 0 ? 0 : calculateAngleDegrees(xComponent, yComponent);
return response.json({ magnitude, angleDegrees });
});
app.post("/api/vector-sum", (request, response) => {
const { vectors } = request.body || {};
if (!Array.isArray(vectors) || vectors.length === 0) {
return response.status(400).json({ error: "Submit at least one vector to add." });
}
let totalX = 0;
let totalY = 0;
for (const vector of vectors) {
const { magnitude, angleDegrees } = vector || {};
if (!isValidNumber(magnitude) || magnitude < 0 || !isValidNumber(angleDegrees)) {
return response
.status(400)
.json({ error: "Each vector needs a non-negative magnitude and a valid angle." });
}
const xComponent = Math.cos(toRadians(angleDegrees)) * magnitude;
const yComponent = Math.sin(toRadians(angleDegrees)) * magnitude;
totalX += xComponent;
totalY += yComponent;
}
const magnitude = Math.sqrt(totalX ** 2 + totalY ** 2);
const angleDegrees = magnitude === 0 ? 0 : calculateAngleDegrees(totalX, totalY);
return response.json({ totalX, totalY, magnitude, angleDegrees });
});
app.get("*", (request, response) => {
response.sendFile(path.join(__dirname, "public", "index.html"));
});
app.listen(PORT, () => {
console.log(`Physics Assistant server running on port ${PORT}`);
});