Created
June 5, 2025 12:03
-
-
Save ConnectedReasoning/29822fcd050791fd856ec4186ee4495c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // | |
| // Hexagon.swift | |
| // Planzu | |
| // | |
| // Created by Manuel Hernandez on 6/2/25. | |
| // | |
| import SwiftUI | |
| struct HexGridView: View { | |
| let colors: [Color] | |
| var body: some View { | |
| let rows = [ | |
| Array(colors.prefix(3)), | |
| Array(colors.dropFirst(3).prefix(4)), | |
| Array(colors.dropFirst(7).prefix(3)) | |
| ] | |
| return VStack(spacing: -8) { | |
| ForEach(rows.indices, id: \.self) { rowIndex in | |
| HStack(spacing: 5) { | |
| if rowIndex != 1 { | |
| Spacer().frame(width: 0) // offset first and last row | |
| } | |
| ForEach(rows[rowIndex].indices, id: \.self) { colIndex in | |
| let color = rows[rowIndex][colIndex] | |
| Hexagon() | |
| .fill(color) | |
| .overlay { | |
| Hexagon() | |
| .stroke(Color.gray.opacity(0.75)) | |
| } | |
| .frame(width: 36, height: 37) | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| struct Hexagon: Shape { | |
| func path(in rect: CGRect) -> Path { | |
| let width = rect.width | |
| let height = rect.height | |
| let spacing = width * 0.25 | |
| return Path { path in | |
| path.move(to: CGPoint(x: 0.5 * width, y: 0)) | |
| path.addLine(to: CGPoint(x: width, y: spacing)) | |
| path.addLine(to: CGPoint(x: width, y: height - spacing)) | |
| path.addLine(to: CGPoint(x: 0.5 * width, y: height)) | |
| path.addLine(to: CGPoint(x: 0, y: height - spacing)) | |
| path.addLine(to: CGPoint(x: 0, y: spacing)) | |
| path.closeSubpath() | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment