public bool IsPointInside(Coordonnees toCheck) { if (IsValid()) { double sumAngle = 0; for (int i = 1; i <= NbPoints; i++) { ZZCoordinate previousPoint = (ZZCoordinate)Coordonnees[i - 1], anglePoint = (ZZCoordinate)toCheck, nextPoint = (ZZCoordinate)Coordonnees[i % NbPoints]; double alpha = ZZMath.AngleFrom3Points(previousPoint, anglePoint, nextPoint); double dx = anglePoint.X - previousPoint.X, dy = anglePoint.Y - previousPoint.Y; int sign = 1; if (dx == 0) // Vertical line { if (dy > 0) // The line goes upward { if (nextPoint.X > previousPoint.X) { sign = -1; } } else // The line goes downward { if (nextPoint.X < previousPoint.X) { sign = -1; } } } else { double m = dy / dx, p = previousPoint.Y - m * previousPoint.X; double slopeAngle = ZZMath.SlopeAngle(previousPoint, anglePoint); if (slopeAngle < 90 || slopeAngle > 270) // The point needs to be below the line { if (nextPoint.Y < m * nextPoint.Y + p) { sign = -1; } } else // The point needs to be above the line { if (nextPoint.Y > m * nextPoint.Y + p) { sign = -1; } } } sumAngle += sign * alpha; } Console.WriteLine(sumAngle); return(sumAngle == 360); // To be inside, the total of the angles needs to equal 360 } return(false); }
public bool IsPointInside(Coordonnees toCheck) { if (IsValid()) { double sumAngle = 0; for (int i = 1; i <= NbPoints; i++) { ZZCoordinate previousPoint = (ZZCoordinate)Coordonnees[i - 1], anglePoint = (ZZCoordinate)toCheck, nextPoint = (ZZCoordinate)Coordonnees[i % NbPoints]; double alpha = ZZMath.AngleFrom3Points(previousPoint, anglePoint, nextPoint); double dx = anglePoint.X - previousPoint.X, dy = anglePoint.Y - previousPoint.Y; int sign = 1; if (dx == 0) // Vertical line { if (dy > 0) // The line goes upward { if (nextPoint.X > previousPoint.X) { sign = -1; } } else // The line goes downward { if (nextPoint.X < previousPoint.X) { sign = -1; } } } else { double m = dy / dx, p = previousPoint.Y - m * previousPoint.X; double slopeAngle = ZZMath.SlopeAngle(anglePoint, previousPoint); double fx = m * nextPoint.X + p; if (slopeAngle < 90 || slopeAngle > 270) // The point needs to be below the line { if (nextPoint.Y > fx) { sign = -1; } } else // The point needs to be above the line { if (nextPoint.Y < fx) { sign = -1; } } } sumAngle += sign * alpha; } return((sumAngle > 359.9 && sumAngle < 360.1) || (sumAngle < -359.9 && sumAngle > -360.1)); // To be inside, the total of the angles needs to be above 359.9 or below 359.9, depending on the rotation sense made when creating the polygon (basicly == 360 but double precision doesn't always makes it work) } return(false); }
private bool IsValid() { if (NbPoints >= 3) // Is at least a triangle, 3 coordinates { for (int i = 0; i < NbPoints; i++) { ZZCoordinate previousPoint = (ZZCoordinate)Coordonnees[ZZFunctions.nmod(i - 1, NbPoints)], anglePoint = (ZZCoordinate)Coordonnees[i], nextPoint = (ZZCoordinate)Coordonnees[(i + 1) % NbPoints]; double alpha = ZZMath.AngleFrom3Points(previousPoint, anglePoint, nextPoint); if (alpha == 0) { return(false); // The angle formed by the 3 points isn't 0 degrees } } return(true); } return(false); }
public Polygon Extend(double extentionSize) { if (IsValid()) { List <Coordonnees> newCoordCollection = new List <Coordonnees>(); for (int i = 0; i < NbPoints; i++) { ZZCoordinate previousPoint = (ZZCoordinate)Coordonnees[ZZFunctions.nmod(i - 1, NbPoints)], anglePoint = (ZZCoordinate)Coordonnees[i], nextPoint = (ZZCoordinate)Coordonnees[(i + 1) % NbPoints]; double alpha = ZZMath.AngleFrom3Points(previousPoint, anglePoint, nextPoint), beta = (180 - alpha) / 2.0; double slopeAngle = ZZMath.SlopeAngle(previousPoint, anglePoint) + beta; double distanceToAdd = extentionSize / Math.Cos(ZZMath.ToRadians(beta)); double newX = anglePoint.X + distanceToAdd * Math.Cos(ZZMath.ToRadians(slopeAngle)); double newY = anglePoint.Y + distanceToAdd * Math.Sin(ZZMath.ToRadians(slopeAngle)); newCoordCollection.Add(new Coordonnees(newX, newY)); } return(new Polygon(newCoordCollection, BackgroundColor, BorderColor, Oppacity)); } return(this); }