/// <summary> /// Projects a polygon onto an axis. /// </summary> /// <param name="polygon">The polygon to project.</param> /// <param name="axis">The axis to project onto.</param> /// <returns>The interval on the axis that the polygon projects onto.</returns> public static Linel Project(Polygon2l polygon, Vector2l axis) { var min = long.MaxValue; var max = long.MinValue; for (int i = 0; i < polygon.Count; ++i) { var proj = Vector.Dot((Vector2l)polygon[i], axis); min = Functions.Min(min, proj); max = Functions.Max(max, proj); } return(new Linel(min, max)); }
public IEnumerable<Tuple<Point2l, Point2l>> Trace(Point2l start, Point2l end) { var x0 = start.X / GridSize.Width; var y0 = start.Y / GridSize.Height; var x1 = end.X / GridSize.Width; var y1 = end.Y / GridSize.Height; var dir = new Vector2l(end.X - start.X, end.Y - start.Y); var dx = Math.Sign(dir.X); var dy = Math.Sign(dir.Y); var deltax = (double)GridSize.Width / Math.Abs(dir.X); var deltay = (double)GridSize.Height / Math.Abs(dir.Y); var minx = x0 * GridSize.Width; var maxx = minx + GridSize.Width; var miny = y0 * GridSize.Height; var maxy = miny + GridSize.Height; var distx = ((dx >= 0) ? (maxx - start.X) : (start.X - minx)) / (double)GridSize.Width; var disty = ((dy >= 0) ? (maxy - start.Y) : (start.Y - miny)) / (double)GridSize.Height; var tx = distx * deltax; var ty = disty * deltay; var prev = new Point2l(x0, y0); var hit = start; while (true) { var grid = new Point2l(x0, y0); yield return Tuple.Create(grid, hit); prev = grid; if (tx <= ty) { if (x0 == x1) break; hit = new Point2l( start.X + (long)(dir.X * tx), start.Y + (long)(dir.Y * tx)); tx += deltax; x0 += dx; } else { if (y0 == y1) break; hit = new Point2l( start.X + (long)(dir.X * ty), start.Y + (long)(dir.Y * ty)); ty += deltay; y0 += dy; } } }
/// <summary> /// Projects a point onto a vector, returns the distance of the projection from the origin. /// </summary> /// <param name="vector">The vector to project onto.</param> /// <param name="point">The point to project.</param> /// <returns>The distance from the origin of the projection.</returns> public static long Project(Point2l point, Vector2l vector) { return(vector.X * point.X + vector.Y * point.Y); }
/// <summary> /// Subtracts a vector from a point and returns the result. /// </summary> /// <param name="point">The point value to subtract from (the minuend).</param> /// <param name="vector">The vector value to subtract (the subtrahend).</param> /// <returns>The result of subtracting vector from point (the difference).</returns> public static Point2l Subtract(Point2l point, Vector2l vector) { return(new Point2l(point.X - vector.X, point.Y - vector.Y)); }
/// <summary> /// Adds a point and a vector and returns the result. /// </summary> /// <param name="point">The point value to add.</param> /// <param name="vector">The vector value to add.</param> /// <returns>The sum of left and right.</returns> public static Point2l Add(Point2l point, Vector2l vector) { return(new Point2l(point.X + vector.X, point.Y + vector.Y)); }