this repo has no description
0
fork

Configure Feed

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

fiddling around with next hext formula

+40 -27
+40 -27
src/main.zig
··· 3 3 4 4 const rl = @import("raylib"); 5 5 6 - const Point = struct { x: i32, y: i32 }; 6 + const Point = struct { x: f32, y: f32 }; 7 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); 11 - const angle_deg = 60 * i; 11 + const angle_deg = 60.0 * i; 12 12 const angle_rad = std.math.degreesToRadians(angle_deg); 13 13 14 - const x_offset: i32 = @intFromFloat(size * std.math.cos(angle_rad)); 15 - const y_offset: i32 = @intFromFloat(size * std.math.sin(angle_rad)); 14 + const x_offset: f32 = size * std.math.cos(angle_rad); 15 + const y_offset: f32 = size * std.math.sin(angle_rad); 16 16 17 17 return Point{ .x = center.x + x_offset, .y = center.y + y_offset }; 18 18 } ··· 22 22 for (0..6) |i| { 23 23 const corner = calculate_hex_corners(hex.center, hex.outer_radius, i); 24 24 corners[i] = corner; 25 - rl.drawCircle(corner.x, corner.y, 5.0, .black); 25 + //rl.drawCircle(corner.x, corner.y, 5.0, .black); 26 26 } 27 27 28 28 for (0..6) |i| { 29 29 const start = corners[i]; 30 30 const end = corners[@mod(i + 1, corners.len)]; 31 31 32 - rl.drawLine(start.x, start.y, end.x, end.y, .black); 32 + const start_x: i32 = @intFromFloat(@floor(start.x)); 33 + const start_y: i32 = @intFromFloat(@floor(start.y)); 34 + const end_x: i32 = @intFromFloat(@floor(end.x)); 35 + const end_y: i32 = @intFromFloat(@floor(end.y)); 36 + 37 + rl.drawLine(start_x, start_y, end_x, end_y, .black); 33 38 } 34 39 } 35 40 36 41 const HexDirections = enum { top_left, top, top_right, bottom_right, bottom, bottom_left }; 37 42 38 43 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, 44 + const x_sign: f32 = switch (direction) { 45 + .bottom_right => 1.0, 46 + .top_right => 1.0, 45 47 .top => 0, 46 48 .bottom => 0, 47 49 .bottom_left => -1, 48 50 .top_left => -1, 49 51 }; 50 52 51 - const y_sign: i2 = switch (direction) { 53 + const y_sign: f32 = switch (direction) { 52 54 .top => -1, 53 - .top_left => -1, 54 - .top_right => -1, 55 + .top_left => -0.5, 56 + .top_right => -0.5, 57 + .bottom_left => 0.5, 58 + .bottom_right => 0.5, 55 59 .bottom => 1, 56 - .bottom_left => 1, 57 - .bottom_right => 1, 58 60 }; 59 61 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, 62 + const x_offset: f32 = x_sign * 1.5 * base.outer_radius; 63 + const y_offset: f32 = y_sign * base.height; 64 + 65 + return new_hex(base.outer_radius, .{ 66 + .x = base.center.x + x_offset, 67 + .y = base.center.y + y_offset, 63 68 }); 64 69 } 65 70 66 71 fn calculate_outer_radius(inner_radius: f32) f32 { 67 72 return 2.0 * inner_radius / std.math.sqrt(3); 68 73 } 74 + fn calculate_inner_radius(outer_radius: f32) f32 { 75 + return outer_radius * std.math.sqrt(3) / 2.0; 76 + } 69 77 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 }; 78 + fn new_hex(outer_radius: f32, center: Point) HexCell { 79 + const size = outer_radius; 80 + const inner = calculate_inner_radius(size); 81 + return HexCell{ .inner_radius = inner, .outer_radius = size, .height = std.math.sqrt(3) * size, .width = 2 * size, .center = center }; 73 82 } 74 83 75 84 pub fn main() anyerror!void { ··· 90 99 //---------------------------------------------------------------------------------- 91 100 // TODO: Update your variables here 92 101 //---------------------------------------------------------------------------------- 93 - const cell = new_hex(20.0, .{ 94 - .x = 200, 95 - .y = 200, 102 + const cell = new_hex(40.0, .{ 103 + .x = screenWidth / 2, 104 + .y = screenHeight / 2, 96 105 }); 97 106 98 107 const tl = add_hex(cell, .top_left); 108 + const tr = add_hex(cell, .top_right); 109 + const t = add_hex(cell, .top); 99 110 const br = add_hex(cell, .bottom_right); 111 + const bl = add_hex(cell, .bottom_left); 112 + const b = add_hex(cell, .bottom); 100 113 101 - const grid = [_]HexCell{ cell, tl, br }; 114 + const grid = [_]HexCell{ cell, tl, t, tr, br, b, bl }; 102 115 103 116 // Draw 104 117 //----------------------------------------------------------------------------------