グルグルまわる

public class Loop
{
  private Storyboard storyboard = new Storyboard();

  public Loop()
  {
    storyboard.Completed += Storyboard_Completed;
  }

  private void Storyboard_Completed(object sender, EventArgs e)
  {
    if(handler != null) handler(this,EventArgs.Empty);
    storyboard.Begin();
  }
  
  private EventHandler handler;
  
  public event EventHandler Fire
  {
    add
    {
      handler += value;
      if(handler.GetInvocationList().Length == 1)
      {
        storyboard.Begin();
      }
    }
    remove
    {
      handler -= value;
      if(handler == null)
      {
        storyboard.Stop();
      }
    }
  }
}

上のコードは詳しくは昨日の日記で。


ここから、本編。

<UserControl x:Class="Rolling.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Width="300" Height="300" Loaded="UserControl_Loaded">
  <Canvas HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Black">
    <Path Fill="AliceBlue" StrokeThickness="1" Stroke="Blue" x:Name="target">
      <Path.Data>
        <EllipseGeometry Center="0.5,0.5" RadiusX="20" RadiusY="20" />
      </Path.Data>
    </Path>
  </Canvas>
</UserControl>
private double radius = 100;
private double speed = 0.04;
private double degree = 0;
private double centerX = 150;
private double centerY = 150;

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
  var loop = new Loop();
  loop.Fire += Loop_Fire;
}

private void Loop_Fire(object sender, EventArgs e)
{
  target.SetValue(Canvas.TopProperty, centerY + radius * Math.Sin(degree));
  target.SetValue(Canvas.LeftProperty, centerX + radius * Math.Cos(degree));
  degree += speed;
}


画像じゃ、あんまりわかりませんな・・・。


「Sin = 対辺/斜辺」なので「対辺 = 斜辺 × Sin」らしい。
「Cos = 隣辺/斜辺」なので「隣辺 = 斜辺 × Cos」らしい。

まだまだ、中学生。