this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

chore:cleaning the implemenatation up

+62 -82
+12 -69
src/main.zig
··· 1 1 const std = @import("std"); 2 - const zig_civ = @import("zig_civ"); 2 + const lib = @import("zig_civ"); 3 3 4 4 const rl = @import("raylib"); 5 5 6 - const Point = struct { x: f32, y: f32 }; 7 - const HexCell = struct { inner_radius: f32, outer_radius: f32, height: f32, width: f32, center: Point }; 8 - 9 - fn calculate_hex_corners(center: Point, size: f32, _i: usize) Point { 10 - const i: f32 = @floatFromInt(_i); 11 - const angle_deg = 60.0 * i; 12 - const angle_rad = std.math.pi / 180.0 * angle_deg; 13 - 14 - const x_offset: f32 = size * std.math.cos(angle_rad); 15 - const y_offset: f32 = size * std.math.sin(angle_rad); 16 - 17 - return Point{ .x = center.x + x_offset, .y = center.y + y_offset }; 18 - } 19 - 20 - fn draw_hex(hex: HexCell) void { 6 + fn draw_hex(hex: lib.HexCell) void { 21 7 //const center_x: i32 = @intFromFloat(@round(hex.center.x)); 22 8 //const center_y: i32 = @intFromFloat(@round(hex.center.y)); 23 9 //rl.drawCircle(center_x, center_y, hex.outer_radius, .green); 24 10 //rl.drawCircle(center_x, center_y, hex.inner_radius, .red); 25 11 26 - var corners: [6]Point = undefined; 12 + var corners: [6]lib.Point = undefined; 27 13 for (0..6) |i| { 28 - const corner = calculate_hex_corners(hex.center, hex.outer_radius, i); 14 + const corner = lib.calculate_hex_corners(hex.center, hex.outer_radius, i); 29 15 corners[i] = corner; 30 16 31 17 //const corner_y: i32 = @intFromFloat(@round(corner.y)); ··· 49 35 } 50 36 } 51 37 52 - const HexDirections = enum { top_left, top, top_right, bottom_right, bottom, bottom_left }; 53 - 54 - fn add_hex(base: HexCell, direction: HexDirections) HexCell { 55 - const x_sign: f32 = switch (direction) { 56 - .bottom_right => 1.0, 57 - .top_right => 1.0, 58 - .top => 0, 59 - .bottom => 0, 60 - .bottom_left => -1, 61 - .top_left => -1, 62 - }; 63 - 64 - const y_sign: f32 = switch (direction) { 65 - .top => -1, 66 - .top_left => -0.5, 67 - .top_right => -0.5, 68 - .bottom_left => 0.5, 69 - .bottom_right => 0.5, 70 - .bottom => 1, 71 - }; 72 - 73 - const x_offset: f32 = x_sign * 1.5 * base.outer_radius; 74 - const y_offset: f32 = y_sign * base.height; 75 - 76 - return new_hex(base.outer_radius, .{ 77 - .x = base.center.x + x_offset, 78 - .y = base.center.y + y_offset, 79 - }); 80 - } 81 - 82 - fn calculate_outer_radius(inner_radius: f32) f32 { 83 - return 2.0 * inner_radius / std.math.sqrt(3.0); 84 - } 85 - fn calculate_inner_radius(outer_radius: f32) f32 { 86 - return outer_radius / 2.0 * std.math.sqrt(3.0); 87 - } 88 - 89 - fn new_hex(outer_radius: f32, center: Point) HexCell { 90 - const size = outer_radius; 91 - const inner = calculate_inner_radius(size); 92 - return HexCell{ .inner_radius = inner, .outer_radius = size, .height = std.math.sqrt(3.0) * size, .width = 2 * size, .center = center }; 93 - } 94 - 95 38 pub fn main() anyerror!void { 96 39 // Initialization 97 40 //-------------------------------------------------------------------------------------- ··· 110 53 //---------------------------------------------------------------------------------- 111 54 // TODO: Update your variables here 112 55 //---------------------------------------------------------------------------------- 113 - const cell = new_hex(50.0, .{ 56 + const cell = lib.new_hex(50.0, .{ 114 57 .x = screenWidth / 2, 115 58 .y = screenHeight / 2, 116 59 }); 117 60 std.debug.print("cell : {}", .{cell}); 118 61 119 - const tl = add_hex(cell, .top_left); 120 - const tr = add_hex(cell, .top_right); 121 - const t = add_hex(cell, .top); 122 - const br = add_hex(cell, .bottom_right); 123 - const bl = add_hex(cell, .bottom_left); 124 - const b = add_hex(cell, .bottom); 62 + const tl = lib.add_hex(cell, .top_left); 63 + const tr = lib.add_hex(cell, .top_right); 64 + const t = lib.add_hex(cell, .top); 65 + const br = lib.add_hex(cell, .bottom_right); 66 + const bl = lib.add_hex(cell, .bottom_left); 67 + const b = lib.add_hex(cell, .bottom); 125 68 126 - const grid = [_]HexCell{ cell, tl, t, tr, br, b, bl }; 69 + const grid = [_]lib.HexCell{ cell, tl, t, tr, br, b, bl }; 127 70 128 71 // Draw 129 72 //----------------------------------------------------------------------------------
+50 -13
src/root.zig
··· 1 1 //! By convention, root.zig is the root source file when making a library. 2 2 const std = @import("std"); 3 3 4 - pub fn bufferedPrint() !void { 5 - // Stdout is for the actual output of your application, for example if you 6 - // are implementing gzip, then only the compressed bytes should be sent to 7 - // stdout, not any debugging messages. 8 - var stdout_buffer: [1024]u8 = undefined; 9 - var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer); 10 - const stdout = &stdout_writer.interface; 4 + const Point = struct { x: f32, y: f32 }; 5 + const HexCell = struct { inner_radius: f32, outer_radius: f32, height: f32, width: f32, center: Point }; 6 + const HexDirections = enum { top_left, top, top_right, bottom_right, bottom, bottom_left }; 7 + 8 + const PI = std.math.pi; 9 + 10 + fn calculate_hex_corners(center: Point, size: f32, _i: usize) Point { 11 + const i: f32 = @floatFromInt(_i); 12 + const angle_deg = 60.0 * i; 13 + const angle_rad = std.math.degreesToRadians(angle_deg); 14 + 15 + const x_offset: f32 = size * std.math.cos(angle_rad); 16 + const y_offset: f32 = size * std.math.sin(angle_rad); 17 + 18 + return Point{ .x = center.x + x_offset, .y = center.y + y_offset }; 19 + } 20 + 21 + fn add_hex(base: HexCell, direction: HexDirections) HexCell { 22 + const x_sign: f32 = switch (direction) { 23 + .bottom_right => 1.0, 24 + .top_right => 1.0, 25 + .top => 0, 26 + .bottom => 0, 27 + .bottom_left => -1, 28 + .top_left => -1, 29 + }; 30 + 31 + const y_sign: f32 = switch (direction) { 32 + .top => -1, 33 + .top_left => -0.5, 34 + .top_right => -0.5, 35 + .bottom_left => 0.5, 36 + .bottom_right => 0.5, 37 + .bottom => 1, 38 + }; 11 39 12 - try stdout.print("Run `zig build test` to run the tests.\n", .{}); 40 + const x_offset: f32 = x_sign * 1.5 * base.outer_radius; 41 + const y_offset: f32 = y_sign * base.height; 13 42 14 - try stdout.flush(); // Don't forget to flush! 43 + return new_hex(base.outer_radius, .{ 44 + .x = base.center.x + x_offset, 45 + .y = base.center.y + y_offset, 46 + }); 15 47 } 16 48 17 - pub fn add(a: i32, b: i32) i32 { 18 - return a + b; 49 + fn calculate_outer_radius(inner_radius: f32) f32 { 50 + return 2.0 * inner_radius / std.math.sqrt(3.0); 51 + } 52 + fn calculate_inner_radius(outer_radius: f32) f32 { 53 + return outer_radius / 2.0 * std.math.sqrt(3.0); 19 54 } 20 55 21 - test "basic add functionality" { 22 - try std.testing.expect(add(3, 7) == 10); 56 + fn new_hex(outer_radius: f32, center: Point) HexCell { 57 + const size = outer_radius; 58 + const inner = calculate_inner_radius(size); 59 + return HexCell{ .inner_radius = inner, .outer_radius = size, .height = std.math.sqrt(3.0) * size, .width = 2 * size, .center = center }; 23 60 }