Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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...

  • Leaders
Posted

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.

[sIGPIC]e[/sIGPIC]
Posted

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.?

  • Leaders
Posted

Here is a successful implementation of the distance formula:

[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.

[sIGPIC]e[/sIGPIC]
Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...