Skip to content

Commit

Permalink
warnings and error for circum radius to polygon function
Browse files Browse the repository at this point in the history
  • Loading branch information
shreshthmohan committed Apr 5, 2022
1 parent 47450e2 commit 035a666
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 7 deletions.
7 changes: 4 additions & 3 deletions demo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ const d1 = roundedPolygonByCircumRadius({
rotate: 0,
cx: 120,
cy: 150,
borderRadius: 10,
borderRadius: 15,
})
const d2 = roundedPolygonBySideLength({
sideLength: 130,
sideCount: 3,
rotate: 30,
cx: 300,
cy: 150,
borderRadius: 10,
borderRadius: -17.5,
})

console.log({ d1 })
console.log({ d2 })

document.getElementById('polygon1').setAttribute('d', d1)
document.getElementById('polygon1').setAttribute('d', d1.d)
document.getElementById('polygon2').setAttribute('d', d2.d)
71 changes: 67 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function roundedPolygonBySideLength({
circumRadius: r,
angleIntendedBySide: alpha,
inRadius,
} = polygonSideToCircleRadius({ sideLength, sideCount })
} = polygonSidetoCircumRadius({ sideLength, sideCount })

const minSideLength = sideLengthFromInRadius({
inRadius: borderRadius,
Expand Down Expand Up @@ -127,7 +127,45 @@ export function roundedPolygonByCircumRadius({
cy?: number
rotate?: number
}) {
const alpha = angleIntendedByPolygonSide(sideCount) // in radians
const errors = []
const warnings = []

if (sideCount < 3) {
errors.push(
'sideCount cannot be smaller than 3. There is no polygon with fewer sides than a triangle. No fun shapes here. :) Sorry!',
)
}

if (errors.length) {
return { d: '', errors }
}
const {
sideLength,
inRadius,
angleIntendedBySide: alpha,
} = circumRadiusToPolygonSide({
sideCount,
circumRadius,
})

const minSideLength = sideLengthFromInRadius({
inRadius: borderRadius,
anglePerSide: alpha,
})
if (borderRadius > inRadius) {
warnings.push(
`borderRadius(${borderRadius}) is larger than inradius(${inRadius}) of the polygon. The resulting shape won't really be a polygon.
To get a proper curved polygon, either make the border radius smaller than ${inRadius} or make the sideLength larger than ${minSideLength}.
Ignore this warning if you intentionally want this curious pattern.
`,
)
}

if (borderRadius < 0) {
warnings.push(
'You provided a negative borderRadius. Might produce an unexpected shape that is not a polygon. Ignore this warning if this was intentional.',
)
}

const radiusOfInnerPolygon = circumRadius - borderRadius / Math.cos(alpha / 2)

Expand All @@ -145,7 +183,17 @@ export function roundedPolygonByCircumRadius({
})

const dForPath: string = pointsToDForPath({ allPoints, borderRadius, alpha })
return dForPath
return {
d: dForPath,
meta: {
circumRadius,
inRadius,
sideLength,
borderRadius,
minSideLength,
},
warnings,
}
}

// returns d attribute used in the SVG <path> element
Expand Down Expand Up @@ -216,7 +264,7 @@ function angleIntendedByPolygonSide(sideCount: number): number {
return (2 * PI) / sideCount
}

function polygonSideToCircleRadius({
function polygonSidetoCircumRadius({
sideLength,
sideCount,
}: {
Expand All @@ -235,6 +283,21 @@ function polygonSideToCircleRadius({
return { circumRadius, angleIntendedBySide, inRadius }
}

function circumRadiusToPolygonSide({
circumRadius,
sideCount,
}: {
circumRadius: number
sideCount: number
}): { sideLength: number; angleIntendedBySide: number; inRadius: number } {
const angleIntendedBySide = angleIntendedByPolygonSide(sideCount)

const sideLength = circumRadius * (2 * Math.sin(angleIntendedBySide / 2))

const inRadius = sideLength / (2 * Math.tan(angleIntendedBySide / 2))
return { sideLength, angleIntendedBySide, inRadius }
}

// Did not keep it in polar, because the calculation gets complicated
function addPolarPointVectorsAndConvertToCartesian(
p1: PolarPoint,
Expand Down

0 comments on commit 035a666

Please sign in to comment.