Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9364

WPFで滑らかマウス操作を実現

$
0
0

WPFとMainWindowで立方体を作成

以下のような立方体を作成し、マウス操作で空間回転できるようにしました。
キャプチャ.PNG

WPFのソースコード

・以下のようになりました。

MainWindow.xaml
<Windowx:Class="RotateTheCube.Rotate"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title=""Height="600"Width="900"Background="Black"MouseLeftButtonUp="MouseUp3D"><Viewport3D><Viewport3D.Camera><PerspectiveCameraLookDirection="0,0,-1"Position="0,0,5"/></Viewport3D.Camera><ModelVisual3D><ModelUIElement3D><DirectionalLightColor="Red"Direction="-0,-0.5,-0.6"/></ModelUIElement3D><ModelUIElement3D><DirectionalLightColor="White"Direction="0.612372,-0.5,-0.612372"/></ModelUIElement3D><ModelUIElement3DMouseMove="MouseMove3D"MouseLeftButtonDown="MouseDown3D"><GeometryModel3D><GeometryModel3D.Geometry><MeshGeometry3DPositions="-0.5  0.5  0.5,  0.5  0.5  0.5,
                                           -0.5 -0.5  0.5,  0.5 -0.5  0.5,
                                            0.5  0.5 -0.5, -0.5  0.5 -0.5,
                                            0.5 -0.5 -0.5, -0.5 -0.5 -0.5,
                                           -0.5  0.5 -0.5, -0.5  0.5  0.5,
                                           -0.5 -0.5 -0.5, -0.5 -0.5  0.5,
                                            0.5  0.5  0.5,  0.5  0.5 -0.5,
                                            0.5 -0.5  0.5,  0.5 -0.5 -0.5,
                                           -0.5  0.5 -0.5,  0.5  0.5 -0.5,
                                           -0.5  0.5  0.5,  0.5  0.5  0.5,
                                            0.5 -0.5 -0.5, -0.5 -0.5 -0.5,
                                            0.5 -0.5  0.5, -0.5 -0.5  0.5"TriangleIndices=" 0  2  1,  1  2  3
                                                  4  6  5,  5  6  7,
                                                  8 10  9,  9 10 11,
                                                 12 14 13, 13 14 15
                                                 16 18 17, 17 18 19
                                                 20 22 21, 21 22 23"/></GeometryModel3D.Geometry><GeometryModel3D.Material><DiffuseMaterialBrush="White"/></GeometryModel3D.Material><GeometryModel3D.Transform><MatrixTransform3Dx:Name="myTransform"/></GeometryModel3D.Transform></GeometryModel3D></ModelUIElement3D></ModelVisual3D></Viewport3D></Window>

C#のソースコード

・続いてC#のコードです。

MainWindow.xaml.cs
// ***************************// * 立方体を空間で回転させる// *   2020.07.05  ProOJI// ***************************usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Windows;usingSystem.Windows.Controls;usingSystem.Windows.Data;usingSystem.Windows.Documents;usingSystem.Windows.Input;usingSystem.Windows.Media;usingSystem.Windows.Media.Media3D;//3D行列使用usingSystem.Windows.Media.Imaging;usingSystem.Windows.Navigation;usingSystem.Windows.Shapes;namespaceRotateTheCube{publicpartialclassRotate:Window{Matrix3Dm=Matrix3D.Identity;privatebool_isDrag=false;privatePoint_Offset;privatevoidMouseDown3D(objectsender,MouseButtonEventArgse){_isDrag=true;_Offset=e.GetPosition(this);}privatevoidMouseUp3D(objectsender,MouseButtonEventArgse){_isDrag=false;}privatevoidMouseMove3D(objectsender,MouseEventArgse){if(_isDrag==true){Pointpt=e.GetPosition(this);m.Rotate(newQuaternion(newVector3D(0,1,0),(pt.X-_Offset.X)/50));m.Rotate(newQuaternion(newVector3D(1,0,0),(pt.Y-_Offset.Y)/50));myTransform.Matrix=m;}}}}

まとめ

線形代数がこんなところで役立つとは…
数学をもっと学ぼうと思いました。


Viewing all articles
Browse latest Browse all 9364

Latest Images

Trending Articles