this repo has no description
0
fork

Configure Feed

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

working on drawing multiple hexes

+52 -7
+52 -7
src/main.zig
··· 4 4 const rl = @import("raylib"); 5 5 6 6 const Point = struct { x: i32, y: i32 }; 7 - const HexCell = struct { inner_radius: f32, center: Point }; 7 + const HexCell = struct { inner_radius: f32, outer_radius: f32, height: f32, width: f32, center: Point }; 8 8 9 9 fn calculate_hex_corners(center: Point, size: f32, _i: usize) Point { 10 10 const i: f32 = @floatFromInt(_i); ··· 20 20 fn draw_hex(hex: HexCell) void { 21 21 var corners: [6]Point = undefined; 22 22 for (0..6) |i| { 23 - const corner = calculate_hex_corners(hex.center, hex.inner_radius, i); 23 + const corner = calculate_hex_corners(hex.center, hex.outer_radius, i); 24 24 corners[i] = corner; 25 25 rl.drawCircle(corner.x, corner.y, 5.0, .black); 26 26 } ··· 33 33 } 34 34 } 35 35 36 + const HexDirections = enum { top_left, top, top_right, bottom_right, bottom, bottom_left }; 37 + 38 + fn add_hex(base: HexCell, direction: HexDirections) HexCell { 39 + const x_offset: i32 = @intFromFloat(3 / 2 * base.outer_radius); 40 + const y_offset: i32 = @intFromFloat(std.math.sqrt(3) * base.outer_radius); 41 + 42 + const x_sign: i2 = switch (direction) { 43 + .bottom_right => 1, 44 + .top_right => 1, 45 + .top => 0, 46 + .bottom => 0, 47 + .bottom_left => -1, 48 + .top_left => -1, 49 + }; 50 + 51 + const y_sign: i2 = switch (direction) { 52 + .top => -1, 53 + .top_left => -1, 54 + .top_right => -1, 55 + .bottom => 1, 56 + .bottom_left => 1, 57 + .bottom_right => 1, 58 + }; 59 + 60 + return new_hex(base.inner_radius, .{ 61 + .x = base.center.x + x_sign * x_offset, 62 + .y = base.center.y + y_sign * y_offset, 63 + }); 64 + } 65 + 66 + fn calculate_outer_radius(inner_radius: f32) f32 { 67 + return 2.0 * inner_radius / std.math.sqrt(3); 68 + } 69 + 70 + fn new_hex(inner_radius: f32, center: Point) HexCell { 71 + const size = calculate_outer_radius(inner_radius); 72 + return HexCell{ .inner_radius = inner_radius, .outer_radius = size, .height = 2 * inner_radius, .width = 2 * size, .center = center }; 73 + } 74 + 36 75 pub fn main() anyerror!void { 37 76 // Initialization 38 77 //-------------------------------------------------------------------------------------- ··· 51 90 //---------------------------------------------------------------------------------- 52 91 // TODO: Update your variables here 53 92 //---------------------------------------------------------------------------------- 93 + const cell = new_hex(20.0, .{ 94 + .x = 200, 95 + .y = 200, 96 + }); 54 97 55 - const cell = HexCell{ .inner_radius = 50.0, .center = .{ 56 - .x = 100, 57 - .y = 100, 58 - } }; 98 + const tl = add_hex(cell, .top_left); 99 + const br = add_hex(cell, .bottom_right); 100 + 101 + const grid = [_]HexCell{ cell, tl, br }; 59 102 60 103 // Draw 61 104 //---------------------------------------------------------------------------------- ··· 64 107 65 108 rl.clearBackground(.white); 66 109 67 - draw_hex(cell); 110 + for (grid) |value| { 111 + draw_hex(value); 112 + } 68 113 69 114 //rl.drawText("Congrats! You created your first window!", 190, 200, 20, .light_gray); 70 115 //----------------------------------------------------------------------------------