static bool SetEndableValidPosition(v2 pos, v2 dir, bool final) { if (mat[pos.x, pos.y] == 3) { return(false); } var nextPos = pos + dir; // if is blocked or already out side - nothing to do if (SetPassablePosition(nextPos)) { // if start is next we cannot put blocker if (mat[nextPos.x, nextPos.y] == 3) { return(false); } mat[nextPos.x, nextPos.y] = 2; } if (final) { mat[pos.x, pos.y] = 4; } return(true); }
public static v2 Max(v2 x, params v2[] vecs) { foreach (var v in vecs) { x = Max(x, v); } return(x); }
[Test] public void PointToLine2d() { var a = new v2(1f, 1f); var b = new v2(4f, 3f); Assert.Equal(0f, Geometry.ClosestPoint(a, b, new v2(0f, 0f))); Assert.Equal(1f, Geometry.ClosestPoint(a, b, new v2(5f, 2f))); Assert.Equal(0.5f, Geometry.ClosestPoint(a, b, new v2(2.5f, 2f))); }
/// <summary>Normalise 'vec' by the length of the XY components or return 'def' if zero</summary> public static v2 Normalise(v2 vec, v2 def) { if (vec == v2.Zero) { return(def); } var norm = Normalise(vec); return(norm != v2.Zero ? norm : def); }
//protected override void RefreshInternal() //{ // var ldr = new Rylogic.LDraw.LdrBuilder(); // ldr.Append("*Box b FF00FF00 {20}"); // m_gfx.UpdateModel(ldr.ToString(), View3d.EUpdateObject.All ^ View3d.EUpdateObject.Transform); // m_gfx.O2P = Position; //} public override DiagramControl.HitTestResult.Hit HitTest(v2 point, View3d.Camera cam) { if ((PositionXY - point).Length > 20) { return(null); } point -= PositionXY; return(new DiagramControl.HitTestResult.Hit(this, point)); }
[Test] public void FromPoints() { var a = new v2(0.5f, 0.3f); var b = new v2(0.7f, -0.2f); var c = new v2(1.0f, 0.6f); var q = Quadratic.FromPoints(a, b, c); Assert.Equal(q.F(a.x), a.y, Math_.TinyF); Assert.Equal(q.F(b.x), b.y, Math_.TinyF); Assert.Equal(q.F(c.x), c.y, Math_.TinyF); }
public ChartDataLegend(Guid id) : base(id, m4x4.Identity, "Legend") { m_bk_colour = 0xFFFFFFFF; m_padding = new Thickness(5); m_anchor = new v2(+1f, +1f); m_font = new Typeface("tahoma"); m_font_size = 14.0; m_series = new List <ChartDataSeries>(); PositionXY = new v2(1f, 1f); PositionZ = 0.001f; ScreenSpace = true; }
public static bool GetLevel(Vector2Int startPosition, out LevelConfig levelConfig) { rowsNr = LevelConfig.HeightCells; columnsNr = LevelConfig.WidthCells; mat = new byte[columnsNr, rowsNr]; var start = new v2(startPosition.x, startPosition.y); mat[start.x, start.y] = 3; var isOk = true; var cursor = start; var lastDir = new v2(0, 0); var laststop = new v2(-1, -1); var maxTurns = 5; var rnd = new System.Random(); while (true) // another corner { var final = rnd.Next(4) == 0 || maxTurns-- == 0; if (!SetNextStop(cursor, out cursor, out var dir, final)) { if (lastDir.x == -1 || !SetEndableValidPosition(laststop, lastDir, true)) { isOk = false; } break; } lastDir = dir; laststop = cursor; if (final) { break; } } #if UNITY_EDITOR Debug.Log(isOk ? "ok" : "fail"); display(); #endif levelConfig = ParseToLevel(); return(isOk); }
public static bool FEqlRelative(v2 a, v2 b, float tol) { var max_a = MaxElement(Abs(a)); var max_b = MaxElement(Abs(b)); if (max_b == 0) { return(max_a < tol); } if (max_a == 0) { return(max_b < tol); } var abs_max_element = Max(max_a, max_b); return(FEqlAbsolute(a, b, tol * abs_max_element)); }
static bool GetAvailableRandomDirection(v2 from, out v2 dir) { var directions = new List <v2> { new v2(1, 0), new v2(0, 1), new v2(-1, 0), new v2(0, -1) }; var rnd = new System.Random(); while (directions.Count > 0) { var idx = rnd.Next(directions.Count); dir = directions[idx]; directions.RemoveAt(idx); var newPos = from + dir; // if outside bounds if (newPos.x < 0 || newPos.x >= columnsNr || newPos.y < 0 || newPos.y >= rowsNr) { continue; } // if is occupied if (mat[newPos.x, newPos.y] != 0) { continue; } return(true); } dir = new v2(0, 0); return(false); }
static bool SetPassablePosition(v2 pos) { if (pos.x < 0 || pos.x >= columnsNr || pos.y < 0 || pos.y >= rowsNr) { return(false); } // if is occupied if (mat[pos.x, pos.y] == 2) { return(false); } if (mat[pos.x, pos.y] == 0) { mat[pos.x, pos.y] = 1; } return(true); }
/// <summary>Returns a vector perpendicular to 'vec' favouring 'previous' as the preferred perpendicular</summary> public static v2 Perpendicular(v2 vec, v2 previous) { Debug.Assert(!FEql(vec, v2.Zero), "Cannot make a perpendicular to a zero vector"); // If 'previous' is still perpendicular, keep it if (FEql(Dot(vec, previous), 0)) { return(previous); } // If 'previous' is parallel to 'vec', choose a new perpendicular if (Parallel(vec, previous)) { return(Perpendicular(vec)); } // Otherwise, make a perpendicular that is close to 'previous' var v = previous - (Dot(vec, previous) / vec.LengthSq) * vec; v *= (float)Math.Sqrt(vec.LengthSq / v.LengthSq); return(v); }
public override void Execute() { try { Length = 0; Size = new v2(); Raw = new byte[] { }; BGRA = new v4[] { }; if (!string.IsNullOrEmpty(Path) && File.Exists(Path)) { // TODO NODE ImageFile.Write // read var bitmapImage = new BitmapImage(new Uri(Path, UriKind.RelativeOrAbsolute)); Size = new v2(bitmapImage.PixelWidth, bitmapImage.PixelHeight); Raw = new byte[bitmapImage.PixelWidth * bitmapImage.PixelHeight * 4]; bitmapImage.CopyPixels(Raw, bitmapImage.PixelWidth * 4, 0); BGRA = new v4[bitmapImage.PixelWidth * bitmapImage.PixelHeight]; for (var i = 0; i < BGRA.Length; i++) { BGRA[i] = new v4( (double)Raw[i * 4 + 0] / 255.0, (double)Raw[i * 4 + 1] / 255.0, (double)Raw[i * 4 + 2] / 255.0, (double)Raw[i * 4 + 3] / 255.0); } } Length = Raw?.Length ?? 0; } catch (Exception ex) { Log.WriteLine(ex.ToString()); } }
[Test] public void FromLinearRegression() { { var pts = new v2[] { new v2(0, 15), new v2(1, 13), new v2(2, 10), new v2(3, 7), new v2(4, 4), new v2(5, 1), }; var m = Monic.FromLinearRegression(pts); //Assert.True(Math_.FEql(m.A, 0.689393937587738)); //Assert.True(Math_.FEql(m.B, -6.10151338577271)); } { var pts = new v2[] { new v2(0, 15), new v2(1, 13), new v2(2, 10), new v2(3, 7), new v2(4, 4), new v2(5, 1), new v2(6, 5), new v2(7, 8), new v2(8, 13), new v2(9, 19), }; var q = Quadratic.FromLinearRegression(pts); Assert.True(Math_.FEql((float)q.A, 0.689394f)); Assert.True(Math_.FEql((float)q.B, -6.10151672f)); Assert.True(Math_.FEql((float)q.C, 17.3090973f)); } }
static bool SetNextStop(v2 from, out v2 cursor, out v2 dir, bool final) { cursor = new v2(0, 0); if (!GetAvailableRandomDirection(from, out dir)) { #if UNITY_EDITOR Debug.Log("no direction"); #endif return(false); } cursor = from; var rnd = new System.Random(); var min = 1; while (min > 0 || rnd.Next(0, 5) > 0) // go forth { min--; if (SetPassablePosition(cursor + dir)) { cursor += dir; } else { break; } } if (SetEndableValidPosition(cursor, dir, final)) { return(true); } return(false); }
/// <summary>Dot product of XYZ components</summary> public static float Dot(v2 lhs, v2 rhs) { return(lhs.x * rhs.x + lhs.y * rhs.y); }
/// <summary>Return the index of the maximum element in 'v'</summary> public static int MaxElementIndex(v2 v) { return(v.x >= v.y ? 0 : 1); }
api_query_thread(NewAPI_StatusQuery, sISDN, v2);
public static string Vec3(v2 vec) { Debug.Assert(Math_.IsFinite(vec)); return($"{vec.x} {vec.y} 0"); }
var(v2, s2) = f(v1).Run(s1);
/// <summary>Rotate 'vec' clockwise</summary> public static v2 RotateCW(v2 vec) { return(new v2(vec.y, -vec.x)); }
/// <summary>Return the index of the minimum element in 'v'</summary> public static int MinElementIndex(v2 v) { return(v.x <= v.y ? 0 : 1); }
/// <summary>Return the cosine of the angle between two vectors</summary> public static float CosAngle(v2 lhs, v2 rhs) { // Return the cosine of the angle between two vectors Debug.Assert(lhs.LengthSq != 0 && rhs.LengthSq != 0, "CosAngle undefined for zero vectors"); return(Clamp(Dot(lhs, rhs) / (float)Math.Sqrt(lhs.LengthSq * rhs.LengthSq), -1f, 1f)); }
/// <summary>Return the angle between two vectors</summary> public static float Angle(v2 lhs, v2 rhs) { return((float)Math.Acos(CosAngle(lhs, rhs))); }
/// <summary>Returns a vector perpendicular to 'vec'</summary> public static v2 Perpendicular(v2 vec) { Debug.Assert(!FEql(vec, v2.Zero), "Cannot make a perpendicular to a zero vector"); return(RotateCCW(vec)); }
/// <summary>Normalise 'vec' by the length of the XY components</summary> public static v2 Normalise(v2 vec) { return(vec / vec.Length); }
/// <summary>Returns a vector guaranteed to not be parallel to 'vec'</summary> public static v2 CreateNotParallelTo(v2 vec) { bool x_aligned = Abs(vec.x) > Abs(vec.y); return(new v2(SignF(!x_aligned), SignF(x_aligned))); }
/// <summary>Linearly interpolate between two vectors</summary> public static v2 Lerp(v2 lhs, v2 rhs, float frac) { return(lhs * (1f - frac) + rhs * (frac)); }
/// <summary>True if 'lhs' and 'rhs' are parallel</summary> public static bool Parallel(v2 lhs, v2 rhs) { return(FEql(Cross(lhs, rhs), 0)); }
/// <summary>Cross product: Dot(Rotate90CW(lhs), rhs)</summary> public static float Cross(v2 lhs, v2 rhs) { return(lhs.y * rhs.x - lhs.x * rhs.y); }
public static v2 Normalise(ref v2 vec) { return(vec /= vec.Length); }