public static V2f Multiply(Rot2f rot, V2f vec) { float a = (float)System.Math.Cos(rot.Angle); float b = (float)System.Math.Sin(rot.Angle); return(new V2f(a * vec.X + b * vec.Y, -b * vec.X + a * vec.Y)); }
/// <summary> /// Calculates the centroid for a given set of V2fs. /// </summary> public static V2f ComputeCentroid(this V2f[] vectors, int[] indices) { V2f sum = V2f.Zero; for (var i = 0; i < indices.Length; i++) { sum += vectors[indices[i]]; } return(sum / (float)indices.Length); }
/// <summary> /// Calculates the sum for a given set of V2fs. /// </summary> public static V2f Sum(this V2f[] vectors) { V2f sum = V2f.Zero; for (var i = 0; i < vectors.Length; i++) { sum += vectors[i]; } return(sum); }
/// <summary> /// Calculates the centroid for a given set of V2fs. /// </summary> public static V2f ComputeCentroid(this V2f[] vectors) { V2f sum = V2f.Zero; for (var i = 0; i < vectors.Length; i++) { sum += vectors[i]; } return(sum / (float)vectors.Length); }
/// <summary> /// Calculates the sum for a given set of V2fs. /// </summary> public static V2f Sum(this IEnumerable <V2f> vectors) { V2f sum = V2f.Zero; foreach (var e in vectors) { sum += e; } return(sum); }
/// <summary> /// Calculates the centroid for a given set of V2fs. /// </summary> public static V2f ComputeCentroid(this IEnumerable <V2f> vectors) { V2f sum = V2f.Zero; int count = 0; foreach (var e in vectors) { sum += e; count++; } return(sum / (float)count); }
/// <summary> /// Calculates a weighted centroid for a given array of V2fs. /// </summary> public static V2f ComputeCentroid(this V2f[] vectors, float[] weights) { V2f sum = V2f.Zero; float weightSum = 0; for (int i = 0; i < vectors.Length; i++) { sum += weights[i] * vectors[i]; weightSum += weights[i]; } return(sum / weightSum); }
/// <summary> /// Calculates a weighted centroid for vectors and weights given by indices. /// Sum(vectors[indices[i]] * weights[indices[i]]) / Sum(weights[indices[i]]. /// </summary> public static V2f ComputeCentroid(this V2f[] vectors, float[] weights, int[] indices) { V2f sum = V2f.Zero; float weightSum = 0; for (int i = 0; i < indices.Length; i++) { var w = weights[indices[i]]; sum += w * vectors[indices[i]]; weightSum += w; } return(sum / weightSum); }
/// <summary> /// Provides perspective projection matrix. /// The parameters describe the dimensions of the view volume. /// </summary> public static M44f PerspectiveProjectionTransformRH(V2f size, float n, float f) { float w = size.X; float h = size.Y; // Fx 0 0 0 // 0 Fy 0 0 // 0 0 A B // 0 0 -1 0 float Fx = 2 * n / w; float Fy = 2 * n / h; float A = f / (n - f); float B = n * f / (n - f); M44f P = new M44f( Fx, 0, 0, 0, 0, Fy, 0, 0, 0, 0, A, B, 0, 0, -1, 0); return(P); }
/// <summary> /// Creates a rigid transformation from a rotation <paramref name="rot"/> and a (subsequent) translation <paramref name="trans"/>. /// </summary> public Euclidean2f(Rot2f rot, V2f trans) { Rot = rot; Trans = trans; }
public static bool ApproxEqual(Euclidean2f r0, Euclidean2f r1, float angleTol, float posTol) { return(V2f.ApproxEqual(r0.Trans, r1.Trans, posTol) && Rot2f.ApproxEqual(r0.Rot, r1.Rot, angleTol)); }
/// <summary> /// Inverts this rigid transformation (multiplicative inverse). /// this = [Rot^T,-Rot^T Trans] /// </summary> public void Invert() { Rot.Invert(); Trans = -Rot.TransformDir(Trans); }
/// <summary> /// Transforms point p (p.w is presumed 1.0) by the inverse of this rigid transformation. /// </summary> public V2f InvTransformPos(V2f p) { return(InvTransformPos(this, p)); }
/// <summary> /// Transforms direction vector v (v.w is presumed 0.0) by the inverse of this rigid transformation. /// Actually, only the rotation is used. /// </summary> public V2f InvTransformDir(V2f v) { return(InvTransformDir(this, v)); }
/// <summary> /// Transforms point p (p.w is presumed 1.0) by this rigid transformation. /// </summary> public V2f TransformPos(V2f p) { return(TransformPos(this, p)); }
/// <summary> /// Transforms direction vector v (v.w is presumed 0.0) by this rigid transformation. /// Actually, only the rotation is used. /// </summary> public V2f TransformDir(V2f v) { return(TransformDir(this, v)); }
/// <summary> /// Transforms a position vector. /// </summary> public static V2f TransformPos(Rot2f rot, V2f v) { return((M22f)rot * v); }
/// <summary> /// Creates a rigid transformation from a trafo <paramref name="trafo"/>. /// </summary> public Euclidean2f(Trafo2d trafo) { Rot = Rot2f.FromM22f((M22f)trafo.Forward); Trans = (V2f)trafo.Forward.C2.XY; }
public static Euclidean2f Parse(string s) { var x = s.NestedBracketSplitLevelOne().ToArray(); return(new Euclidean2f(Rot2f.Parse(x[0]), V2f.Parse(x[1]))); }
/// <summary> /// Transforms a position vector. /// </summary> public static V2f InvTransformPos(Rot2f rot, V2f v) { return((M22f)Rot2f.Negate(rot) * v); }
/// <summary> /// Transforms a position vector. /// </summary> public V2f InvTransformPos(V2f v) { return(Rot2f.InvTransformPos(this, v)); }
/// <summary> /// Creates a rigid transformation from a rotation matrix <paramref name="rot"/> and a (subsequent) translation <paramref name="trans"/>. /// </summary> public Euclidean2f(M22f rot, V2f trans) { Rot = Rot2f.FromM22f(rot); Trans = trans; }
/// <summary> /// Returns the outer product (tensor-product) of v1 * v2^T as a 3x3 Matrix. /// </summary> public static M22f OuterProduct(this V2f v1, V2f v2) { return(new M22f( v2.X * v1.X, v2.Y * v1.X, v2.X * v1.Y, v2.Y * v1.Y)); }
/// <summary> /// Transforms direction vector v (v.w is presumed 0.0) by rigid transformation r. /// Actually, only the rotation is used. /// </summary> public static V2f TransformDir(Euclidean2f r, V2f v) { return(r.Rot.TransformDir(v)); }
/// <summary> /// Transforms point p (p.w is presumed 1.0) by rigid transformation r. /// </summary> public static V2f TransformPos(Euclidean2f r, V2f p) { return(r.Rot.TransformPos(p) + r.Trans); }
/// <summary> /// Multiplacation of a <see cref="Scale3f"/> with a <see cref="V2f"/>. /// </summary> public static V2f Multiply(Scale3f scale, V2f v) { return(new V2f(v.X * scale.X, v.Y * scale.Y)); }
/// <summary> /// Transforms point p (p.w is presumed 1.0) by the inverse of the rigid transformation r. /// </summary> public static V2f InvTransformPos(Euclidean2f r, V2f p) { return(r.Rot.InvTransformPos(p - r.Trans)); }