Detect if mouse moves in a circle

I am trying to implement mouse tracking. When the mouse moves in a circle or rectangle, a specific message is displayed.

bool IsWithinCircle(int centerX, int centerY, int mouseX, int mouseY, double radius) 
{
    int diffX = centerX - mouseX;
    int diffY = centerY - mouseY;
    return  (diffX * diffX + diffY * diffY) <= radius * radius;
}

I determine the shape of the circle using this function using the location of the mouse. any other way to detect mouse movement?
Could you give a little example code or link?

+4
source share
3 answers

You probably want to track mouse movements as a series of line segments and use these line segments to create a circle approximation. If the center of the circle remains relatively consistent, the user moves in a circle.

, . ( - ) , . , . , , , .

two consecutive line segments approximating a circle

:

  • 100,0 104,2 106,6, 2
  • Δy/Δx. -Δx/Δy
  • 1 102,1 -1/2 (atan2(-1,2) -26°)
  • 2 105,4 -2/1 (atan2(-2,1) -63°)
  • 99,7
  • 99 ± t, 7 ± t, t - ,
  • ( ) ,

, :

  • , , , , - ., .
  • , .
+2

... , , . , MouseMove, Point reset.

Point, , ( ) ( ).

enter image description here

WPF:

<Window x:Class="Shaping.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Canvas x:Name="ShapeCanvas" Background="Transparent" />
        <Ellipse Width="100" Height="100" Stroke="Black" StrokeThickness="1" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Grid>
</Window>

:

public partial class MainWindow : Window
{
    List<Point> points;
    Timer reset;

    public MainWindow()
    {
        InitializeComponent();

        points = new List<Point>();
        reset = new Timer { AutoReset = false, Interval = 500 };
        reset.Elapsed += (s, e) =>
        {
            App.Current.Dispatcher.BeginInvoke((Action)(() =>
            {
                ShapeCanvas.Children.Clear();
            }));

            if (points.Count > 10)
            {

                double? max_x = null;
                double? min_x = null;
                double? max_y = null;
                double? min_y = null;

                // evaulate points to determine center
                foreach (var point in points)
                {
                    max_x = Math.Max(max_x ?? point.X, point.X);
                    min_x = Math.Min(min_x ?? point.X, point.X);
                    max_y = Math.Max(max_y ?? point.Y, point.Y);
                    min_y = Math.Min(min_y ?? point.Y, point.Y);
                }
                var center = new Point((((double)max_x - (double)min_x) / 2) + (double)min_x, (((double)max_y - (double)min_y) / 2) + (double)min_y);

                var count_r = 0;
                var sum_r = 0d;
                var all_r = new List<double>();
                var quadrands = new int[4];

                // evaluate points again to determine quadrant and to get all distances and average distance
                foreach (var point in points)
                {
                    // distance
                    var r = Math.Sqrt(Math.Pow(point.X - center.X, 2) + Math.Pow(point.Y - center.Y, 2));
                    sum_r += r;
                    count_r += 1;
                    all_r.Add(r);

                    // quadrand
                    quadrands[(point.Y > center.Y ? 1 : 0) + (point.X > center.X ? 2 : 0)] += 1;
                }
                var r_avg = sum_r / count_r;

                if (all_r.Count(r => Math.Abs(r - r_avg) < r_avg * .3) >= count_r * .8 && quadrands.All(q => q > 1))
                {
                    // you made a circle
                    App.Current.Dispatcher.BeginInvoke((Action)(() =>
                    {
                        ShapeCanvas.Children.Clear();
                        ShapeCanvas.Children.Add(new Ellipse() { Fill = new SolidColorBrush(Colors.Red), Width = 15, Height = 15 });
                        reset.Start();
                    }));
                }
            }

            points.Clear();
        };

        ShapeCanvas.MouseMove += (s, e) =>
        {
            points.Add(e.GetPosition((Canvas)s));
            reset.Stop();
            reset.Start();
        };

    }
}

quadrands.All(q=> q > 1) - quadrants.All(q=> Math.Abs(q - avg_quad) < avg_quad * .3, , ( , , )

.3 .8 - , roughly a circle -, : 80% +15% .

+1

github repo. , .

-1

All Articles