/
TrigonometricTool.tsx
188 lines (175 loc) · 5.23 KB
/
TrigonometricTool.tsx
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
import {
Canvas,
Circle,
DashPathEffect,
Path,
} from "@shopify/react-native-skia"
import { useCallback, useMemo, useState } from "react"
import { StyleSheet, Text, TextInput, View } from "react-native"
const canvasHeight = 400
export default function TrigonometricTool() {
const [toolWidth, setToolWidth] = useState(340)
const center = { x: toolWidth / 2, y: 200 }
const [radius, setRadius] = useState<number | null>(100)
const [degree, setDegree] = useState<number | null>(60)
const actualRadius = radius * 1.3
const lineEndPos = useMemo(() => {
const sin = actualRadius * Math.sin((degree * Math.PI) / 180)
const cos = actualRadius * Math.cos((degree * Math.PI) / 180)
return { x: center.x + cos, y: center.y - sin }
}, [degree, actualRadius, center])
const { sin, cos } = useMemo(() => {
const sin = radius * Math.sin((degree * Math.PI) / 180)
const cos = actualRadius * Math.cos((degree * Math.PI) / 180)
return { sin: sin.toFixed(3), cos: cos.toFixed(3) }
}, [radius, degree])
const xyLineProps = {
color: "rgb(150, 150, 150)",
style: "stroke" as const,
strokeWidth: 0.5,
}
const supportLineProps = {
color: "lightblue",
style: "stroke" as const,
strokeWidth: 1.5,
}
return (
<View style={{ width: toolWidth }}>
<Canvas style={[styles.canvas]}>
{/* X, Y軸 */}
<Path
{...xyLineProps}
path={`M 0 ${center.y} L ${toolWidth} ${center.y}`}
>
<DashPathEffect intervals={[3, 3]} />
</Path>
<Path
{...xyLineProps}
path={`M ${toolWidth / 2} 0 L ${toolWidth / 2} ${canvasHeight}`}
>
<DashPathEffect intervals={[3, 3]} />
</Path>
{/* 中心から伸びる線 (角度により変わる) */}
<Path
color="lightblue"
style="stroke"
strokeWidth={2}
path={`M ${center.x} ${center.y} L ${lineEndPos.x} ${lineEndPos.y}`}
/>
{/* sinとcosの補助線 */}
<Path
{...supportLineProps}
path={`M ${lineEndPos.x} ${lineEndPos.y} ${lineEndPos.x} ${center.y}`}
/>
<Path
{...supportLineProps}
path={`M ${lineEndPos.x} ${lineEndPos.y} ${center.x} ${lineEndPos.y}`}
/>
<Circle
cx={center.x}
cy={center.y}
r={actualRadius}
style="stroke"
strokeWidth={2}
color="lightblue"
/>
</Canvas>
<View style={styles.infoPanel}>
<View style={styles.infoRow}>
<Text>sinθ:</Text>
<Text>{sin}</Text>
</View>
<View style={styles.infoRow}>
<Text>cosθ:</Text>
<Text>{cos}</Text>
</View>
<View style={styles.infoRow}>
<Text>ラジアン:</Text>
<Text>
{((degree * Math.PI) / 180).toFixed(3)} (
{((degree * Math.PI) / 180 / Math.PI).toFixed(3)}π)
</Text>
</View>
</View>
<View style={styles.settings}>
<View style={styles.settingRow}>
<Text>半径(1 - 100):</Text>
<View style={{ flexDirection: "row", alignItems: "center" }}>
<TextInput
style={styles.textInput}
keyboardType="numeric"
value={radius?.toString() || ""}
onChangeText={useCallback(s => {
if (s.trim() === "") {
setRadius(null)
return
}
try {
const i = parseInt(s, 10)
if (i > 100 || i < 1) return
setRadius(i)
} catch (e) {}
}, [])}
/>
<Text style={{ marginLeft: 4, width: 14 }}></Text>
</View>
</View>
<View style={styles.settingRow}>
<Text>角度(0 - 360):</Text>
<View style={{ flexDirection: "row", alignItems: "center" }}>
<TextInput
style={styles.textInput}
keyboardType="numeric"
value={degree?.toString() || ""}
onChangeText={useCallback(s => {
if (s.trim() === "") {
setDegree(null)
return
}
try {
const i = parseInt(s, 10)
if (i > 360 || i < 1) return
setDegree(i)
} catch (e) {}
}, [])}
/>
<Text style={{ marginLeft: 4 }}>度</Text>
</View>
</View>
</View>
</View>
)
}
const styles = StyleSheet.create({
canvas: { height: canvasHeight },
infoPanel: {
marginTop: 24,
marginHorizontal: 8,
borderWidth: 1,
borderRadius: 8,
borderColor: "lightgray",
padding: 16,
},
infoRow: {
flexDirection: "row",
alignItems: "center",
justifyContent: "space-between",
paddingVertical: 8,
},
settings: { marginTop: 24, paddingHorizontal: 16 },
settingRow: {
flexDirection: "row",
alignItems: "center",
justifyContent: "space-between",
paddingVertical: 4,
},
textInput: {
width: 80,
height: 40,
borderWidth: 1,
borderColor: "lightgray",
borderRadius: 4,
textAlign: "right",
paddingHorizontal: 8,
},
})