Click to See Complete Forum and Search --> : Some Co-ordinate help
TubbZ
May 14th, 2009, 12:53 PM
Hello,
I am creating an application for my local pub team to keep score of darts, i am using an image of a board which is then clicked per dart to register the score. I am using the co ordinates of the section on the image, for example:
double 13:
Top Left: (569 , 219)
Top Right: (595 , 294)
Bottom Left: (581 , 298)
Bottom Right: (557 , 227)
the issue being how do i mathematically calculate that say 571 , 260 is inside this range and only inside this shape?
Oh i am programming in VB.NET 2005 but i don't need code really but logic help :confused:
InertiaM
May 14th, 2009, 02:10 PM
Create a GraphicsPath for your Double 13 shape using the coordinates as listed - call it say gpDouble13. You can then use gpDouble13.IsVisible (571,260)
jemidiah
May 14th, 2009, 02:20 PM
Are these sections of pies cut out by the given coordinates? If they're just some sort of 4-sided figure InertiaM's solution sounds simple and direct, though I don't use .NET.
TubbZ
May 15th, 2009, 08:14 AM
ooh thats an idea, thanks!! i have spent a few too many hours of trying to get point inside polygon formulae to work, but they all seem to be slightly wrong for the points passed so i will just have to try this instead :p Thanks
Logophobic
May 15th, 2009, 01:00 PM
You're making it way too complicated. Measure the distance from center and angle from horizontal. That's all you need.
jemidiah
May 16th, 2009, 12:42 AM
You're making it way too complicated. Measure the distance from center and angle from horizontal. That's all you need.
Well, again it depends if these graphics are, say, trapezoidal regions that you click, or if they're actually pie sectors.
TubbZ
May 16th, 2009, 05:59 AM
No i haven't cut the image up into shapes, i have never really touched any graphics functions in vb.net so i just considered using co-ordinates, how easy is it to create custom click able shapes?
InertiaM
May 16th, 2009, 06:30 AM
Yes, it's easy. This code should get you started :-
Option Explicit On
Option Strict On
Imports SYSTEM.Drawing.Drawing2D
Public Class Form1
Private CurrentMouseLocation As Point
Private gpDouble13 As New GraphicsPath
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
Me.SetStyle(ControlStyles.UserPaint, True)
gpDouble13.AddPolygon(New PointF() {New PointF(569, 219), New PointF(595, 294), New PointF(581, 298), New PointF(557, 227)})
End Sub
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
If gpDouble13.IsVisible(CurrentMouseLocation) Then
MessageBox.Show("You clicked on Double 13")
End If
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
'store the mouse location every time the mouse moves
CurrentMouseLocation = e.Location
'repaint form
Me.Invalidate()
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
With e.Graphics
If gpDouble13.IsVisible(CurrentMouseLocation) Then
'mouse over double 13, paint in green
.FillPath(Brushes.Green, gpDouble13)
Else
'mouse NOT over double 13, paint in red
.FillPath(Brushes.Red, gpDouble13)
End If
.DrawPath(Pens.Black, gpDouble13)
End With
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'If PanelToStack.Bounds.IntersectsWith(New Rectangle(-51, -51, 50, 50)) = True Then
' MessageBox.Show("Hit")
'End If
End Sub
End Class
Logophobic
May 16th, 2009, 09:44 AM
Well, again it depends if these graphics are, say, trapezoidal regions that you click, or if they're actually pie sectors.
There are no trapezoidal regions on a dartboard. The example given in the OP is only an approximation of the actual region and is most definitely not the best approach. Instead of trying to make trapezoids work (you would have to test each of the 82 regions until you get a hit), simply use the angle to find the base value (1 to 20) and the radius to find the multiplier (single, double, or triple). The radius will also tell you if the point is in one of the two central regions of the dartboard, or not within the scoring area at all.
jemidiah
May 16th, 2009, 12:01 PM
I know how a dart board looks :). I'd agree that procedurally generating the regions as you describe is the cleanest way to go, but I could also see someone approximating the dartboard. Hence, I've asked which way it is (twice :D).
TubbZ
May 17th, 2009, 01:33 PM
Oooh thanks for the code, i am not really entirely sure the best approach and i now see what you mean about the angles and the distance. Makes much more sense than drawing all regions and testing them :p
Personally the quickest and cleanest way i would obviously be best :D
Thanks for your help
EDIT:
Thanks Jemidiah, i did it your way and just finished. I sometimes over complicate problems and this was a classic example of that!! and thanks to Logophobic for your code on 2d graphics :)
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.