From c70948471af61e5513b07a65ac79dca56c56b8b5 Mon Sep 17 00:00:00 2001 From: Coder-Blue Date: Mon, 15 Jun 2026 23:33:49 +0700 Subject: [PATCH 1/2] feat(cube): its spinning --- src/main.zig | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 114 insertions(+), 2 deletions(-) diff --git a/src/main.zig b/src/main.zig index 5a503b9..7e6973c 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,5 +1,117 @@ const std = @import("std"); -pub fn main() void { - std.debug.print("Hello, {s}!\n", .{"World"}); +const width: usize = 160; +const height: usize = 44; +const background_ascii_code: u8 = ' '; + +const distance_from_cam: f32 = 100.0; +const K1: f32 = 40.0; +const increment_speed: f32 = 0.6; + +const Renderer = struct { + a: f32 = 0.0, + b: f32 = 0.0, + c: f32 = 0.0, + + cube_width: f32 = undefined, + horizontal_offset: f32 = undefined, + + z_buffer: [width * height]f32 = undefined, + buffer: [width * height]u8 = undefined, + + fn calculate_x(self: *const Renderer, i: f32, j: f32, k: f32) f32 { + return j * @sin(self.a) * @sin(self.b) * @cos(self.c) - k * @cos(self.a) * @sin(self.b) * @cos(self.c) + j * @sin(self.a) * @sin(self.c) + k * @sin(self.a) * @sin(self.c) + i * @cos(self.b) * @cos(self.c); + } + + fn calculate_y(self: *const Renderer, i: f32, j: f32, k: f32) f32 { + return j * @cos(self.a) * @cos(self.c) + k * @sin(self.a) * @cos(self.c) - j * @sin(self.a) * @sin(self.b) * @sin(self.c) + k * @cos(self.a) * @sin(self.b) * @sin(self.c) - i * @cos(self.b) * @sin(self.c); + } + + fn calculate_z(self: *const Renderer, i: f32, j: f32, k: f32) f32 { + return k * @cos(self.a) * @cos(self.b) - j * @sin(self.a) * @cos(self.b) + i * @sin(self.b); + } + + fn calculate_for_surface( + self: *Renderer, + cube_x: f32, + cube_y: f32, + cube_z: f32, + ch: u8, + ) void { + const x = self.calculate_x(cube_x, cube_y, cube_z); + const y = self.calculate_y(cube_x, cube_y, cube_z); + const z = self.calculate_z(cube_x, cube_y, cube_z) + distance_from_cam; + + const ooz = 1.0 / z; + + const width_f = @as(f32, @floatFromInt(width)); + const height_f = @as(f32, @floatFromInt(height)); + + const xp: i32 = @intFromFloat(width_f / 2.0 + self.horizontal_offset + K1 * ooz * x * 2); + const yp: i32 = @intFromFloat(height_f / 2.0 + K1 * ooz * y); + + if (xp >= 0 and xp < width and yp >= 0 and yp < height) { + const idx: usize = @intCast(xp + yp * @as(i32, @intCast(width))); + + if (ooz > self.z_buffer[idx]) { + self.z_buffer[idx] = ooz; + self.buffer[idx] = ch; + } + } + } +}; + +pub fn main(init: std.process.Init) !void { + const io = init.io; + + var stdout_buffer: [8192]u8 = undefined; + var stdout_file_writer: std.Io.File.Writer = .init(.stdout(), io, &stdout_buffer); + const stdout_writer = &stdout_file_writer.interface; + + try stdout_writer.print("\x1b[2J", .{}); + try stdout_writer.flush(); + + var renderer = Renderer{}; + + while (true) { + @memset(&renderer.buffer, background_ascii_code); + @memset(&renderer.z_buffer, 0.0); + + renderer.cube_width = 20.0; + renderer.horizontal_offset = -2.0 * renderer.cube_width; + + var cube_x = -renderer.cube_width; + + while (cube_x < renderer.cube_width) : (cube_x += increment_speed) { + var cube_y = -renderer.cube_width; + + while (cube_y < renderer.cube_width) : (cube_y += increment_speed) { + renderer.calculate_for_surface(cube_x, cube_y, -renderer.cube_width, '@'); + renderer.calculate_for_surface(renderer.cube_width, cube_y, cube_y, '$'); + renderer.calculate_for_surface(-renderer.cube_width, cube_y, -cube_x, '~'); + + renderer.calculate_for_surface(-cube_x, cube_y, renderer.cube_width, '#'); + renderer.calculate_for_surface(cube_x, -renderer.cube_width, -cube_y, ';'); + renderer.calculate_for_surface(cube_x, renderer.cube_width, cube_y, '+'); + } + } + + try stdout_writer.print("\x1b[H", .{}); + var y: usize = 0; + while (y < height) : (y += 1) { + const row_start = y * width; + const row_end = row_start + width; + + try stdout_writer.print("{s}\n", .{renderer.buffer[row_start..row_end]}); + } + + try stdout_writer.flush(); + + renderer.a += 0.015; + renderer.b += 0.015; + renderer.c += 0.003; + + const duration = std.Io.Duration.fromMilliseconds(16); + try io.sleep(duration, .real); + } } From 368915aacc192346f13746f1dadbb536023eb343 Mon Sep 17 00:00:00 2001 From: Coder-Blue Date: Mon, 15 Jun 2026 23:41:08 +0700 Subject: [PATCH 2/2] feat(docs): for future improvement --- .github/CODE_OF_CONDUCT.md | 77 ++++++++ .github/COMMIT_CONVENTION.md | 85 +++++++++ .github/CONTRIBUTING.md | 170 ++++++++++++++++++ .github/ISSUE_TEMPLATE/bug_report.yml | 68 +++++++ .github/ISSUE_TEMPLATE/feature_request.yml | 49 +++++ .../ISSUE_TEMPLATE/security_vulnerability.yml | 96 ++++++++++ .github/PULL_REQUEST_TEMPLATE.md | 18 ++ 7 files changed, 563 insertions(+) create mode 100644 .github/CODE_OF_CONDUCT.md create mode 100644 .github/COMMIT_CONVENTION.md create mode 100644 .github/CONTRIBUTING.md create mode 100644 .github/ISSUE_TEMPLATE/bug_report.yml create mode 100644 .github/ISSUE_TEMPLATE/feature_request.yml create mode 100644 .github/ISSUE_TEMPLATE/security_vulnerability.yml create mode 100644 .github/PULL_REQUEST_TEMPLATE.md diff --git a/.github/CODE_OF_CONDUCT.md b/.github/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..3cd65e8 --- /dev/null +++ b/.github/CODE_OF_CONDUCT.md @@ -0,0 +1,77 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community. + +## Our Standards + +Examples of behavior that contributes to a positive environment for our community include: + +- Demonstrating empathy and kindness toward other people +- Being respectful of differing opinions, viewpoints, and experiences +- Giving and gracefully accepting constructive feedback +- Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience +- Focusing on what is best not just for us as individuals, but for the overall community + +Examples of unacceptable behavior include: + +- The use of sexualized language or imagery, and sexual attention or advances of any kind +- Trolling, insulting or derogatory comments, and personal or political attacks +- Public or private harassment +- Publishing others' private information, such as a physical or email address, without their explicit permission +- Other conduct which could reasonably be considered inappropriate in a professional setting + +## Enforcement Responsibilities + +Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful. + +Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate. + +## Scope + +This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at [trananhquan1009@gmail.com](mailto:trananhquan1009@gmail.com). All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the reporter of any incident. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested. + +### 2. Warning + +**Community Impact**: A violation through a single incident or series of actions. + +**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within the community. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.1, available at [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html](https://www.contributor-covenant.org/version/2/1/code_of_conduct.html). + +Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity). + +[homepage]: https://www.contributor-covenant.org diff --git a/.github/COMMIT_CONVENTION.md b/.github/COMMIT_CONVENTION.md new file mode 100644 index 0000000..8a65481 --- /dev/null +++ b/.github/COMMIT_CONVENTION.md @@ -0,0 +1,85 @@ +## Git Commit Message Convention + +> This is adapted from [Angular's commit convention](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-changelog-angular). + +### Examples + +Appears under "Features" header, pencil subheader: + +``` +feat(pencil): add 'graphiteWidth' option +``` + +Appears under "Bug Fixes" header, graphite subheader, with a link to issue #28: + +``` +fix(graphite): stop graphite breaking when width < 0.1 + +Closes #28 +``` + +Appears under "Performance Improvements" header, and under "Breaking Changes" with the breaking change explanation: + +``` +perf(pencil): remove graphiteWidth option + +BREAKING CHANGE: The graphiteWidth option has been removed. The default graphite width of 10mm is always used for performance reason. +``` + +The following commit and commit `667ecc1` do not appear in the changelog if they are under the same release. If not, the revert commit appears under the "Reverts" header. + +``` +revert: feat(pencil): add 'graphiteWidth' option + +This reverts commit 667ecc1654a317a13331b17617d973392f415f02. +``` + +### Commit Message Format + +A commit message consists of a **header**, **body** and **footer**. The header has a **type**, **scope** and **subject**: + +``` +(): + + + +