Vector2 closestPointToRectangle(Vector2 p, Rectangle r) { Vector2 rectPos = new Vector2(r.Left, r.Top); // relative position of p from the point 'p' Vector2 d = p - rectPos; // rectangle half-size Vector2 h = new Maths.Vector2((float)r.Width / 2f, (float)r.Height / 2f); // special case when the sphere centre is inside the rectangle if (Math.Abs(d.X) < h.X && Math.Abs(d.Y) < h.Y) { // use left or right side of the rectangle boundary // as it is the closest if ((h.X - Math.Abs(d.X)) < (h.Y - Math.Abs(d.Y))) { d.Y = 0.0f; d.X = h.X * Math.Sign(d.X); } // use top or bottom side of the rectangle boundary // as it is the closest else { d.X = 0.0f; d.Y = h.Y * Math.Sign(d.Y); } } else { // clamp to rectangle boundary if (Math.Abs(d.X) > h.X) { d.X = h.X * Math.Sign(d.X); } if (Math.Abs(d.Y) > h.Y) { d.Y = h.Y * Math.Sign(d.Y); } } // the closest point on rectangle from p Vector2 c = rectPos + d; return(c); }
/// <summary> /// /// </summary> /// <param name="pX"></param> /// <param name="pY"></param> /// <param name="pRadius"></param> /// <param name="pColor"></param> public override void DrawCircle(float pX, float pY, float pRadius, float pWidth, bool pDashed, Maths.Color4 pColor) { SMX.Maths.Vector2 start = GetScreenCoords(pX, pY); SMX.Maths.Vector2 radScreen = GetScreenCoords(pRadius, pRadius); Pen pen = new Pen(pColor.ToGDI(), pWidth); if (pDashed) { pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash; } else { pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid; } float width = radScreen.X * 2; mGraphics.DrawEllipse(pen, start.X - width / 2, start.Y - width / 2, width, width); }
/// <summary> /// /// </summary> /// <param name="pX"></param> /// <param name="pY"></param> /// <param name="pRadius"></param> /// <param name="pColor"></param> public override void DrawCircle(float pX, float pY, float pRadius, float pWidth, bool pDashed, Maths.Color4 pColor) { SolidColorBrush brush = new SolidColorBrush(mRenderTarget2D, pColor.ToSDX()); SharpDX.Vector2 start = GetScreenCoords(pX, pY).ToSDX(); SMX.Maths.Vector2 radScreen = GetScreenCoords(pRadius, pRadius); if (pDashed) { StrokeStyleProperties props = new StrokeStyleProperties(); props.DashCap = CapStyle.Flat; props.EndCap = CapStyle.Flat; props.StartCap = CapStyle.Flat; props.DashOffset = 0; props.DashStyle = DashStyle.Dash; props.LineJoin = LineJoin.Round; SharpDX.Direct2D1.StrokeStyle strokeStyle = new StrokeStyle(mFactory2D, props); mRenderTarget2D.DrawEllipse(new Ellipse(start, radScreen.X, radScreen.X), brush, pWidth, strokeStyle); } else { mRenderTarget2D.DrawEllipse(new Ellipse(start, radScreen.X, radScreen.X), brush, pWidth); } }