How to draw a circle with C# and to be illustrated in WindowsForm

Viewed 77

everyone!

I have a problem with drawing a circle in C#. I need to illustrate the circle with the mouse in WindowsForm, that starts from certain points X and Y. Here is the code that I have, but the method Intersect() doesn't fit the needs of a circle to return the right illustration after that.

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace KursovaRabotaLibrary
{
    [Serializable]
    public class Circle : Shape
    {
        public override int Width { get; set; }
        public override int Height { get; set; }

        public override bool PointInShape(Point point)
        {
            return
                Location.X <= point.X && point.X <= Location.X + Width &&
                Location.Y <= point.Y && point.Y <= Location.Y + Height;
        }
        public override bool Intersect(Rectangle rectangle, Circle circle)
        {
            return
                Location.X < circle.Location.X + circle.Width && circle.Location.X < Location.X + Width &&
                Location.Y < circle.Location.Y + circle.Height && circle.Location.Y < Location.Y + Height;
        }
    }
}

Can you help me with defining better the method Intersect() to fit the needs of a circle? Thank you in advance!

1 Answers

You can split this complex test to a sequence of three simple tests:

  • If bounds of both objects do not intersect, then objects also do not intersect.
  • If the circle center is inside the rectangle (or at it's border), they do intersect.
  • If any rectangle's corner is inside the circle, they do intersect.

If the circle is really a circle and not an ellipse (Width == Height), and if the Intersect method is supposed to test the intersection of rectangle and circle arguments (ignore it's instance variables), then the code might look like this (untested):

public override bool PointInShape(Point point)
{
    var dx = point.X - Location.X;
    var dy = point.Y - Location.Y;
    // You can optimize the following
    var distance = Math.Sqrt(dx * dx + dy * dy);
    var radius = Width / 2;
    return distance < radius;
}

public override bool Intersect(Rectangle rectangle, Circle circle)
{
    var centerDistanceX = Math.Abs(circle.Location.X + circle.Width / 2 - (rectangle.Location.X + rectangle.Width / 2));
    var centerDistanceY = Math.Abs(circle.Location.Y + circle.Height / 2 - (rectangle.Location.Y + rectangle.Height / 2));

    var circleRadius = circle.Width / 2;

    // Are centers too far (bounds do not intersect)?
    if (centerDistanceX > (rectangle.Width / 2 + circleRadius))
        return false;
    if (centerDistanceY > (rectangle.Height / 2 + circleRadius))
        return false;

    // Is circle center inside the rectangle?
    if (centerDistanceX <= (rectangle.Width / 2))
        return true;
    if (centerDistanceY <= (rectangle.Height / 2))
        return true;

    // Is rectangle corner inside the circle?
    var dx = centerDistanceX - rectangle.Width / 2;
    var dy = centerDistanceY - rectangle.Height / 2;
    return dx * dx + dy * dy <= circleRadius * circleRadius;
}
Related