浏览代码

Merge pull request #185 from wolfgarnet/almost-equal

Added almost equal for vector2 and vector4
Daniel Salvadori 5 年之前
父节点
当前提交
714f8d82e6
共有 2 个文件被更改,包括 22 次插入0 次删除
  1. 10 0
      math32/vector2.go
  2. 12 0
      math32/vector4.go

+ 10 - 0
math32/vector2.go

@@ -404,3 +404,13 @@ func (v *Vector2) InTriangle(p0, p1, p2 *Vector2) bool {
 
 	return s >= 0 && t >= 0 && (s+t) < 2*A*sign
 }
+
+// AlmostEquals returns whether the vector is almost equal to another vector within the specified tolerance.
+func (v *Vector2) AlmostEquals(other *Vector2, tolerance float32) bool {
+
+	if (Abs(v.X - other.X) < tolerance) &&
+		(Abs(v.Y - other.Y) < tolerance) {
+		return true
+	}
+	return false
+}

+ 12 - 0
math32/vector4.go

@@ -630,3 +630,15 @@ func (v *Vector4) Clone() *Vector4 {
 
 	return NewVector4(v.X, v.Y, v.Z, v.W)
 }
+
+// AlmostEquals returns whether the vector is almost equal to another vector within the specified tolerance.
+func (v *Vector4) AlmostEquals(other *Vector4, tolerance float32) bool {
+
+	if (Abs(v.X - other.X) < tolerance) &&
+		(Abs(v.Y - other.Y) < tolerance) &&
+		(Abs(v.Z - other.Z) < tolerance) &&
+		(Abs(v.W - other.W) < tolerance) {
+		return true
+	}
+	return false
+}