/// <summary> /// Returns true if the element intersect the rectangle from the parent class. /// </summary> /// <param name="rect">The rectangle to test must be in the virtual modeling coordinant plane.</param> /// <returns>True if the element intersect the rectangle from the parent class.</returns> public override bool ElementInRectangle(Rectangle rect) { Geometry rectanglePoly; if ((rect.Height == 0) && (rect.Width == 0)) { rectanglePoly = new NetTopologySuite.Geometries.Point(rect.X, rect.Y); } else if (rect.Width == 0) { Coordinate[] rectanglePoints = new Coordinate[2]; rectanglePoints[0] = new Coordinate(rect.X, rect.Y); rectanglePoints[1] = new Coordinate(rect.X, rect.Y + rect.Height); rectanglePoly = new LineString(rectanglePoints); } else if (rect.Height == 0) { Coordinate[] rectanglePoints = new Coordinate[2]; rectanglePoints[0] = new Coordinate(rect.X, rect.Y); rectanglePoints[1] = new Coordinate(rect.X + rect.Width, rect.Y); rectanglePoly = new LineString(rectanglePoints); } else { Coordinate[] rectanglePoints = new Coordinate[5]; rectanglePoints[0] = new Coordinate(rect.X, rect.Y); rectanglePoints[1] = new Coordinate(rect.X, rect.Y + rect.Height); rectanglePoints[2] = new Coordinate(rect.X + rect.Width, rect.Y + rect.Height); rectanglePoints[3] = new Coordinate(rect.X + rect.Width, rect.Y); rectanglePoints[4] = new Coordinate(rect.X, rect.Y); rectanglePoly = new Polygon(new LinearRing(rectanglePoints)); } if (Shape == ModelShape.Arrow) { Coordinate[] arrowPoints = new Coordinate[_arrowPath.PointCount]; for (int i = 0; i < _arrowPath.PointCount; i++) { arrowPoints[i] = new Coordinate(_arrowPath.PathPoints[i].X + Location.X, _arrowPath.PathPoints[i].Y + Location.Y); } LineString arrowLine = new LineString(arrowPoints); return(arrowLine.Intersects(rectanglePoly)); } return(false); }
/// <summary> /// Returns true if the element intersects the rectangle from the parent class. /// </summary> /// <param name="rect">The rectangle to test must be in the virtual modeling coordinant plane</param> /// <returns>True, if the element intersects the rectangle from the parent class.</returns> public virtual bool ElementInRectangle(Rectangle rect) { IGeometry rectanglePoly; if (rect.Height == 0 && rect.Width == 0) { rectanglePoly = new NetTopologySuite.Geometries.Point(rect.X, rect.Y); } else if (rect.Width == 0) { Coordinate[] rectanglePoints = new Coordinate[2]; rectanglePoints[0] = new Coordinate(rect.X, rect.Y); rectanglePoints[1] = new Coordinate(rect.X, rect.Y + rect.Height); rectanglePoly = new LineString(rectanglePoints); } else if (rect.Height == 0) { Coordinate[] rectanglePoints = new Coordinate[2]; rectanglePoints[0] = new Coordinate(rect.X, rect.Y); rectanglePoints[1] = new Coordinate(rect.X + rect.Width, rect.Y); rectanglePoly = new LineString(rectanglePoints); } else { Coordinate[] rectanglePoints = new Coordinate[5]; rectanglePoints[0] = new Coordinate(rect.X, rect.Y); rectanglePoints[1] = new Coordinate(rect.X, rect.Y + rect.Height); rectanglePoints[2] = new Coordinate(rect.X + rect.Width, rect.Y + rect.Height); rectanglePoints[3] = new Coordinate(rect.X + rect.Width, rect.Y); rectanglePoints[4] = new Coordinate(rect.X, rect.Y); rectanglePoly = new Polygon(new LinearRing(rectanglePoints)); } switch (Shape) { case ModelShape.Rectangle: return(rect.IntersectsWith(Rectangle)); case ModelShape.Ellipse: int b = Height / 2; int a = Width / 2; Coordinate[] ellipsePoints = new Coordinate[(4 * a) + 1]; for (int x = -a; x <= a; x++) { if (x == 0) { ellipsePoints[x + a] = new Coordinate(Location.X + x + a, Location.Y); ellipsePoints[(3 * a) - x] = new Coordinate(Location.X + x + a, Location.Y + Height); } else { ellipsePoints[x + a] = new Coordinate(Location.X + x + a, Location.Y + b - Math.Sqrt(Math.Abs(((b * b * x * x) / (a * a)) - (b * b)))); ellipsePoints[(3 * a) - x] = new Coordinate(Location.X + x + a, Location.Y + b + Math.Sqrt(Math.Abs(((b * b * x * x) / (a * a)) - (b * b)))); } } Polygon ellipsePoly = new Polygon(new LinearRing(ellipsePoints)); return(ellipsePoly.Intersects(rectanglePoly)); case ModelShape.Triangle: Coordinate[] trianglePoints = new Coordinate[4]; trianglePoints[0] = new Coordinate(Location.X, Location.Y); trianglePoints[1] = new Coordinate(Location.X, Location.Y + Height); trianglePoints[2] = new Coordinate(Location.X + Width - 5, Location.Y + ((Height - 5) / 2)); trianglePoints[3] = new Coordinate(Location.X, Location.Y); Polygon trianglePoly = new Polygon(new LinearRing(trianglePoints)); return(trianglePoly.Intersects(rectanglePoly)); default: return(false); } }