Skip to content

Commit 28249b0

Browse files
committed
チップを追加
1 parent d7e5933 commit 28249b0

4 files changed

Lines changed: 56 additions & 18 deletions

File tree

src/components/Trajectory/ResultCard.tsx

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,26 @@ import { calculate } from "./calculate";
55

66
export const ResultCard = ({ condition }: { condition: Condition }) => {
77
const [results, setResults] = useState<
8-
{ x_list: number[]; y_list: number[]; condition: Condition }[]
8+
{
9+
x_list: number[];
10+
y_list: number[];
11+
condition: Condition;
12+
finalVelocity: number;
13+
flightTime: number;
14+
}[]
915
>([]);
1016
useEffect(() => {
1117
if (!condition) return;
1218
setResults((prev) => {
1319
const exists = prev.some((r) => isSameCondition(r.condition, condition));
1420
if (exists) return prev;
1521
const res = calculate(condition);
22+
const finalVelocity = res.finalVelocity;
23+
const flightTime = res.flightTime;
1624
let len = res.x_list.length;
1725
res.x_list = res.x_list.filter((_, i) => i % Math.ceil(len / 500) === 0);
1826
res.y_list = res.y_list.filter((_, i) => i % Math.ceil(len / 500) === 0);
19-
return [...prev, { ...res, condition }];
27+
return [...prev, { ...res, condition, finalVelocity, flightTime }];
2028
});
2129
}, [condition]);
2230
return (
@@ -30,12 +38,12 @@ export const ResultCard = ({ condition }: { condition: Condition }) => {
3038
return (
3139
<li
3240
key={idx}
33-
className="rounded-full px-3 py-1 text-white flex items-center justify-center gap-2 hover:shadow-lg hover:opacity-80 transition-all duration-200 ease-in-out"
41+
className="relative group rounded-full px-3 py-1 text-white flex items-center justify-center gap-2 hover:shadow-lg hover:opacity-80 transition-all duration-200 ease-in-out"
3442
style={{ backgroundColor: color }}
3543
>
3644
<div>{idx}</div>
3745
<div
38-
className="bg-white w-3 h-3 rounded-full flex items-center justify-center font-bold"
46+
className="bg-white w-3 h-3 rounded-full flex items-center justify-center font-bold cursor-pointer"
3947
style={{ color: color }}
4048
onClick={() => {
4149
const newResults = results.filter((_, i) => i !== idx);
@@ -44,6 +52,21 @@ export const ResultCard = ({ condition }: { condition: Condition }) => {
4452
>
4553
×
4654
</div>
55+
56+
{/* Tooltip: shows condition on parent hover */}
57+
<div className="absolute top-full left-[200%] mb-2 transform -translate-x-1/2 w-56 p-2 rounded shadow bg-white text-neutral-800 text-xs opacity-0 scale-95 pointer-events-none group-hover:opacity-100 group-hover:scale-100 transition-all duration-200 z-10">
58+
<div className="font-semibold">Condition</div>
59+
<div>初速: {res.condition.initV} m/s</div>
60+
<div>角度: {res.condition.angle}°</div>
61+
<div>発射高さ: {res.condition.yoffset} m</div>
62+
<div>飛翔体半径: {res.condition.radius} m</div>
63+
<div>質量: {res.condition.mass.toFixed(4)} kg</div>
64+
<div>Δt: {res.condition.deltaT}</div>
65+
<div>飛距離: {Math.max(...res.x_list).toFixed(4)} m</div>
66+
<div>最高高度: {Math.max(...res.y_list).toFixed(4)} m</div>
67+
<div>到達時間: {res.flightTime.toFixed(4)} s</div>
68+
<div>着弾速度: {res.finalVelocity.toFixed(2)} m/s</div>
69+
</div>
4770
</li>
4871
);
4972
})}

src/components/Trajectory/calculate.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const calculate = (condition: Condition) => {
1717
const x_list = [x];
1818
const y_list = [y];
1919

20-
while (y >= 0) {
20+
while (y >= 0 && x_list.length < 100000) {
2121
// 空気抵抗の計算
2222
const v = Math.sqrt(vx * vx + vy * vy);
2323

@@ -27,11 +27,17 @@ export const calculate = (condition: Condition) => {
2727
condition.radius * 2,
2828
condition.viscosity
2929
);
30-
const cd = dragCoefficient(re);
31-
const area = Math.PI * condition.radius * condition.radius;
3230

33-
const dragForceX = 0.5 * condition.fluidDensity * v * vx * cd * area;
34-
const dragForceY = 0.5 * condition.fluidDensity * v * vy * cd * area;
31+
let dragForceX = 0;
32+
let dragForceY = 0;
33+
34+
if (re > 0) {
35+
const cd = dragCoefficient(re);
36+
const area = Math.PI * condition.radius * condition.radius;
37+
38+
dragForceX = 0.5 * condition.fluidDensity * v * vx * cd * area;
39+
dragForceY = 0.5 * condition.fluidDensity * v * vy * cd * area;
40+
}
3541

3642
// 加速度の計算
3743
const ax = -dragForceX / condition.mass;
@@ -50,5 +56,10 @@ export const calculate = (condition: Condition) => {
5056
y_list.push(y);
5157
}
5258

53-
return { x_list, y_list };
59+
return {
60+
x_list,
61+
y_list,
62+
finalVelocity: Math.sqrt(vx * vx + vy * vy),
63+
flightTime: (x_list.length - 1) * deltaT,
64+
};
5465
};

src/components/Trajectory/index.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,17 @@ import { ResultCard } from "./ResultCard";
66
export const TrajectoryCalculator = () => {
77
const [condition, setCondition] = useState<Condition | undefined>(undefined);
88
return (
9-
<div className="flex flex-col gap-4">
10-
<InputCard setCondition={setCondition} />
11-
<ResultCard condition={condition} />
9+
<div className="flex flex-col gap-4 w-full md:flex-row-reverse">
10+
<div className="flex flex-col gap-4 w-full md:w-1/3">
11+
<div className="text-center p-8 bg-white rounded-xl shadow-lg">
12+
<h1 className="text-4xl font-bold">簡易弾道計算機</h1>
13+
<p className="text-sm text-neutral-500">ティータイムのおともに。</p>
14+
</div>
15+
<InputCard setCondition={setCondition} />
16+
</div>
17+
<div className="w-full md:w-2/3">
18+
<ResultCard condition={condition} />
19+
</div>
1220
</div>
1321
);
1422
};

src/pages/trajectory.astro

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ import "../styles/global.css";
1313
image=""
1414
>
1515
<div class="min-h-screen w-dvw bg-neutral-100 text-neutral-800">
16-
<div class="min-h-screen w-1/2 mx-auto py-16">
17-
<div class="text-center p-8 mb-4 bg-white rounded-xl shadow-lg">
18-
<h1 class="text-4xl font-bold">簡易弾道計算機</h1>
19-
<p class="text-sm text-neutral-500">ティータイムのおともに。</p>
20-
</div>
16+
<div class="w-full mx-auto p-4">
2117
<TrajectoryCalculator client:load />
2218
</div>
2319
</div>

0 commit comments

Comments
 (0)