How do you determine the position of an image when it stops rotating?
I want to create a wheel steering for my school project using Visual studio 2012. What I have now is an image, and it can rotate when I press the button and stop at random positions. However I am not sure how to determine the position when the image stops rotating. This is the trigger button event when the image rotates when I click the button.
private void SpinBtn_Click(object sender, RoutedEventArgs e)
{
var ease = new PowerEase { EasingMode = EasingMode.EaseOut };
Random rng = new Random(Guid.NewGuid().GetHashCode());
//DoubleAnimation(FromValue. ToValue, Duration)
DoubleAnimation myanimation = new DoubleAnimation
(0, rng.Next(360,720), new Duration(TimeSpan.FromSeconds(3)));
//Adding Power ease to the animation
myanimation.EasingFunction = ease;
RotateTransform rotate = new RotateTransform();
img.RenderTransform = rotate;
img.RenderTransformOrigin = new Point(0.5, 0.5);
rotate.BeginAnimation(RotateTransform.AngleProperty, myanimation);
}
How do I determine the position of the image (where the pointer is pointing) once it stops rotating? So when the pointer points to this object, I can drag the words into a specific text box.
Refer to the image .
source to share
you don't need to know where the image (pointer) is, just calculate the degree of rotation.
double degree = rng.Next(360, 720);
DoubleAnimation myanimation = new DoubleAnimation
(0, degree, new Duration(TimeSpan.FromSeconds(3)));
double result_degree = degree % 360;
now you can get the target with result_degree.
eg:
you have 12 objects in a circle like a clock, so the 1st degree of the object is from 0 to 29, the second is from 30 to 59 ....
or you can set the degree of the object yourself, 1st is from 0 to 9, 2nd is from 10 to 39 ...
about drag and drop:
I'll give you a simple example:
<Grid>
<TextBox x:Name="tbResult" HorizontalAlignment="Left" AllowDrop="True" Height="23" Margin="416,245,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
<TextBlock x:Name="tb1" HorizontalAlignment="Left" MouseLeftButtonDown="MyMouseLeftButtonDown" Margin="280,210,0,0" TextWrapping="Wrap" Text="ob1" VerticalAlignment="Top"/>
<TextBlock x:Name="tb2" IsEnabled="False" HorizontalAlignment="Left" MouseLeftButtonDown="MyMouseLeftButtonDown" Margin="280,245,0,0" TextWrapping="Wrap" Text="ob2" VerticalAlignment="Top"/>
<TextBlock x:Name="tb3" IsEnabled="False" HorizontalAlignment="Left" MouseLeftButtonDown="MyMouseLeftButtonDown" Margin="280,281,0,0" TextWrapping="Wrap" Text="ob3" VerticalAlignment="Top"/>
</Grid>
private void MyMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
TextBlock tb = sender as TextBlock ;
if(tb != null && tb.IsEnabled == true)
{
switch(tb.Name)
{
case "tb1" :
DragDrop.DoDragDrop(tb1, tb1.Text, DragDropEffects.Copy);
break;
case "tb2":
DragDrop.DoDragDrop(tb2, tb2.Text, DragDropEffects.Copy);
break;
case "tb3":
DragDrop.DoDragDrop(tb3, tb3.Text, DragDropEffects.Copy);
break;
}
}
}
when you get result_degree and you know which object is selected, set it IsEnable = true
and others IsEnable = false
, set the TextBox AllowDrop = true
. For this example, just allow the user to drag and drop ob1
.
source to share