Created
August 5, 2024 12:38
-
-
Save cedricbahirwe/201030ce3df47e9fd45c89c7f594116e to your computer and use it in GitHub Desktop.
This function creates a horizontal row of hexagons based on the specified width, height, number of hexagons, and spacing. Each hexagon is generated by the addHexagon(at:) helper function, which places it at the correct position with a flat-sided shape.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Creates a combined path of hexagons arranged horizontally with specified width, height, and spacing. | |
/// | |
/// - Parameters: | |
/// - width: The total width available for the hexagons. | |
/// - height: The height of each hexagon. | |
/// - count: The number of hexagons to include (default is 1). | |
/// - spacing: The spacing between adjacent hexagons (default is 0). | |
/// - Returns: A `UIBezierPath` object representing the combined hexagon path. | |
func createCombinedHexagonPath(width: CGFloat, height: CGFloat, count: Int = 1, spacing: CGFloat = 0) -> UIBezierPath { | |
let path = UIBezierPath() | |
let hexagonWidth = (width - spacing * CGFloat(count-1)) / CGFloat(count) // Width for each hexagon | |
// Helper function to create a single hexagon with flat sides | |
func addHexagon(at x: CGFloat) { | |
let pointHeight = height * 0.25 // Adjust this for pointiness | |
path.move(to: CGPoint(x: x, y: pointHeight)) | |
path.addLine(to: CGPoint(x: x, y: height - pointHeight)) | |
path.addLine(to: CGPoint(x: x + hexagonWidth * 0.5, y: height)) | |
path.addLine(to: CGPoint(x: x + hexagonWidth, y: height - pointHeight)) | |
path.addLine(to: CGPoint(x: x + hexagonWidth, y: pointHeight)) | |
path.addLine(to: CGPoint(x: x + hexagonWidth * 0.5, y: 0)) | |
path.close() | |
} | |
// Add all hexagons with spacing | |
for n in 0..<count { | |
addHexagon(at: (hexagonWidth + spacing) * CGFloat(n)) | |
} | |
return path | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment