/// <summary> /// Rotates a Vector2 around the Z axis /// </summary> /// <param name="v1">The Vector2 to be rotated</param> /// <param name="radians">The angle to rotate the Vector2 around in radians</param> /// <returns>Vector2 representing the rotation around the Z axis</returns> public static Vector2 Rotate(Vector2 v1, double radians) { var x = v1.X * SysMath.Cos(radians) - v1.Y * SysMath.Sin(radians); var y = v1.X * SysMath.Sin(radians) + v1.Y * SysMath.Cos(radians); return(new Vector2(x, y)); }
public static Matrix23 Rotation(Angle angle) { var sin = SysMath.Sin(angle); var cos = SysMath.Cos(angle); return(new Matrix23( cos, -sin, 0, sin, +cos, 0)); }
/// <summary> /// Calculates the signed angle corresponding to specified vector /// in range of -PI to PI both inclusive. /// </summary> /// <returns>Signed angle, in radians.</returns> public static double OriginAngle(Vector2 vector) { vector.Normalize(); var angleSign = vector.Y >= 0 ? 1 : -1; var angle = SysMath.Acos(vector.X); angle *= angleSign; return(angle); }
public static bool operator==(Matrix23 m1, Matrix23 m2) { return (SysMath.Abs(m1.S00 - m2.S00) <= EqualityTolerence && SysMath.Abs(m1.S10 - m2.S10) <= EqualityTolerence && SysMath.Abs(m1.S01 - m2.S01) <= EqualityTolerence && SysMath.Abs(m1.S11 - m2.S11) <= EqualityTolerence && SysMath.Abs(m1.S02 - m2.S02) <= EqualityTolerence && SysMath.Abs(m1.S12 - m2.S12) <= EqualityTolerence); }
private static int CompareVec2(Vector2 a, Vector2 b) { if (a.X == b.X) { return(SysMath.Sign(a.Y - b.Y)); } else { return(SysMath.Sign(a.X - b.X)); } }
/// <summary> /// Find the crossing angle between lines passing through two vectors. /// </summary> /// <param name="v1">The Vector2 to discern the angle from </param> /// <param name="v2">The Vector2 to discern the angle to</param> /// <returns>The crossing angle.</returns> public static double CrossingAngle(Vector2 v1, Vector2 v2) { var dot = SysMath.Abs(v1.Normal.Dot(v2.Normal)); var error = dot - 1; if (error > 0) { dot -= error; } return(SysMath.Acos(dot)); }
public override void DrawCircle(Vec2 center, float radius, Color color) { float k_segments = 16.0f; float k_increment = 2.0f * Box2DNet.Common.Settings.Pi / k_segments; float theta = 0.0f; GL.Color3(color.R, color.G, color.B); GL.Begin(BeginMode.LineLoop); for (int i = 0; i < k_segments; ++i) { Vec2 v = center + radius * new Vec2((float)SysMath.Cos(theta), (float)SysMath.Sin(theta)); GL.Vertex2(v.X, v.Y); theta += k_increment; } GL.End(); }
public override void DrawCircle(Vec2 center, float radius, Color color) { float k_segments = 16.0f; float k_increment = 2.0f * Box2DX.Common.Settings.Pi / k_segments; float theta = 0.0f; Gl.glColor3f(color.R, color.G, color.B); Gl.glBegin(Gl.GL_LINE_LOOP); for (int i = 0; i < k_segments; ++i) { Vec2 v = center + radius * new Vec2((float)SysMath.Cos(theta), (float)SysMath.Sin(theta)); Gl.glVertex2f(v.X, v.Y); theta += k_increment; } Gl.glEnd(); }
public double DistanceToSegment(Vector2 v, Vector2 w) { // Return minimum distance between line segment vw and this vertex var l2 = (v - w).SqrLength; // i.e. |w-v|^2 - avoid a sqrt if (l2 == 0.0) { return(Distance(v)); // v == w case } // Consider the line extending the segment, parameterized as v + t (w - v). // We find projection of point p onto the line. // It falls where t = [(p-v) . (w-v)] / |w-v|^2 // We clamp t from [0,1] to handle points outside the segment vw. var t = SysMath.Max(0, SysMath.Min(1, (this - v).Dot(w - v) / l2)); Vector2 proj = v + t * (w - v); // Projection falls on the segment return(Distance(proj)); }
public override void DrawSolidCircle(Vec2 center, float radius, Vec2 axis, Color color) { float k_segments = 16.0f; float k_increment = 2.0f * Box2DNet.Common.Settings.Pi / k_segments; float theta = 0.0f; GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); GL.Color4(0.5f * color.R, 0.5f * color.G, 0.5f * color.B, 0.5f); GL.Begin(BeginMode.TriangleFan); for (int i = 0; i < k_segments; ++i) { Vec2 v = center + radius * new Vec2((float)SysMath.Cos(theta), (float)SysMath.Sin(theta)); GL.Vertex2(v.X, v.Y); theta += k_increment; } GL.End(); GL.Disable(EnableCap.Blend); theta = 0.0f; GL.Color4(color.R, color.G, color.B, 1.0f); GL.Begin(BeginMode.LineLoop); for (int i = 0; i < k_segments; ++i) { Vec2 v = center + radius * new Vec2((float)SysMath.Cos(theta), (float)SysMath.Sin(theta)); GL.Vertex2(v.X, v.Y); theta += k_increment; } GL.End(); Vec2 p = center + radius * axis; GL.Begin(BeginMode.Lines); GL.Vertex2(center.X, center.Y); GL.Vertex2(p.X, p.Y); GL.End(); }
public override void DrawSolidCircle(Vec2 center, float radius, Vec2 axis, Color color) { float k_segments = 16.0f; float k_increment = 2.0f * Box2DX.Common.Settings.Pi / k_segments; float theta = 0.0f; Gl.glEnable(Gl.GL_BLEND); Gl.glBlendFunc(Gl.GL_SRC_ALPHA, Gl.GL_ONE_MINUS_SRC_ALPHA); Gl.glColor4f(0.5f * color.R, 0.5f * color.G, 0.5f * color.B, 0.5f); Gl.glBegin(Gl.GL_TRIANGLE_FAN); for (int i = 0; i < k_segments; ++i) { Vec2 v = center + radius * new Vec2((float)SysMath.Cos(theta), (float)SysMath.Sin(theta)); Gl.glVertex2f(v.X, v.Y); theta += k_increment; } Gl.glEnd(); Gl.glDisable(Gl.GL_BLEND); theta = 0.0f; Gl.glColor4f(color.R, color.G, color.B, 1.0f); Gl.glBegin(Gl.GL_LINE_LOOP); for (int i = 0; i < k_segments; ++i) { Vec2 v = center + radius * new Vec2((float)SysMath.Cos(theta), (float)SysMath.Sin(theta)); Gl.glVertex2f(v.X, v.Y); theta += k_increment; } Gl.glEnd(); Vec2 p = center + radius * axis; Gl.glBegin(Gl.GL_LINES); Gl.glVertex2f(center.X, center.Y); Gl.glVertex2f(p.X, p.Y); Gl.glEnd(); }
/// <summary> /// Checks if a vector a unit vector /// Checks if the Vector2 has been normalized /// Checks if a vector has a magnitude of 1 /// </summary> /// <param name="v1"> /// The vector to be checked for Normalization /// </param> /// <returns>Truth if the vector is a unit vector</returns> public static bool IsUnitVector(Vector2 v1) { return(SysMath.Abs(v1.Length - 1) <= EqualityTolerence); }
/// <summary> /// Find the distance between two Vectors /// Pythagoras theorem on two Vectors /// </summary> /// <param name="v1">The Vector2 to find the distance from </param> /// <param name="v2">The Vector2 to find the distance to </param> /// <returns>The distance between two Vectors</returns> public static double Distance(Vector2 v1, Vector2 v2) { return(SysMath.Sqrt((v1.X - v2.X) * (v1.X - v2.X) + (v1.Y - v2.Y) * (v1.Y - v2.Y))); }
/// <summary> /// Returns Vector2 representing the absolute values of the vector /// </summary> public static Vector2 Abs(Vector2 v1) { return(new Vector2(SysMath.Abs(v1.X), SysMath.Abs(v1.Y))); }
/// <summary> /// Find the angle between two Vectors /// </summary> /// <param name="v1">The Vector2 to discern the angle from </param> /// <param name="v2">The Vector2 to discern the angle to</param> /// <returns>The angle between two positional Vectors</returns> public static double Angle(Vector2 v1, Vector2 v2) { return(SysMath.Acos(v1.Normal.Dot(v2.Normal))); }
public double DistanceToLine(Vector2 l1, Vector2 l2) { return(SysMath.Abs((l2.X - l1.X) * (l1.Y - Y) - (l1.X - X) * (l2.Y - l1.Y)) / l1.Distance(l2)); }
/// <summary> /// Compare two Vectors for equality. /// Are two Vectors equal. /// </summary> /// <param name="v1">Vector2 to be compared for equality </param> /// <param name="v2">Vector2 to be compared to </param> /// <returns>Boolean decision (truth for equality)</returns> public static bool operator==(Vector2 v1, Vector2 v2) { return(SysMath.Abs(v1.X - v2.X) <= EqualityTolerence && SysMath.Abs(v1.Y - v2.Y) <= EqualityTolerence); }