开发工具:
文件大小: 3mb
下载次数: 0
上传时间: 2013-04-22
详细说明:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.Windows.Media.Animation; using System.Windows.Media.Media3D; using System.Collections; namespace RubiksCube { /// /// MainPanel.xaml 的交互逻辑 /// public partial class Cube : Window { private ModelUIElement3D[][][] cubeArray = null; private Vector3D[][][] cubeAxis = null; private List sbList = new List(); public Cube() { InitializeComponent(); InitCube(); } #region 初始化及还原操作 /// /// 初始化魔方 /// private void InitCube() { cubeArray = new ModelUIElement3D[3][][]; cubeAxis = new Vector3D[3][][]; for (int i = 0; i < 3; i++) { cubeArray[i] = new ModelUIElement3D[3][]; cubeAxis[i] = new Vector3D[3][]; for (int j = 0; j < 3; j++) { cubeArray[i][j] = new ModelUIElement3D[3]; cubeAxis[i][j] = new Vector3D[3]; for (int k = 0; k < 3; k++) { ModelUIElement3D modelElement = new ModelUIElement3D(); Model3DGroup modelGroup = (Model3DGroup)FindResource( string.Format("CubePanel__{0}{1}{2}", i + 1, j + 1, k + 1)); modelElement.Model = modelGroup; modelElement.Transform = new Transform3DGroup(); cubeArray[i][j][k] = modelElement; viewport.Children.Add(cubeArray[i][j][k]); cubeAxis[i][j][k] = new Vector3D( (int)CenterAxis.PositiveX, (int)CenterAxis.PositiveY, (int)CenterAxis.PositiveZ); } } } cubeArray[0][1][1].MouseDown += forePanel_MouseDown; cubeArray[2][1][1].MouseDown += backPanel_MouseDown; cubeArray[1][1][0].MouseDown += leftPanel_MouseDown; cubeArray[1][1][2].MouseDown += rightPanel_MouseDown; cubeArray[1][0][1].MouseDown += topPanel_MouseDown; cubeArray[1][2][1].MouseDown += bottomPanel_MouseDown; } /// /// 还原魔方 /// private void ResetCube() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { cubeArray[i][j][k].Transform = new Transform3DGroup(); cubeArray[i][j][k].Model = (Model3DGroup)FindResource( string.Format("CubePanel__{0}{1}{2}", i + 1, j + 1, k + 1)); cubeAxis[i][j][k] = new Vector3D( (int)CenterAxis.PositiveX, (int)CenterAxis.PositiveY, (int)CenterAxis.PositiveZ); } } } } /// /// 获取一个值,该值指示魔方是否已被复位 /// private bool IsReseted { get { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { Vector3D vector = cubeAxis[i][j][k]; if (vector.X != (int)CenterAxis.PositiveX || vector.Y != (int)CenterAxis.PositiveY || vector.Z != (int)CenterAxis.PositiveZ) { return false; } } } } return true; } } /// /// 随机旋转魔方 /// private Random r = new Random(); private void RandomRotate() { int axis = r.Next(1, 4); axis *= 2 * r.Next(0, 2) - 1; int direct = 2 * r.Next(0, 2) - 1; RotatePanel((CenterAxis)axis, (Direction)direct, 100); } #endregion #region 事件处理 // 前 private void forePanel_MouseDown(object sender, MouseButtonEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { RotatePanel(CenterAxis.PositiveZ, Direction.Counterclockwise, 100); } else if (e.RightButton == MouseButtonState.Pressed) { RotatePanel(CenterAxis.PositiveZ, Direction.Clockwise, 100); } ShowWinMsg(); e.Handled = true; } // 后 private void backPanel_MouseDown(object sender, MouseButtonEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { RotatePanel(CenterAxis.NegativeZ, Direction.Counterclockwise, 100); } else if (e.RightButton == MouseButtonState.Pressed) { RotatePanel(CenterAxis.NegativeZ, Direction.Clockwise, 100); } ShowWinMsg(); e.Handled = true; } // 左 private void leftPanel_MouseDown(object sender, MouseButtonEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { RotatePanel(CenterAxis.NegativeX, Direction.Counterclockwise, 100); } else if (e.RightButton == MouseButtonState.Pressed) { RotatePanel(CenterAxis.NegativeX, Direction.Clockwise, 100); } ShowWinMsg(); e.Handled = true; } // 右 private void rightPanel_MouseDown(object sender, MouseButtonEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { RotatePanel(CenterAxis.PositiveX, Direction.Counterclockwise, 100); } else if (e.RightButton == MouseButtonState.Pressed) { RotatePanel(CenterAxis.PositiveX, Direction.Clockwise, 100); } ShowWinMsg(); e.Handled = true; } // 上 private void topPanel_MouseDown(object sender, MouseButtonEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { RotatePanel(CenterAxis.PositiveY, Direction.Counterclockwise, 100); } else if (e.RightButton == MouseButtonState.Pressed) { RotatePanel(CenterAxis.PositiveY, Direction.Clockwise, 100); } ShowWinMsg(); e.Handled = true; } // 下 private void bottomPanel_MouseDown(object sender, MouseButtonEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { RotatePanel(CenterAxis.NegativeY, Direction.Counterclockwise, 100); } else if (e.RightButton == MouseButtonState.Pressed) { RotatePanel(CenterAxis.NegativeY, Direction.Clockwise, 100); } ShowWinMsg(); e.Handled = true; } private void ShowWinMsg() { if (IsReseted) { MessageBox.Show("哈哈,成功咯"); } } #endregion #region 对魔方的各种操作 /// /// 旋转魔方面板 /// /// 旋转中心轴 /// 旋转方向 /// 旋转时间(以毫秒为单位) private void RotatePanel(CenterAxis axis, Direction direction, double time) { Position[] position = null; switch (axis) { case CenterAxis.PositiveX: // X正方向 position = new Position[] { new Position(0, 0, 2), new Position(1, 0, 2), new Position(2, 0, 2), new Position(0, 1, 2), new Position(1, 1, 2), new Position(2, 1, 2), new Position(0, 2, 2), new Position(1, 2, 2), new Position(2, 2, 2) }; break; case CenterAxis.PositiveY: // Y正方向 position = new Position[] { new Position(2, 0, 0), new Position(2, 0, 1), new Position(2, 0, 2), new Position(1, 0, 0), new Position(1, 0, 1), new Position(1, 0, 2), new Position(0, 0, 0), new Position(0, 0, 1), new Position(0, 0, 2) }; break; case CenterAxis.PositiveZ: // Z正方向 position = new Position[] { new Position(0, 0, 0), new Position(0, 0, 1), new Position(0, 0, 2), new Position(0, 1, 0), new Position(0, 1, 1), new Position(0, 1, 2), new Position(0, 2, 0), new Position(0, 2, 1), new Position(0, 2, 2) }; break; case CenterAxis.NegativeX: // X负方向 position = new Position[] { new Position(2, 0, 0), new Position(1, 0, 0), new Position(0, 0, 0), new Position(2, 1, 0), new Position(1, 1, 0), new Position(0, 1, 0), new Position(2, 2, 0), new Position(1, 2, 0), new Position(0, 2, 0) }; break; case CenterAxis.NegativeY: // Y负方向 position = new Position[] { new Position(0, 2, 0), new Position(0, 2, 1), new Position(0, 2, 2), new Position(1, 2, 0), new Position(1, 2, 1), new Position(1, 2, 2), new Position(2, 2, 0), new Position(2, 2, 1), new Position(2, 2, 2) }; break; case CenterAxis.NegativeZ: // Z负方向 position = new Position[] { new Position(2, 0, 2), new Position(2, 0, 1), new Position(2, 0, 0), new Position(2, 1, 2), new Position(2, 1, 1), new Position(2, 1, 0), new Position(2, 2, 2), new Position(2, 2, 1), new Position(2, 2, 0) }; break; } RotatePanelCube(position, axis, direction, time); TransformPanelPostion(position, direction); for (int i = 0; i < 9; i++) { FixCubeAxis(position[i], axis, direction); } } /// /// 旋转魔方,并播放动画 /// /// 当前被旋转面板的各小方块的索引数组 /// 旋转中心轴 /// 旋转方向 /// 动画完成时间(以毫秒为单位) private void RotatePanelCube(Position[] position, CenterAxis centeraxis, Direction direction, double time) { if (position.Length != 9) { throw new Exception("数组包含元素个数必须为9"); } Vector3D axis = new Vector3D(1, 0, 0); int axisIndex = 0; double angle = 0; switch (centeraxis) { case CenterAxis.PositiveX: axis = new Vector3D(1, 0, 0); angle = -90; break; case CenterAxis.PositiveY: axis = new Vector3D(0, 1, 0); angle = -90; break; case CenterAxis.PositiveZ: axis = new Vector3D(0, 0, 1); angle = -90; break; case CenterAxis.NegativeX: axis = new Vector3D(1, 0, 0); angle = 90; break; case CenterAxis.NegativeY: axis = new Vector3D(0, 1, 0); angle = 90; break; case CenterAxis.NegativeZ: axis = new Vector3D(0, 0, 1); angle = 90; break; } if (direction == Direction.Counterclockwise) { angle = -angle; } // 播放动画 Storyboard sb = new Storyboard(); for (int i = 0; i < 9; i++) { ModelUIElement3D model = GetCubeModel(position[i]); Transform3DGroup transform = (Transform3DGroup)model.Transform; axisIndex = transform.Children.Count; transform.Children.Add(new RotateTransform3D(new AxisAngleRotation3D(axis, 0))); DoubleAnimation animation = new DoubleAnimation(); animation.By = angle; animation.Duration = new Duration(TimeSpan.FromMilliseconds(time)); Storyboard.SetTarget(animation, model); Storyboard.SetTargetProperty(animation, new PropertyPath(string.Format("Transform.Children[{0}].Rotation.Angle", axisIndex))); sb.Children.Add(animation); } sb.Completed += sb_Completed; sbList.Add(sb); if (sbList.Count == 1) { sb.Begin(); } } private void sb_Completed(object sender, EventArgs e) { sbList.RemoveAt(0); if (sbList.Count > 0) { sbList[0].Begin(); } } /// /// 魔方面板旋转后,修正各小方块在数组中的索引 /// /// 当前被旋转面板的各小方块的索引数组 /// 旋转方向 private void TransformPanelPostion(Position[] position, Direction direction) { if (position.Length != 9) { throw new Exception("数组包含元素个数必须为9"); } ModelUIElement3D model1 = GetCubeModel(position[0]); ModelUIElement3D model2 = GetCubeModel(position[1]); Vector3D axis1 = GetCubeAxis(position[0]); Vector3D axis2 = GetCubeAxis(position[1]); if (direction == Direction.Clockwise) // 顺时针旋转 { SetCubeModel(position[0], GetCubeModel(position[6])); SetCubeModel(position[6], GetCubeModel(position[8])); SetCubeModel(position[8], GetCubeModel(position[2])); SetCubeModel(position[2], model1); SetCubeModel(position[1], GetCubeModel(position[3])); SetCubeModel(position[3], GetCubeModel(position[7])); SetCubeModel(position[7], GetCubeModel(position[5])); SetCubeModel(position[5], model2); SetCubeAxis(position[0], GetCubeAxis(position[6])); SetCubeAxis(position[6], GetCubeAxis(position[8])); SetCubeAxis(position[8], GetCubeAxis(position[2])); SetCubeAxis(position[2], axis1); SetCubeAxis(position[1], GetCubeAxis(position[3])); SetCubeAxis(position[3], GetCubeAxis(position[7])); SetCubeAxis(position[7], GetCubeAxis(position[5])); SetCubeAxis(position[5], axis2); } else // 逆时针旋转 { SetCubeModel(position[0], GetCubeModel(position[2])); SetCubeModel(position[2], GetCubeModel(position[8])); SetCubeModel(position[8], GetCubeModel(position[6])); SetCubeModel(position[6], model1); SetCubeModel(position[1], GetCubeModel(position[5])); SetCubeModel(position[5], GetCubeModel(position[7])); SetCubeModel(position[7], GetCubeModel(position[3])); SetCubeModel(position[3], model2); SetCubeAxis(position[0], GetCubeAxis(position[2])); SetCubeAxis(position[2], GetCubeAxis(position[8])); SetCubeAxis(position[8], GetCubeAxis(position[6])); SetCubeAxis(position[6], axis1); SetCubeAxis(position[1], GetCubeAxis(position[5])); SetCubeAxis(position[5], GetCubeAxis(position[7])); SetCubeAxis(position[7], GetCubeAxis(position[3])); SetCubeAxis(position[3], axis2); } } private ModelUIElement3D GetCubeModel(Position position) { return cubeArray[position.X][position.Y][position.Z]; } private void SetCubeModel(Position position, ModelUIElement3D model) { cubeArray[position.X][position.Y][position.Z] = model; } private Vector3D GetCubeAxis(Position position) { return cubeAxis[position.X][position.Y][position.Z]; } private void SetCubeAxis(Position position, Vector3D axis) { cubeAxis[position.X][position.Y][position.Z] = axis; } private void FixCubeAxis(Position position, CenterAxis axis, Direction direction) { int direct = (int)direction; Vector3D vector = GetCubeAxis(position); switch (axis) { case CenterAxis.PositiveX: { int y = (int)vector.Y; vector.Y = vector.Z * direct; vector.Z = -y * direct; break; } case CenterAxis.PositiveY: { int x = (int)vector.X; vector.X = -vector.Z * direct; vector.Z = x * direct; break; } case CenterAxis.PositiveZ: { int x = (int)vector.X; vector.X = vector.Y * direct; vector.Y = -x * direct; break; } case CenterAxis.NegativeX: { int y = (int)vector.Y; vector.Y = -vector.Z * direct; vector.Z = y * direct; break; } case CenterAxis.NegativeY: { int x = (int)vector.X; vector.X = vector.Z * direct; vector.Z = -x * direct; break; } case CenterAxis.NegativeZ: { int x = (int)vector.X; vector.X = -vector.Y * direct; vector.Y = x * direct; break; } } SetCubeAxis(position, vector); } #endregion private void btnReset_Click(object sender, RoutedEventArgs e) { ResetCube(); } private void btnRotate_Click(object sender, RoutedEventArgs e) { for (int i = 0; i < 10; i++) { RandomRotate(); } } private void btnAbout_Click(object sender, RoutedEventArgs e) { FrmAbout about = new FrmAbout(); about.Top = this.Top + (this.ActualHeight - about.Height) / 2; about.Left = this.Left + (this.ActualWidth - about.Width) / 2; about.ShowDialog(); } } struct Position { public int X; public int Y; public int Z; public Position(int X, int Y, int Z) { this.X = X; this.Y = Y; this.Z = Z; } } struct Rotate { public CenterAxis CenterAxis; public Direction Direction; public Rotate(CenterAxis axis, Direction direct) { CenterAxis = axis; Direction = direct; } } enum CenterAxis { PositiveX = 1, PositiveY = 2, PositiveZ = 3, NegativeX = -1, NegativeY = -2, NegativeZ = -3 } enum Direction { Clockwise = 1, Counterclockwise = -1 } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.