CopperHat is an Android application that showcases a visually captivating 3D point cloud visualization, rendered dynamically using Jetpack Compose's Canvas API. This project demonstrates how to leverage mathematical functions and modern Android development practices to create unique and interactive graphics within your apps.
- Dynamic Point Cloud: The application generates a complex point cloud, creating a 3D-like effect on a 2D canvas.
- Mathematical Foundation: Utilizes trigonometric functions (sine, cosine) and π (pi) to calculate point positions, forming an intricate pattern.
- Jetpack Compose: Built with the declarative UI toolkit, Jetpack Compose, for a modern and efficient UI.
- Canvas API: Leverages Compose's
Canvasto draw directly on the screen, providing full control over rendering. - Code Clarity: The code is written with clear variable names, constants, and comments.
- Efficient rendering: The code uses sequences, making the generation of points more efficient.
- Android Studio (latest version recommended)
- Android SDK (API level 26 or higher)
- Kotlin (latest version recommended)
- Clone the Repository:
- Open in Android Studio:
- Launch Android Studio.
- Select "Open an Existing Project."
- Navigate to the cloned repository and select the root directory.
- Build and Run:
- Click the "Sync Project with Gradle Files" button.
- Select your target device/emulator.
- Click the "Run" button.
The CopperHat composable function is the core of the visualization. Here's a high-level overview of its logic:
- Canvas Setup: A
Canvascomposable is created to provide a drawing surface. - Constants:
degreesToRadians: A constant to convert degrees to radiansrotationAngleRadians: The angle of rotation, in radianscenterX: the center x of the canvascenterY: the center y of the canvasmaxRadius: The maximum radius used to generate points
- Point Generation (Sequence):
- A
Sequenceis used to generateOffsetobjects lazily. This means points are computed only when needed, optimizing memory usage. - Nested loops iterate through a range of radii (
radius) and angles (theta) x,y, andzare calculated using trigonometric functions, defining the 3D-like coordinates.projectedXandprojectedYtransform the 3D coordinates into 2D screen positions.yield()is used to emit eachOffsetobject into the sequence.
- A
- Drawing:
drawPoints()is called with the generated list ofOffsetto render the point cloud on theCanvas.drawPoints()is an API from the Canvas, used to draw points.- PointMode.Points: Defines that each point will be drawn as an individual point.
- Color.White: Defines the color of the points.
- strokeWidth = 1f: Defines the width of each point.
- Interactivity: Add touch/drag gestures to rotate or zoom the point cloud.
- Color Dynamics: Experiment with changing point colors based on their distance from the center or other criteria.
- Parameterization: Make the point density, rotation angle, and other visual aspects configurable.
- Performance: Explore using
Pathinstead of individual points to draw for improved rendering. - More Complex Patterns: Implement different trigonometric equations to generate more complex patterns.
- Use of the Canvas Size: Adapt the generated points depending on the Canvas size.
Copper_Hat/
├── app/
│
└── src/
│
└── main/
│
├── kotlin/
│ │
└── sco/
│ │
└── carlukesoftware/
│ │
└── copperhat/
│ │
├── MainActivity.kt # Entry point of the app
│ │
└── ui/
│ │
└── components/
│ │
└── CopperHat.kt # The CopperHat Composable
│ │
└── theme/
│ │
└── ... # Compose theming
│
└── res/ # Resources
│
└── AndroidManifest.xml # App Manifest
│ └── ... # other files
├── build.gradle.kts
└── ...
Enjoy exploring the fascinating world of 3D graphics with Jetpack Compose!
Contributions are welcome! If you find a bug or have an idea for a new feature, please open an issue or submit a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.
Donald McCaskey - forteanjo@sky.com
Project Link: https://github.com/forteanjo/copperhat