//public StaticlyPlacedObject(Vector begin, Vector end) //{ // BeginPoint = begin; // EndPoint = end; //} public Polygon ToPolygon() { //Make, add points, build and return. Polygon p = new Polygon(); p.Points.Add(BeginPoint); p.Points.Add(EndPoint); p.BuildEdges(); return p; }
public void event_PanelMouseDown(object sender, MouseEventArgs e) { foreach (PlacedProduct PlacedP in PlacementController.placedProductList) { //Get the mouse location var MouseLocation = new Point(e.X, e.Y); Polygon P = new Polygon(); P.Points.Add(new Vector(MouseLocation.X, MouseLocation.Y)); P.BuildEdges(); //And compare it to all possible polygons, so that when one collides, you know which one the user has clicked on. PolygonCollisionController.PolygonCollisionResult r = PolygonCollisionController.PolygonCollision(P, PlacedP.Poly, new Vector(0, 0)); if (r.WillIntersect) { //Do DragDrop clickLocation = MouseLocation; productAdding.DoDragDrop(PlacedP, DragDropEffects.Copy); //Set as current product productAdding.productInfo1.setProduct(PlacedP.Product); currentProduct = PlacedP; break; } } }
// Check if polygon A is going to collide with polygon B for the given velocity public static PolygonCollisionResult PolygonCollision(Polygon polygonA, Polygon polygonB, Vector velocity) { PolygonCollisionResult result = new PolygonCollisionResult(); result.Intersect = true; result.WillIntersect = true; int edgeCountA = polygonA.Edges.Count; int edgeCountB = polygonB.Edges.Count; float minIntervalDistance = float.PositiveInfinity; Vector translationAxis = new Vector(); Vector edge; // Loop through all the edges of both polygons for (int edgeIndex = 0; edgeIndex < edgeCountA + edgeCountB; edgeIndex++) { if (edgeIndex < edgeCountA) { edge = polygonA.Edges[edgeIndex]; } else { edge = polygonB.Edges[edgeIndex - edgeCountA]; } // ===== 1. Find if the polygons are currently intersecting ===== // Find the axis perpendicular to the current edge Vector axis = new Vector(-edge.Y, edge.X); axis.Normalize(); // Find the projection of the polygon on the current axis float minA = 0; float minB = 0; float maxA = 0; float maxB = 0; ProjectPolygon(axis, polygonA, ref minA, ref maxA); ProjectPolygon(axis, polygonB, ref minB, ref maxB); // Check if the polygon projections are currentlty intersecting if (IntervalDistance(minA, maxA, minB, maxB) > 0) result.Intersect = false; // ===== 2. Now find if the polygons *will* intersect ===== // Project the velocity on the current axis float velocityProjection = axis.DotProduct(velocity); // Get the projection of polygon A during the movement if (velocityProjection < 0) { minA += velocityProjection; } else { maxA += velocityProjection; } // Do the same test as above for the new projection float intervalDistance = IntervalDistance(minA, maxA, minB, maxB); if (intervalDistance > 0) result.WillIntersect = false; // If the polygons are not intersecting and won't intersect, exit the loop if (!result.Intersect && !result.WillIntersect) break; // Check if the current interval distance is the minimum one. If so store // the interval distance and the current distance. // This will be used to calculate the minimum translation vector intervalDistance = Math.Abs(intervalDistance); if (intervalDistance < minIntervalDistance) { minIntervalDistance = intervalDistance; translationAxis = axis; Vector d = polygonA.Center - polygonB.Center; if (d.DotProduct(translationAxis) < 0) translationAxis = -translationAxis; } } // The minimum translation vector can be used to push the polygons appart. // First moves the polygons by their velocity // then move polygonA by MinimumTranslationVector. if (result.WillIntersect) result.MinimumTranslationVector = translationAxis * minIntervalDistance; return result; }
// Calculate the projection of a polygon on an axis and returns it as a [min, max] interval public static void ProjectPolygon(Vector axis, Polygon polygon, ref float min, ref float max) { // To project a point on an axis use the dot product float d = axis.DotProduct(polygon.Points[0]); min = d; max = d; for (int i = 0; i < polygon.Points.Count; i++) { d = polygon.Points[i].DotProduct(axis); if (d < min) { min = d; } else { if (d > max) { max = d; } } } }
public List<Polygon> PolyBorder(int width, int height) { //List to return List<Polygon> list = new List<Polygon>(); // //Add the 4 corners // Polygon pTop = new Polygon(); Polygon pRight = new Polygon(); Polygon pBottom = new Polygon(); Polygon pLeft = new Polygon(); //Points for corners Vector pointTopLeft = new Vector(0, 0); Vector pointTopRight = new Vector(width, 0); Vector pointBottomLeft = new Vector(0, height); Vector pointBottomRight = new Vector(width, height); //Add points/vectors pTop.Points.Add(pointTopLeft); pTop.Points.Add(pointTopRight); // pRight.Points.Add(pointTopRight); pRight.Points.Add(pointBottomRight); // pBottom.Points.Add(pointBottomRight); pBottom.Points.Add(pointBottomLeft); // pLeft.Points.Add(pointBottomLeft); pLeft.Points.Add(pointTopLeft); //Build edges pTop.BuildEdges(); pRight.BuildEdges(); pBottom.BuildEdges(); pLeft.BuildEdges(); //Add to the list list.Add(pTop); list.Add(pRight); list.Add(pBottom); list.Add(pLeft); return list; }
public Polygon GetVirtualPolygon(Point newLocation) { Polygon virtualPolygon = new Polygon(); foreach (Vector v in Poly.Points) { virtualPolygon.Points.Add(v); } Vector delta = new Vector(new Vector(newLocation) - new Vector(Location)); virtualPolygon.Offset(delta); virtualPolygon.BuildEdges(); return virtualPolygon; }