C#3D立方体ワイヤーフレーム (第5回) 座標を表示 for VS2013/2015 Express
立方体をマウスの左クリック移動で回転、右クリックで平行移動している様子。
5回目
絶対座標と、オブジェクト(と言うことにする)の相対座標を表示する
クラスを追加。
CThreeD.cs C#3D立方体ワイヤーフレーム (第1回) for VS2013 Express
CBond.cs C#3D立方体ワイヤーフレーム (第2回) for VS2013 Express
CRubic.cs C#3D立方体ワイヤーフレーム (第3回) for VS2013 Express
Form1.cs C#3D立方体ワイヤーフレーム (第4回) for VS2013 Express
Zahyou.cs C#3D立方体ワイヤーフレーム (第5回) 座標を表示 for VS2013/2015 Express
1回目~4回目で使用したものを利用する。
Form1.csと、CThreeD.csはそのまま利用。
CBond.cs と CRubic.csをちょっと変更。
Zahyouクラスを作成した。
図解入門 最新人工知能がよ~くわかる本 (How-nual図解入門Visual Guide Book)
- 作者: 神崎洋治
- 出版社/メーカー: 秀和システム
- 発売日: 2016/07/05
- メディア: 単行本
- この商品を含むブログを見る
Zahyouクラス
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Drawing; namespace WindowsFormsApplication1 { public partial class Form1 : Form { class Zahyou { //絶対座標の表示 public CThreeD m_pThreeD; public Vertex m_Center = new Vertex(); //相対座標の中心 Listm_Vertex = new List (); public Zahyou(int len, Vertex pCenter, ref CThreeD pThreeD) { InitBond(len); m_pThreeD = pThreeD; m_Center = pCenter; } /*============================================================================= 機能 初期化する 引数 x, y, z : =============================================================================*/ public void InitBond(int len) { InitVertex(len, len, len); } /*============================================================================= 機能 頂点を初期化する 引数 x, y, z : 立方体の番号( -1 ~ 1 ) =============================================================================*/ public void InitVertex(int x, int y, int z) { MyPoint3D[] A = new MyPoint3D[6]; //X軸 A[0] = new MyPoint3D(x, 0, 0); A[1] = new MyPoint3D(-x, 0, 0); //Y軸 A[2] = new MyPoint3D(0, y, 0); A[3] = new MyPoint3D(0, -y, 0); //Z軸 A[4] = new MyPoint3D(0, 0, z); A[5] = new MyPoint3D(0, 0, -z); //コネクト情報 A[0].bond.Add(1); A[2].bond.Add(3); A[4].bond.Add(5); List result = new List (); //格納 for (int i = 0; 6 > i; i++) { result.Add(A[i]); } for (int i = 0; result.Count > i; i++) { SetVertex(0, i, result[i].bond, result[i].x, result[i].y, result[i].z); } } /*============================================================================= 機能 頂点を決める 引数 group: グループ番号 n : 線の番号 m : どこと繋がるか x, y, z : 端の座標 =============================================================================*/ public void SetVertex(int group, int n, List bond, int x, int y, int z) { Vertex ver = new Vertex(); ver.group = group; for (int i = 0; bond.Count > i; i++) ver.bond.Add(bond[i]); ver.x = x; ver.y = y; ver.z = z; m_Vertex.Add(ver); } /*============================================================================= 機能 2次元座標系に変換する(回転行列をつくる) 引数 ViewPolar : 視点の極座標 =============================================================================*/ public void TransferScreen(Polar ViewPolar) { int i; Vertex ver = new Vertex(); for (i = 0; i < m_Vertex.Count; i++) { if (m_Vertex[i] != null) { ver = m_Vertex[i]; m_pThreeD.TransferScreen(ViewPolar, ref ver); m_Vertex[i] = ver; } } } /*============================================================================= 機能 立方体を描画する 引数 pDC : メモリデバイスコンテキストへのポインタ ViewVertex : 視点の座標 =============================================================================*/ public void DrawBond(Graphics gra, Pen pen, Vertex ViewVertex) { int i, j; Point[] point = new Point[2]; i = 0; for (i = 0; i < m_Vertex.Count; i++) { if (m_Vertex[i] != null) { for (j = 0; j < m_Vertex[i].bond.Count; j++) { int c = m_Vertex[i].bond[j]; //接続先 point[0].X = m_Center.x + m_Vertex[i].point.X; point[0].Y = m_Center.y + m_Vertex[i].point.Y; point[1].X = m_Center.x + m_Vertex[c].point.X; point[1].Y = m_Center.y + m_Vertex[c].point.Y; gra.DrawLine(pen, point[0], point[1]); } } } } } } }
CBondクラスの変更
CBond.cs C#3D立方体ワイヤーフレーム (第2回) for VS2013 Express
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Drawing; namespace WindowsFormsApplication1 { public partial class Form1 : Form { class CBond { public CThreeD m_pThreeD; public Vertex m_Center = new Vertex(); //相対座標の中心 Listm_Vertex = new List (); public int mGourp_num; //グループ名の個数 private Zahyou m_pZahyou; //追加 public CBond(int x, int y, int z, ref CThreeD pThreeD) { InitBond(x, y, z); m_pThreeD = pThreeD; m_pZahyou = new Zahyou(100, m_Center, ref m_pThreeD); } /*============================================================================= 機能 初期化する 引数 x, y, z : =============================================================================*/ public void InitBond(int x, int y, int z) { InitVertex(x - 1, y - 1, z - 1); } /*============================================================================= 機能 頂点を初期化する 引数 x, y, z : 立方体の番号( -1 ~ 1 ) =============================================================================*/ public void InitVertex(int x, int y, int z) { int cx, cy, cz; m_Center.x = 0; m_Center.y = 0; m_Center.z = 0; cx = 0; cy = 0; cz = 0; mGourp_num = 0; MyPoint3D[] A = new MyPoint3D[8]; A[0] = new MyPoint3D(cx + 100, cy + 100, cz + 100); A[1] = new MyPoint3D(cx - 100, cy + 100, cz + 100); A[2] = new MyPoint3D(cx - 100, cy - 100, cz + 100); A[3] = new MyPoint3D(cx + 100, cy - 100, cz + 100); A[4] = new MyPoint3D(cx + 100, cy + 100, cz - 100); A[5] = new MyPoint3D(cx - 100, cy + 100, cz - 100); A[6] = new MyPoint3D(cx - 100, cy - 100, cz - 100); A[7] = new MyPoint3D(cx + 100, cy - 100, cz - 100); //コネクト情報 A[0].bond.Add(1); A[1].bond.Add(2); A[2].bond.Add(3); A[3].bond.Add(0); A[4].bond.Add(5); A[5].bond.Add(6); A[6].bond.Add(7); A[7].bond.Add(4); A[0].bond.Add(4); A[1].bond.Add(5); A[2].bond.Add(6); A[3].bond.Add(7); List result = new List (); //格納 for (int i = 0; 8 > i; i++) { result.Add(A[i]); } for (int i = 0; result.Count > i; i++) { SetVertex(0, i, result[i].bond, result[i].x, result[i].y, result[i].z); } } /*============================================================================= 機能 頂点を決める 引数 group: グループ番号 n : 線の番号 m : どこと繋がるか x, y, z : 端の座標 =============================================================================*/ public void SetVertex(int group, int n, List bond, int x, int y, int z) { Vertex ver = new Vertex(); ver.group = group; for (int i = 0; bond.Count > i; i++) ver.bond.Add(bond[i]); ver.x = x; ver.y = y; ver.z = z; m_Vertex.Add(ver); } /*============================================================================= 機能 2次元座標系に変換する(回転行列をつくる) 引数 ViewPolar : 視点の極座標 =============================================================================*/ public void TransferScreen(Polar ViewPolar) { int i; Vertex ver = new Vertex(); for (i = 0; i < m_Vertex.Count; i++) { if (m_Vertex[i] != null) { ver = m_Vertex[i]; m_pThreeD.TransferScreen(ViewPolar, ref ver); m_Vertex[i] = ver; } } m_pZahyou.TransferScreen(ViewPolar); } /*============================================================================= 機能 立方体を描画する 引数 pDC : メモリデバイスコンテキストへのポインタ ViewVertex : 視点の座標 =============================================================================*/ public void DrawBond(Graphics gra, Pen pen, Vertex ViewVertex) { int i, j; Point[] point = new Point[2]; i = 0; for (i = 0; i < m_Vertex.Count; i++) { if (m_Vertex[i] != null) { for (j = 0; j < m_Vertex[i].bond.Count; j++) { int c = m_Vertex[i].bond[j]; //接続先 point[0].X = m_Center.x + m_Vertex[i].point.X; point[0].Y = m_Center.y + m_Vertex[i].point.Y; point[1].X = m_Center.x + m_Vertex[c].point.X; point[1].Y = m_Center.y + m_Vertex[c].point.Y; gra.DrawLine(pen, point[0], point[1]); } } } m_pZahyou.DrawBond(gra, pen, ViewVertex); } } } }
CRubicクラスの変更
CRubic.cs C#3D立方体ワイヤーフレーム (第3回) for VS2013 Express
CRubic.csをちょっと変更。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Drawing; namespace WindowsFormsApplication1 { public partial class Form1 : Form { class CRubic { private Vertex m_ViewVertex; private Polar m_ViewPolar = new Polar(); private CThreeD m_pThreeD; private int m_scale; Polar m_OldViewPolar = new Polar(); Point m_ClickPoint = new Point(); private CBond[] m_pBond = new CBond[3]; private Zahyou m_pZahyou; //追加 /*============================================================================= 機能 メモリDCに描画する 引数 pDC : メモリデバイスコンテキストへのポインタ =============================================================================*/ public void DrawRubic(Graphics gra, Pen pen, int scale) { int i, j, k; i = 0; j = 0; k = 0; m_scale = scale; m_ViewPolar.r = m_scale; m_pBond[0].DrawBond(gra, pen, m_ViewVertex); m_pZahyou.DrawBond(gra, pen, m_ViewVertex); } /*============================================================================= 機能 コンストラクタ =============================================================================*/ public CRubic(int scale) { int i, j, k; i = 0; j = 0; k = 0; m_pThreeD = new CThreeD(); m_scale = scale; m_pBond[0] = new CBond(i, j, k, ref m_pThreeD); Vertex center = new Vertex(); //絶対座標 center.x = 0; center.y = 0; center.z = 0; m_pZahyou = new Zahyou(400, center, ref m_pThreeD); InitWorld(); TransferScreen(); } /*============================================================================= 機能 視点を初期化する =============================================================================*/ public void InitWorld() { m_ViewPolar.r = m_scale; m_ViewPolar.p = 0; m_ViewPolar.q = 0; m_pThreeD.PolarToVertex(m_ViewPolar, ref m_ViewVertex); } /*======================================================================== 機能 ルービックキューブを投影変換をする ========================================================================*/ public void TransferScreen() { int i, j, k; i = 0; j = 0; k = 0; m_pBond[0].TransferScreen(m_ViewPolar); m_pZahyou.TransferScreen(m_ViewPolar); } /*======================================================================== 機能 マウスがクリックされた位置を覚えておく 引数 point : クリックされた位置 ========================================================================*/ public void SetClickPoint(Point point) { SetViewPolar(); m_ClickPoint.X = point.X; m_ClickPoint.Y = point.Y; } /*======================================================================== 機能 視点を回転させる 引数 point : 現在のマウスの位置 ========================================================================*/ public void TurnViewPoint(Point point) { double p, q; p = m_ClickPoint.X - point.X; q = m_ClickPoint.Y - point.Y; m_pThreeD.TurnPolar(ref m_ViewPolar, m_OldViewPolar, p / 200, q / 200); m_pThreeD.PolarToVertex(m_ViewPolar, ref m_ViewVertex); TransferScreen(); } public void MoveCubes(Point point) { double p, q; p = m_ClickPoint.X - point.X; q = m_ClickPoint.Y - point.Y; m_pBond[0].m_Center.x = -(int)p; m_pBond[0].m_Center.y = -(int)q; TransferScreen(); } /*======================================================================== 機能 現在の視点を記憶する ========================================================================*/ public void SetViewPolar() { m_OldViewPolar.r = m_ViewPolar.r; m_OldViewPolar.p = m_ViewPolar.p; m_OldViewPolar.q = m_ViewPolar.q; } } } }
座標が表示されると、いろいろ見えてなかったのが見えてくる感じがしますね。
第1回~4回で触れなかったのですが、
オブジェクトの平行移動は、マウスの右クリックで行っている。
マウスの縦横の変化量をX,Yに与えています。
しかし、前回までは座標が表示されていなかったため、
本当に移動しているのかどうかよくわからないので、触れませんでした。
今回、座標を追加したことで、確かにうまく移動しているんだな~というのが
視覚的にも、わかり大満足ですにゃ(*´ω`*)。
終わり。