Skip to content

Commit

Permalink
fix j2d.Vector, add test suits
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Ji committed Nov 21, 2022
1 parent 846e051 commit 5aedc0e
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 9 deletions.
116 changes: 110 additions & 6 deletions src/j2d/Vector.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const std = @import("std");
const math = std.math;
const expectEqual = std.testing.expectEqual;
const jok = @import("../jok.zig");
const Self = @This();

Expand All @@ -20,7 +21,7 @@ pub fn y(self: Self) f32 {

/// Set all components to the same given value.
pub fn set(val: f32) Self {
const result = @splat(f32, val);
const result = @splat(2, val);
return .{ .data = result };
}

Expand All @@ -41,7 +42,7 @@ pub fn up() Self {

/// Shorthand for (0, 1).
pub fn down() Self {
return up().negate();
return Self.new(0, 1);
}

/// Shorthand for (1, 0).
Expand All @@ -51,7 +52,7 @@ pub fn right() Self {

/// Shorthand for (-1, 0).
pub fn left() Self {
return right().negate();
return Self.new(-1, 0);
}

/// Negate the given vector.
Expand All @@ -61,7 +62,7 @@ pub fn negate(self: Self) Self {

/// Construct new vector from slice.
pub fn fromSlice(slice: []const f32) Self {
const result = slice[0..f32].*;
const result = slice[0..2].*;
return .{ .data = result };
}

Expand Down Expand Up @@ -94,7 +95,7 @@ pub fn norm(self: Self) Self {
if (l == 0) {
return self;
}
const result = self.data / @splat(f32, l);
const result = self.data / @splat(2, l);
return .{ .data = result };
}

Expand Down Expand Up @@ -150,6 +151,109 @@ pub fn lerp(first_vector: Self, second_vector: Self, t: f32) Self {
const from = first_vector.data;
const to = second_vector.data;

const result = from + (to - from) * @splat(f32, t);
const result = from + (to - from) * @splat(2, t);
return .{ .data = result };
}

test "Vectors.eql" {
const a = Self.new(1, 2);
const b = Self.new(1, 2);
const c = Self.new(1.5, 2);

try expectEqual(Self.eql(a, b), true);
try expectEqual(Self.eql(a, c), false);
}

test "Vectors.set" {
const a = Self.new(2.5, 2.5);
const b = Self.set(2.5);
try expectEqual(a, b);
}

test "Vectors.add" {
const a = Self.one();
const b = Self.one();
try expectEqual(a.add(b), Self.set(2));
}

test "Vectors.negate" {
const a = Self.set(5);
const a_negated = Self.set(-5);
try expectEqual(a.negate(), a_negated);
}

test "Vectors.getAngle" {
const a = Self.right();
const b = Self.up();
const c = Self.left();
const d = Self.one();

try expectEqual(math.approxEqAbs(f32, a.getAngle(a), 0, 0.0001), true);
try expectEqual(math.approxEqAbs(f32, a.getAngle(b), 90, 0.0001), true);
try expectEqual(math.approxEqAbs(f32, a.getAngle(c), 180, 0.0001), true);
try expectEqual(math.approxEqAbs(f32, a.getAngle(d), 45, 0.0001), true);
}

test "Vectors.toArray" {
const a = Self.up().toArray();
const b = [_]f32{ 0, -1 };

try std.testing.expectEqualSlices(f32, &a, &b);
}

test "Vectors.length" {
const a = Self.new(1.5, 2.6);
try expectEqual(a.length(), 3.00166606);
}

test "Vectors.distance" {
const a = Self.zero();
const b = Self.left();
const c = Self.new(0, 5);

try expectEqual(a.distance(b), 1);
try expectEqual(a.distance(c), 5);
}

test "Vectors.normalize" {
const a = Self.new(1.5, 2.6);
const a_normalized = Self.new(0.499722480, 0.866185605);
try expectEqual(a.norm(), a_normalized);
}

test "Vectors.scale" {
const a = Self.new(1, 2);
const a_scaled = Self.new(5, 10);
try expectEqual(a.scale(5), a_scaled);
}

test "Vectors.dot" {
const a = Self.new(1.5, 2.6);
const b = Self.new(2.5, 3.45);
try expectEqual(a.dot(b), 12.7200002);
}

test "Vectors.lerp" {
const a = Self.new(-10, 0);
const b = Self.set(10);
try expectEqual(Self.lerp(a, b, 0.5), Self.new(0, 5));
}

test "Vectors.min" {
const a = Self.new(10, -2);
const b = Self.new(-10, 5);
const minimum = Self.new(-10, -2);
try expectEqual(Self.min(a, b), minimum);
}

test "Vectors.max" {
const a = Self.new(10, -2);
const b = Self.new(-10, 5);
const maximum = Self.new(10, 5);
try expectEqual(Self.max(a, b), maximum);
}

test "Vectors.fromSlice" {
const slice = [_]f32{ 2, 4 };
try expectEqual(Self.fromSlice(&slice), Self.new(2, 4));
}
5 changes: 5 additions & 0 deletions src/jok.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ pub const deps = @import("deps/deps.zig");

/// Misc util functions
pub const utils = @import("utils.zig");

// Tests
test "all" {
_ = @import("j2d/Vector.zig");
}
6 changes: 3 additions & 3 deletions src/utils/math.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ const math = std.math;
/// Map `v` from [from, to] to [map_from, map_to]
pub inline fn mapf(v: f32, from: f32, to: f32, map_from: f32, map_to: f32) f32 {
if (math.approxEqAbs(f32, from, to, math.epsilon(f32))) return from;
const v1 = std.math.clamp(v, from, to);
const v1 = math.clamp(v, from, to);
return map_from + (map_to - map_from) * (v1 - from) / (to - from);
}

/// Convert radian to degree
pub inline fn radianToDegree(r: f32) f32 {
return r * 180.0 / std.math.pi;
return r * 180.0 / math.pi;
}

/// Convert degree to radian
pub inline fn degreeToRadian(d: f32) f32 {
return d * std.math.pi / 180.0;
return d * math.pi / 180.0;
}

0 comments on commit 5aedc0e

Please sign in to comment.