Reading circle radius from mouse coords

EFileTahi-A

Contributor
Joined
Aug 8, 2004
Messages
623
Location
Portugal / Barreiro
Imagine you have a circle with a radius of 100 pixels at the center of the screen. I need to return the current radius of the circle where ever I put the mouse anywhere inside the circle. That is.. if I put the mouse half way inside the circle it should return 50 pixels.

I have no clue how to do this...
 
It's not too complicated. I overlooked your question because it was one among many topics that got spammed in the same day.

I think what you are looking for is the distance formula:
distance = Sqrt( (x2 - x1) ^ 2 + (y2 - y1) ^ 2 )

You would just want to calculate the distance between the center of the circle and the mouse position.
 
Hi, thanks for the reply.

I tried that formula but it does not work... I get odd values, like 12 pixels at the distance of 300 pixels away from the circle's center. And sometimes I get NaN result.?
 
Here is a successful implementation of the distance formula:
Code:
[Color=Blue]using[/Color] System;
[Color=Blue]using[/Color] System[Color=Red].[/Color]Drawing;
[Color=Blue]using[/Color] System[Color=Red].[/Color]Windows[Color=Red].[/Color]Forms;

namespace WindowsFormsApplication1
[B]{[/B]
    [Color=Blue]public[/Color] [Color=Blue]partial[/Color] [Color=Blue]class[/Color] Form1 [Color=Red]:[/Color] Form
    [B]{[/B]
        [Color=Green]// Location and size of bulls-eye[/Color]
        [Color=Blue]const[/Color] [Color=Blue]int[/Color] originX [Color=Red]=[/Color] 100;
        [Color=Blue]const[/Color] [Color=Blue]int[/Color] originY [Color=Red]=[/Color] 100;

        [Color=Blue]const[/Color] [Color=Blue]int[/Color] outerRadius [Color=Red]=[/Color] 10;
        [Color=Blue]const[/Color] [Color=Blue]int[/Color] innerRadius [Color=Red]=[/Color] 2;

        [Color=Blue]protected[/Color] [Color=Blue]override[/Color] [Color=Blue]void[/Color] OnPaint[B]([/B]PaintEventArgs e[B])[/B] [B]{[/B]
            
            base[Color=Red].[/Color]OnPaint[B]([/B]e[B])[/B];

            [Color=Green]// Draw our bulls-eye[/Color]
            e[Color=Red].[/Color]Graphics[Color=Red].[/Color]DrawEllipse[B]([/B]Pens[Color=Red].[/Color]Black, [Color=Blue]new[/Color] [Color=Blue]Rectangle[/Color][B]([/B]originX [Color=Red]-[/Color] outerRadius, originY [Color=Red]-[/Color] outerRadius, outerRadius [Color=Red]*[/Color] 2, outerRadius [Color=Red]*[/Color] 2[B])[/B][B])[/B];
            e[Color=Red].[/Color]Graphics[Color=Red].[/Color]FillEllipse[B]([/B]Brushes[Color=Red].[/Color]Red, [Color=Blue]new[/Color] [Color=Blue]Rectangle[/Color][B]([/B]originX [Color=Red]-[/Color] innerRadius, originY [Color=Red]-[/Color] innerRadius, innerRadius [Color=Red]*[/Color] 2, innerRadius [Color=Red]*[/Color] 2[B])[/B][B])[/B];
        [B]}[/B]

        [Color=Blue]protected[/Color] [Color=Blue]override[/Color] [Color=Blue]void[/Color] OnMouseMove[B]([/B]MouseEventArgs e[B])[/B] [B]{[/B]
            base[Color=Red].[/Color]OnMouseMove[B]([/B]e[B])[/B];

            [Color=Green]// X and Y Distance from bulls-eye[/Color]
            [Color=Blue]int[/Color] dx [Color=Red]=[/Color] e[Color=Red].[/Color]X [Color=Red]-[/Color] originX;
            [Color=Blue]int[/Color] dy [Color=Red]=[/Color] e[Color=Red].[/Color]Y [Color=Red]-[/Color] originY;

            [Color=Green]// Distance formula: Dist = Sqrt(dX ^ 2 + dY ^ 2)[/Color]
            var dist [Color=Red]=[/Color] Math[Color=Red].[/Color]Sqrt[B]([/B]dx [Color=Red]*[/Color] dx [Color=Red]+[/Color] dy [Color=Red]*[/Color] dy[B])[/B];

            [Color=Green]// Show Dist[/Color]
            [Color=Blue]this[/Color][Color=Red].[/Color]Text [Color=Red]=[/Color] dist[Color=Red].[/Color]ToString[B]([/B][Color=DarkRed]"0.00"[/Color][B])[/B];
        [B]}[/B]
    [B]}[/B]
[B]}[/B]
Without seeing your code I couldn't even guess where the problem may lie.
 
Thank you!

Your example was simple awesome!

Do you mind I add your name / nickname to my credit list? If so, what name should I include.

Thank you again! And please, post back.
 
I don't think there is much credit due; I didn't come up with the formula. At any rate, if you really feel so compelled I'd say just leave it at snarfblam.
 
It might be simple what you did, but the truth is that not one else managed to help me (I post this problem of mine also at other forums).

Consider yourself added to my credit list.

Thank you once more.
 
Back
Top