public static CollisionOption Collided(Polygon p1, Polygon p2) { // check collision for all pair of polygon's lines with each other System.Collections.Generic.List <PointF> pointCollision = new System.Collections.Generic.List <PointF>(2); CollisionOption collision = new CollisionOption { Collided = false, Points = null }; for (int current = 0; current < p1.currentPoints.Length; ++current) { // go through each of the vertices, plus the next vertex in the list PointF pc = p1.currentPoints[current]; // c for "current" PointF pn = p1.currentPoints[(current + 1) % p1.currentPoints.Length]; // n for "next" // now we can use these two points (a line) to compare // to the other polygon's vertices using polyLine() CollisionOption res = PolyLine(p2, pc, pn); if (res.Points != null) { pointCollision.AddRange(res.Points); } collision.Collided |= res.Collided; } // if lines are not collided // check one point if it inside polygon if (collision.Collided == false) { // optional: check if the 2nd polygon is INSIDE the first bool isInside = PolyPoint(p1, p2.currentPoints[0]); // if it inside, collision points are same as second polygon point return(new CollisionOption { Collided = isInside, Points = isInside ? p2.currentPoints : null }); } else // if lines are collided { // check for vertexes to be inside polygon foreach (PointF p in p2.currentPoints) { if (PolyPoint(p1, p)) { pointCollision.Add(p); } } } collision.Points = pointCollision.ToArray(); return(collision); }
private static CollisionOption PolyLine(Polygon p, PointF p1, PointF p2) { System.Collections.Generic.List <PointF> pointCollision = new System.Collections.Generic.List <PointF>(2); for (int current = 0; current < p.currentPoints.Length; ++current) { PointF p3 = p.currentPoints[current]; // current PointF p4 = p.currentPoints[(current + 1) % p.currentPoints.Length]; // next // check if vertex are inside point if (PolyPoint(p, p3)) { pointCollision.Add(p3); } // do a Line/Line comparison // collect all point where line collide with polygone // could be more than 1 CollisionOption hit = LineLine(p1, p2, p3, p4); if (hit.Collided == true) { pointCollision.AddRange(hit.Points); } // check if vertex are inside point if (PolyPoint(p, p4)) { pointCollision.Add(p4); } } // all checking has been passed if (pointCollision.Count > 0)// collided { return(new CollisionOption { Collided = true, Points = pointCollision.ToArray() }); } else // never got hit { return(new CollisionOption { Collided = false, Points = null }); } }
// PAINTING private void displayPb_Paint(object sender, PaintEventArgs e) { graphics.Clear(Color.LightGray); try { // check for collision collided?.Dispose(); Classes.CollisionOption collisionOption = Classes.Polygon.Collided(polygon1, polygon2); if (collisionOption.Collided == true) { // collision point foreach (var item in collisionOption.Points) { graphics.FillEllipse(Brushes.Red, item.X - 2, item.Y - 2, 4, 4); } // collision area collided = Classes.Polygon.CollidedZone(collisionOption.Points); currentSquare = collided.S; MaxSquare = currentSquare; } else { collided = null; currentSquare = 0; } // showing all polygon1?.Show(graphics); polygon2?.Show(graphics); collided?.Show(graphics); // text graphics.DrawString(string.Format(TIME_MESSAGE_FORMAT, timeCounter), this.Font, Brushes.Black, 15, 10); graphics.DrawString(string.Format(SQUARE_MESSAGE_FORMAT, currentSquare), this.Font, Brushes.Black, 15, 30); } catch (System.Exception ex) { MessageBox.Show(ex.Message, ex.TargetSite.Name, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); timer.Stop(); timeCounter = 0; return; } }