Skip to content

Instantly share code, notes, and snippets.

@webserveis
Created June 17, 2024 10:42
Show Gist options
  • Save webserveis/6af6e131b2ccbbdec0de3616e5237c5d to your computer and use it in GitHub Desktop.
Save webserveis/6af6e131b2ccbbdec0de3616e5237c5d to your computer and use it in GitHub Desktop.
struct RoundedTrapezoid: Shape {
var depth: CGFloat
var edge: Edge
var cornerRadius: CGFloat = 0
var isRightAngle: Edge? = .none
func path(in rect: CGRect) -> Path {
var path = Path()
// Definimos los puntos base del rectángulo
let topLeft = CGPoint(x: rect.minX, y: rect.minY)
let topRight = CGPoint(x: rect.maxX, y: rect.minY)
let bottomRight = CGPoint(x: rect.maxX, y: rect.maxY)
let bottomLeft = CGPoint(x: rect.minX, y: rect.maxY)
// Ajustamos los puntos en función del edge y depth
var adjustedTopLeft = topLeft
var adjustedTopRight = topRight
var adjustedBottomLeft = bottomLeft
var adjustedBottomRight = bottomRight
switch edge {
case .top:
adjustedTopLeft.x += depth
adjustedTopRight.x -= depth
case .bottom:
adjustedBottomLeft.x += depth
adjustedBottomRight.x -= depth
case .leading:
adjustedTopLeft.y += depth
adjustedBottomLeft.y -= depth
case .trailing:
adjustedTopRight.y += depth
adjustedBottomRight.y -= depth
}
// Movemos el punto inicial del path al topLeft ajustado
path.move(to: CGPoint(x: adjustedTopLeft.x + cornerRadius, y: adjustedTopLeft.y))
// Top side
path.addLine(to: CGPoint(x: adjustedTopRight.x - cornerRadius, y: adjustedTopRight.y))
path.addArc(tangent1End: adjustedTopRight, tangent2End: CGPoint(x: adjustedTopRight.x, y: adjustedTopRight.y + cornerRadius), radius: cornerRadius)
// Right side
path.addLine(to: CGPoint(x: adjustedBottomRight.x, y: adjustedBottomRight.y - cornerRadius))
path.addArc(tangent1End: adjustedBottomRight, tangent2End: CGPoint(x: adjustedBottomRight.x - cornerRadius, y: adjustedBottomRight.y), radius: cornerRadius)
// Bottom side
path.addLine(to: CGPoint(x: adjustedBottomLeft.x + cornerRadius, y: adjustedBottomLeft.y))
path.addArc(tangent1End: adjustedBottomLeft, tangent2End: CGPoint(x: adjustedBottomLeft.x, y: adjustedBottomLeft.y - cornerRadius), radius: cornerRadius)
// Left side
path.addLine(to: CGPoint(x: adjustedTopLeft.x, y: adjustedTopLeft.y + cornerRadius))
path.addArc(tangent1End: adjustedTopLeft, tangent2End: CGPoint(x: adjustedTopLeft.x + cornerRadius, y: adjustedTopLeft.y), radius: cornerRadius)
// Cerramos el path
path.closeSubpath()
return path
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment