Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:degree to radian conversion #73

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions lib/jscad-fns/rotate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ export type RotateProps = {
children: React.ReactNode
}

const convertToDegrees = (value: string | number): number => {
const convertToRadians = (value: string | number): number => {
if (typeof value === "string") {
const numericValue = value.replace(/[^\d.-]/g, "")
const parsedValue = parseFloat(numericValue)
if (!isNaN(parsedValue)) {
if (value.toLowerCase().includes("deg")) {
return (parsedValue * Math.PI) / 180
}
return parsedValue
}
throw new Error(`Invalid rotation value: ${value}`)
Expand All @@ -33,15 +36,15 @@ const RotateBase = ({ rotation, angles, children }: RotateProps) => {
value: RotateProps["rotation"] | RotateProps["angles"],
): [number, number, number] => {
if (typeof value === "string" || typeof value === "number") {
const angle = convertToDegrees(value)
const angle = convertToRadians(value)
return [0, 0, angle]
} else if (Array.isArray(value)) {
return value.map(convertToDegrees) as [number, number, number]
return value.map(convertToRadians) as [number, number, number]
} else if (value && typeof value === "object") {
return [
convertToDegrees(value.x),
convertToDegrees(value.y),
convertToDegrees(value.z),
convertToRadians(value.x),
convertToRadians(value.y),
convertToRadians(value.z),
]
}
return [0, 0, 0]
Expand Down
Loading