/// <summary> /// Determines whether a ray intersects the specified sphere. /// </summary> /// <param name="ray">The ray which will be tested for intersection.</param> /// <param name="sphere">A sphere that will be tested for intersection.</param> /// <returns>Distance at which the ray intersects the sphere or null if there is no intersection.</returns> public static double?Intersects(Rayd ray, Spherei sphere) { var distance = sphere.Center - ray.Position; var pyth = Vector.AbsoluteSquared(distance); var rr = sphere.Radius * sphere.Radius; if (pyth <= rr) { return(0); } double dot = Vector.Dot(distance, ray.Direction); if (dot < 0) { return(null); } var temp = pyth - (dot * dot); if (temp > rr) { return(null); } return((double)(dot - Functions.Sqrt(rr - temp))); }
/// <summary> /// Returns the squared distance between two points. /// </summary> /// <param name="value1">The first point.</param> /// <param name="value2">The second point.</param> /// <returns>The squared distance between value1 and value2.</returns> public static double DistanceSquared(Point2d value1, Point2d value2) { return(Vector.AbsoluteSquared(value2 - value1)); }
public static bool Contains(Spherel sphere, Point3l point) { return(Vector.AbsoluteSquared(sphere.Center - point) <= sphere.Radius * sphere.Radius); }
public static void Fit(ColordBlock block, ColordSet colors, Options options, bool isBC1) { Vector3d maxColor = Vector3d.Zero; Vector3d minColor = Vector3d.One; foreach (Vector3d color in colors.Points) { minColor = Vector.Min(minColor, color); maxColor = Vector.Max(maxColor, color); } Vector3d inset = (maxColor - minColor) / 8.0; minColor = Vector.Clamp(minColor + inset, Vector3d.Zero, Vector3d.One); maxColor = Vector.Clamp(maxColor - inset, Vector3d.Zero, Vector3d.One); Vector3d color0 = minColor; Vector3d color1 = maxColor; Vector3d color2; Vector3d color3; if (isBC1 & colors.IsTransparent) { //need alpha coding, co <= c1 var max = Vector.Pack(5, 6, 5, Color.Quantize(5, 6, 5, (Colord)maxColor)); var min = Vector.Pack(5, 6, 5, Color.Quantize(5, 6, 5, (Colord)minColor)); if (max <= min) { color0 = maxColor; color1 = minColor; } } if (!isBC1 || (Vector.Pack(5, 6, 5, Color.Quantize(5, 6, 5, (Colord)color0)) > Vector.Pack(5, 6, 5, Color.Quantize(5, 6, 5, (Colord)color1)))) { color2 = Vector.Lerp(color0, color1, 1.0 / 3.0); color3 = Vector.Lerp(color0, color1, 2.0 / 3.0); } else { color2 = Vector.Lerp(color0, color1, 1.0 / 2.0); color3 = new Vector3d(0.0); } for (int y = 0; y < colors.Height; ++y) { for (int x = 0; x < colors.Width; ++x) { Vector3d color = colors.Points[x, y]; double d0 = Vector.AbsoluteSquared(Vector.Modulate(options.Metric, (color0 - color))); double d1 = Vector.AbsoluteSquared(Vector.Modulate(options.Metric, (color1 - color))); double d2 = Vector.AbsoluteSquared(Vector.Modulate(options.Metric, (color2 - color))); double d3 = Vector.AbsoluteSquared(Vector.Modulate(options.Metric, (color3 - color))); int b0 = d0 > d3 ? 1 : 0; int b1 = d1 > d2 ? 1 : 0; int b2 = d0 > d2 ? 1 : 0; int b3 = d1 > d3 ? 1 : 0; int b4 = d2 > d3 ? 1 : 0; int x0 = b1 & b2; int x1 = b0 & b3; int x2 = b0 & b4; block.Indices[x + (y * 4)] = (x2 | ((x0 | x1) << 1)); } } block.Colord0 = (int)Vector.Pack(5, 6, 5, Color.Quantize(5, 6, 5, (Colord)color0)); block.Colord0 = (int)Vector.Pack(5, 6, 5, Color.Quantize(5, 6, 5, (Colord)color0)); }
/// <summary> /// Returns the squared distance between two points. /// </summary> /// <param name="value1">The first point.</param> /// <param name="value2">The second point.</param> /// <returns>The squared distance between value1 and value2.</returns> public static float DistanceSquared(Point3f value1, Point3f value2) { return(Vector.AbsoluteSquared(value2 - value1)); }
public static bool Contains(Circlef circle, Point2f point) { return(Vector.AbsoluteSquared(circle.Center - point) <= circle.Radius * circle.Radius); }
/// <summary> /// Returns the squared distance between two points. /// </summary> /// <param name="value1">The first point.</param> /// <param name="value2">The second point.</param> /// <returns>The squared distance between value1 and value2.</returns> public static long DistanceSquared(Point2l value1, Point2l value2) { return(Vector.AbsoluteSquared(value2 - value1)); }