private void TestToStringWithCulture(CultureInfo culture) { CultureInfo originalCulture = Thread.CurrentThread.CurrentCulture; Thread.CurrentThread.CurrentCulture = culture; try { string listSeparator = culture.TextInfo.ListSeparator; string decimalSeparator = culture.NumberFormat.NumberDecimalSeparator; var o = new Ray2F(new Vec2F(1.1f, 2.2f), new Vec2F(3.3f, 4.4f)); string s = o.ToString(null, null); TestToStringResults(o, s, listSeparator, decimalSeparator); string s2 = o.ToString(); Assert.AreEqual(s, s2); s = o.ToString("G", culture); TestToStringResults(o, s, listSeparator, decimalSeparator); s = o.ToString("R", culture); TestToStringResults(o, s, listSeparator, decimalSeparator); } finally { Thread.CurrentThread.CurrentCulture = originalCulture; } }
/// <summary> /// Gets the first intersection of a ray with a box (closest intersection to the /// ray origin)</summary> /// <param name="ray">Ray</param> /// <param name="box">Box</param> /// <param name="intersection">Intersection point</param> /// <returns>True iff ray hits box</returns> public static bool Intersect(Ray2F ray, Box2F box, ref Vec2F intersection) { // do X slab float tmin, tmax; SlabIntersect( ray.Direction.X, ray.Origin.X, box.Min.X, box.Max.X, out tmin, out tmax); // do Y slab float tminTemp, tmaxTemp; SlabIntersect( ray.Direction.Y, ray.Origin.Y, box.Min.Y, box.Max.Y, out tminTemp, out tmaxTemp); if ((tmin > tmaxTemp) || (tminTemp > tmax)) { return(false); } tmin = Math.Max(tmin, tminTemp); tmax = Math.Min(tmax, tmaxTemp); // intersection at tmin, the maximal-minimal value if (tmin > 0) { intersection = ray.Origin + ray.Direction * tmin; return(true); } return(false); }
/// <summary> /// Gets the first intersection of a ray with a box (closest intersection to the /// ray origin)</summary> /// <param name="ray">Ray</param> /// <param name="box">Box</param> /// <param name="intersection">Intersection point</param> /// <returns>True iff ray hits box</returns> public static bool Intersect(Ray2F ray, Box2F box, ref Vec2F intersection) { // do X slab float tmin, tmax; SlabIntersect( ray.Direction.X, ray.Origin.X, box.Min.X, box.Max.X, out tmin, out tmax); // do Y slab float tminTemp, tmaxTemp; SlabIntersect( ray.Direction.Y, ray.Origin.Y, box.Min.Y, box.Max.Y, out tminTemp, out tmaxTemp); if ((tmin > tmaxTemp) || (tminTemp > tmax)) return false; tmin = Math.Max(tmin, tminTemp); tmax = Math.Min(tmax, tmaxTemp); // intersection at tmin, the maximal-minimal value if (tmin > 0) { intersection = ray.Origin + ray.Direction * tmin; return true; } return false; }
private void TestToStringResults(Ray2F o, string s, string listSeparator, string decimalSeparator) { string[] results = s.Split(new[] { listSeparator }, StringSplitOptions.RemoveEmptyEntries); Assert.AreEqual(results.Length, 4); foreach (string oneFloatString in results) Assert.True(oneFloatString.Contains(decimalSeparator)); Assert.AreEqual(float.Parse(results[0]), o.Origin.X); Assert.AreEqual(float.Parse(results[1]), o.Origin.Y); Assert.AreEqual(float.Parse(results[2]), o.Direction.X); Assert.AreEqual(float.Parse(results[3]), o.Direction.Y); }
/// <summary> /// Determines if 2 rays intersect, and if so, returns the ray parameters of the intersection point</summary> /// <param name="r1">Ray 1</param> /// <param name="r2">Ray 2</param> /// <param name="t1">Returned ray parameter 1</param> /// <param name="t2">Returned ray parameter 2</param> /// <returns>True iff the rays intersect (are not parallel)</returns> public static bool Intersect(Ray2F r1, Ray2F r2, ref double t1, ref double t2) { double denom = (r2.Direction.Y * r1.Direction.X - r2.Direction.X * r1.Direction.Y); if (Math.Abs(denom) < 0.000001f) return false; Vec2F d = Vec2F.Sub(r1.Origin, r2.Origin); t1 = (r2.Direction.X * d.Y - r2.Direction.Y * d.X) / denom; t2 = (r1.Direction.X * d.Y - r1.Direction.Y * d.X) / denom; return true; }
/// <summary> /// Determines if 2 rays intersect, and if so, returns the ray parameters of the intersection point</summary> /// <param name="r1">Ray 1</param> /// <param name="r2">Ray 2</param> /// <param name="t1">Returned ray parameter 1</param> /// <param name="t2">Returned ray parameter 2</param> /// <returns>True iff the rays intersect (are not parallel)</returns> public static bool Intersect(Ray2F r1, Ray2F r2, ref double t1, ref double t2) { double denom = (r2.Direction.Y * r1.Direction.X - r2.Direction.X * r1.Direction.Y); if (Math.Abs(denom) < 0.000001f) { return(false); } Vec2F d = Vec2F.Sub(r1.Origin, r2.Origin); t1 = (r2.Direction.X * d.Y - r2.Direction.Y * d.X) / denom; t2 = (r1.Direction.X * d.Y - r1.Direction.Y * d.X) / denom; return(true); }
/// <summary> /// Constructs the circle containing 3 points</summary> /// <param name="p1">Point 1</param> /// <param name="p2">Point 2</param> /// <param name="p3">Point 3</param> public CircleF(Vec2F p1, Vec2F p2, Vec2F p3) { Vec2F o1 = Vec2F.Add(p1, p2); o1 *= 0.5f; Vec2F o2 = Vec2F.Add(p3, p2); o2 *= 0.5f; Vec2F d1 = Vec2F.Sub(p2, p1); d1 = d1.Perp; Vec2F d2 = Vec2F.Sub(p3, p2); d2 = d2.Perp; double t1 = 0; double t2 = 0; if (Ray2F.Intersect(new Ray2F(o1, d1), new Ray2F(o2, d2), ref t1, ref t2)) { Center = o1 + d1 * (float)t1; Radius = Vec2F.Distance(Center, p1); } else { Center = new Vec2F(float.PositiveInfinity, float.PositiveInfinity); Radius = float.PositiveInfinity; } }