/// <summary> /// Clip a line to the shape /// </summary> /// <param name="sourcePoint">The source of the line</param> /// <returns>The point which clips at the boundary</returns> internal override PointF ClipLine(PointF sourcePoint) { PointF intersect; if (GraphUtils.IntersectLine(sourcePoint, this.Center, new PointF(Boundary.Left, Boundary.Top), new PointF(Boundary.Left, Boundary.Bottom), out intersect)) { return(intersect); } if (GraphUtils.IntersectLine(sourcePoint, this.Center, new PointF(Boundary.Left, Boundary.Top), new PointF(Boundary.Right, Boundary.Top), out intersect)) { return(intersect); } if (GraphUtils.IntersectLine(sourcePoint, this.Center, new PointF(Boundary.Left, Boundary.Bottom), new PointF(Boundary.Right, Boundary.Bottom), out intersect)) { return(intersect); } if (GraphUtils.IntersectLine(sourcePoint, this.Center, new PointF(Boundary.Right, Boundary.Top), new PointF(Boundary.Right, Boundary.Bottom), out intersect)) { return(intersect); } return(Center); }
/// <summary> /// Intersect a line with a rectangle, returns the first intersecting point /// </summary> /// <param name="boundary">The rectangle boundary</param> /// <param name="sourcePoint">The source point of the line</param> /// <param name="destPoint">The destination point of the line</param> /// <param name="intersect">Parameter to store the intersection point if found</param> /// <returns>True if the line intersected at least one side of the rectangle</returns> public static bool IntersectRectangle(RectangleF boundary, PointF sourcePoint, PointF destPoint, out PointF intersect) { if (IntersectLine(sourcePoint, destPoint, new PointF(boundary.Left, boundary.Top), new PointF(boundary.Left, boundary.Bottom), out intersect)) { return(true); } if (GraphUtils.IntersectLine(sourcePoint, destPoint, new PointF(boundary.Left, boundary.Top), new PointF(boundary.Right, boundary.Top), out intersect)) { return(true); } if (GraphUtils.IntersectLine(sourcePoint, destPoint, new PointF(boundary.Left, boundary.Bottom), new PointF(boundary.Right, boundary.Bottom), out intersect)) { return(true); } if (GraphUtils.IntersectLine(sourcePoint, destPoint, new PointF(boundary.Right, boundary.Top), new PointF(boundary.Right, boundary.Bottom), out intersect)) { return(true); } return(false); }
/// <summary> /// Determine if this point hits the object /// </summary> /// <param name="hit"></param> /// <returns></returns> public override bool HitTest(PointF hit) { PointF center = Center; float radius2 = GraphUtils.Square(Boundary.Width / 2); float distance2 = GraphUtils.Square(hit.X - center.X) + GraphUtils.Square(hit.Y - center.Y); return(distance2 < radius2); }
public virtual bool HitTest(PointF hit) { if ((SourceShape != null) && (DestShape != null)) { RectangleF hitbox = new RectangleF(hit.X - (HIT_SIZE / 2.0f), hit.Y - (HIT_SIZE / 2.0f), HIT_SIZE, HIT_SIZE); PointF intersect; return(GraphUtils.IntersectRectangle(hitbox, SourceShape.Center, DestShape.Center, out intersect)); } else { return(false); } }
/// <summary> /// Clip the line destination to be outside of the shape if possible /// </summary> /// <param name="sourcePoint">The source point of the line</param> /// <returns>The clipped line position</returns> internal override PointF ClipLine(PointF sourcePoint) { float relX = Center.X - sourcePoint.X; float relY = Center.Y - sourcePoint.Y; float length = (float)Math.Sqrt(GraphUtils.Square(relX) + GraphUtils.Square(relY)); float newLength = length - Boundary.Width / 2.0f; // If we have a length which is less that 0 then return just the center point if (newLength < 0) { return(Center); } return(new PointF(sourcePoint.X + ((relX * newLength) / length), sourcePoint.Y + ((relY * newLength) / length))); }
/// <summary> /// Clip the line, defaults to flattening the path and intersecting every line. /// If a more efficient method is available it can be overridden /// </summary> /// <param name="sourcePoint"></param> /// <returns></returns> internal virtual PointF ClipLine(PointF sourcePoint) { PointF intersect = Center; float distance = GraphUtils.CalculateDistance(sourcePoint, Center); using (GraphicsPath path = GetPath()) { path.Flatten(); if (path.PointCount > 0) { PointF lastPoint = path.GetLastPoint(); // Intersect all the lines in the path with our line and choose the closest for (int i = 0; i < path.PointCount; ++i) { PointF currIntersect; if (GraphUtils.IntersectLine(sourcePoint, this.Center, lastPoint, path.PathPoints[i], out currIntersect)) { float newDistance = GraphUtils.CalculateDistance(sourcePoint, currIntersect); if (newDistance < distance) { distance = newDistance; intersect = currIntersect; } } lastPoint = path.PathPoints[i]; } } } return(intersect); }