EFileTahi-A Posted September 5, 2010 Posted September 5, 2010 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... Quote
EFileTahi-A Posted September 6, 2010 Author Posted September 6, 2010 Please, if my previous post was kinda confusing do let me know about it. I will gladly reformulate it! Thank you Quote
EFileTahi-A Posted September 11, 2010 Author Posted September 11, 2010 Just wondering is this so complicated? Quote
Leaders snarfblam Posted September 11, 2010 Leaders Posted September 11, 2010 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. Quote [sIGPIC]e[/sIGPIC]
EFileTahi-A Posted September 12, 2010 Author Posted September 12, 2010 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.? Quote
Leaders snarfblam Posted September 14, 2010 Leaders Posted September 14, 2010 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. Quote [sIGPIC]e[/sIGPIC]
EFileTahi-A Posted September 14, 2010 Author Posted September 14, 2010 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. Quote
Leaders snarfblam Posted September 18, 2010 Leaders Posted September 18, 2010 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. Quote [sIGPIC]e[/sIGPIC]
EFileTahi-A Posted September 19, 2010 Author Posted September 19, 2010 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.