//Vector2DをPointF[]に変換 public static PointF[] ToPointF(Vector2D[] A) { PointF[] P = new PointF[A.Length]; for (int i = 0; i < A.Length; i++) { P[i] = A[i].ToPointF(); } return P; }
//コンストラクタ(引数:3D座標) public Vertex(float x, float y, float z) { center2D = new Vector2D(); center3D = new Vector3D(x, y, z); brush = new SolidBrush(Color.Black); isSelected = false; willSelected = false; viewOK = false; diameter = 8; neighbor = new List<Vertex>(); nearlyNeighbor = new List<Vertex>(); }
//描画 public void Draw(Graphics g) { if (isVisible) { Vector2D[] temp = new Vector2D[vertex.Count]; for (int i = 0; i < temp.Length; i++) temp[i] = vertex[i].center2D; if (isFill) g.FillPolygon(nowbrush, Vector2D.ToPointF(temp.ToArray())); else g.DrawPolygon(Pens.Black, Vector2D.ToPointF(temp.ToArray())); foreach (Edge ed in edge) ed.Draw(g); foreach (Vertex vtx in vertex) vtx.Draw(g); } }
//2つのベクトルの距離を取得 public float Getdistance(Vector2D A) { return (A - this).Getnorm(); }
public static float Inner(Vector2D A, Vector2D B) { return A.X * B.X + A.Y * B.Y; }
//座標を設定 public Vector2D(Vector2D v) { this.Set(v.X, v.Y); }
//ビューポート変換 public Vector2D ViewPortTranslate(Vector3D A) { Vector2D P = new Vector2D(); P.X = (float)(A.X * (w / 2) + (w / 2)); P.Y = (float)(A.Y * -(h / 2) + (h / 2)); return P; }
//一括座標変換(関数オーバロード) public Vector2D Translate(Vector3D A) { Vector3D P = new Vector3D(); Vector2D R = new Vector2D(); P = ViewTranslate(A); P = ProjectionTranslate(P); R = ViewPortTranslate(P); return R; }
//一括座標変換(関数オーバロード) public Vertex[] Translate(Vertex[] A) { int len = A.Length; Vector3D[] P = new Vector3D[len]; Vector2D[] R = new Vector2D[len]; Vector3D[] Pt = new Vector3D[len]; Vertex[] Rt = new Vertex[len]; for (int i = 0; i < len; i++) { P[i] = new Vector3D(); R[i] = new Vector2D(); Pt[i] = new Vector3D(); Rt[i] = new Vertex(0, 0); } for (int i = 0; i < len; i++) { Pt[i] = A[i].center3D; } for (int i = 0; i < len; i++) { P[i] = ViewTranslate(Pt[i]); P[i] = ProjectionTranslate(P[i]); R[i] = ViewPortTranslate(P[i]); } for (int i = 0; i < len; i++) { Rt[i].center3D = Pt[i]; Rt[i].center2D = R[i]; } return Rt; }
//一括座標変換 public Vector2D[] Translate(Vector3D[] A) { int len = A.Length; Vector3D[] P = new Vector3D[len]; Vector2D[] R = new Vector2D[len]; for (int i = 0; i < len; i++) { P[i] = new Vector3D(); R[i] = new Vector2D(); } for (int i = 0; i < len; i++) { P[i] = ViewTranslate(A[i]); P[i] = ProjectionTranslate(P[i]); R[i] = ViewPortTranslate(P[i]); } return R; }
//カメラの回転 public void RotateCamera(double x1, double y1, double x2, double y2) { Vector2D A = new Vector2D(); Vector3D N1 = new Vector3D(); Vector3D N2 = new Vector3D(); Vector3D temp = new Vector3D(); double alpha, beta; float x, y, z; N1.X = Setposition.Y; N1.Y = -Setposition.X; N1.Z = LookatPoint.Z; N1.Normalization(); N2.X = LookatPoint.X; N2.Y = LookatPoint.Y; N2.Z = 1; N2.Normalization(); if (Upward.Z > 0) { A.X = (float)((x2 - x1) / w); A.Y = (float)((y2 - y1) / h); } if (Upward.Z < 0) { A.X = (float)((x1 - x2) / w); A.Y = (float)((y1 - y2) / h); } alpha = 2 * Math.PI * A.Y; beta = 2 * Math.PI * A.X; x = Setposition.X; y = Setposition.Y; z = Setposition.Z; rotatematrix.Set_Rotate_ArbAx(N1, alpha); temp = Setposition * rotatematrix; rotatematrix.Set_Rotate_ArbAx(N2, beta); temp = temp * rotatematrix; SetPosition(temp.X, temp.Y, temp.Z); if (Vector3D.Inner(temp2, Setposition - LookatPoint) > 0 && Upward.Z > 0) Upward.Z = -Upward.Z; if (Vector3D.Inner(temp2, Setposition - LookatPoint) < 0 && Upward.Z < 0) Upward.Z = -Upward.Z; if (Upward.Z > 0) temp2.Set(-Setposition.X, -Setposition.Y, LookatPoint.Z); if (Upward.Z < 0) temp2.Set(Setposition.X, Setposition.Y, LookatPoint.Z); }
//正二十面体を生成 private void SetIcosahedron(float length) { Face3D.Clear(); Dual.Clear(); Vector3D[] Vtemp = new Vector3D[12]; osPolygon Ptemp; Vector2D temp1 = new Vector2D(); double Ztemp1, Ztemp2; for (int i = 0; i < 12; i++) Vtemp[i] = new Vector3D(); Ztemp1 = (length / (2.0 * Math.Sin(Math.PI / 5.0))); Ztemp1 = Math.Sqrt(length * length - Ztemp1 * Ztemp1); Ztemp2 = Math.Sqrt((length * Math.Cos(Math.PI / 6.0)) * (length * Math.Cos(Math.PI / 6.0)) - (length / (2 * Math.Sin(Math.PI / 5.0)) - length / (2 * Math.Tan(Math.PI / 5.0))) * (length / (2 * Math.Sin(Math.PI / 5.0)) - length / (2 * Math.Tan(Math.PI / 5.0)))); Ztemp2 /= 2.0; Vtemp[0].Set((float)(length / (2 * Math.Sin(Math.PI / 5))), 0.0f, (float)Ztemp2); for (int i = 1; i < 5; i++) { temp1.X = (float)(Vtemp[0].X * Math.Cos(2 * i * Math.PI / 5.0) - Vtemp[0].Y * Math.Sin(2 * i * Math.PI / 5.0)); temp1.Y = (float)(Vtemp[0].X * Math.Sin(2 * i * Math.PI / 5.0) + Vtemp[0].Y * Math.Cos(2 * i * Math.PI / 5.0)); Vtemp[i].Set(temp1.X, temp1.Y, (float)Ztemp2); } Vtemp[5].Set(0.0f, 0.0f, (float)(Ztemp1 + Ztemp2)); Vtemp[6].Set((float)(length / (2 * Math.Sin(Math.PI / 5))), 0.0f, -(float)Ztemp2); temp1.X = (float)(Vtemp[6].X * Math.Cos(Math.PI / 5.0) - Vtemp[6].Y * Math.Sin(Math.PI / 5.0)); temp1.Y = (float)(Vtemp[6].X * Math.Sin(Math.PI / 5.0) + Vtemp[6].Y * Math.Cos(Math.PI / 5.0)); Vtemp[6].Set(temp1.X, temp1.Y, -(float)Ztemp2); for (int i = 7; i < 11; i++) { temp1.X = (float)(Vtemp[6].X * Math.Cos(2 * (i - 6) * Math.PI / 5) - Vtemp[6].Y * Math.Sin(2 * (i - 6) * Math.PI / 5)); temp1.Y = (float)(Vtemp[6].X * Math.Sin(2 * (i - 6) * Math.PI / 5) + Vtemp[6].Y * Math.Cos(2 * (i - 6) * Math.PI / 5)); Vtemp[i].Set(temp1.X, temp1.Y, -(float)Ztemp2); } Vtemp[11].Set(0.0f, 0.0f, -(float)(Ztemp1 + Ztemp2)); Ptemp = new osPolygon(0); Ptemp.AddVertex(Vtemp[0]); Ptemp.AddVertex(Vtemp[1]); Ptemp.AddVertex(Vtemp[5]); Face3D.Add(Ptemp); Ptemp = new osPolygon(1); Ptemp.AddVertex(Vtemp[1]); Ptemp.AddVertex(Vtemp[2]); Ptemp.AddVertex(Vtemp[5]); Face3D.Add(Ptemp); Ptemp = new osPolygon(2); Ptemp.AddVertex(Vtemp[2]); Ptemp.AddVertex(Vtemp[3]); Ptemp.AddVertex(Vtemp[5]); Face3D.Add(Ptemp); Ptemp = new osPolygon(3); Ptemp.AddVertex(Vtemp[3]); Ptemp.AddVertex(Vtemp[4]); Ptemp.AddVertex(Vtemp[5]); Face3D.Add(Ptemp); Ptemp = new osPolygon(4); Ptemp.AddVertex(Vtemp[4]); Ptemp.AddVertex(Vtemp[0]); Ptemp.AddVertex(Vtemp[5]); Face3D.Add(Ptemp); Ptemp = new osPolygon(5); Ptemp.AddVertex(Vtemp[7]); Ptemp.AddVertex(Vtemp[6]); Ptemp.AddVertex(Vtemp[11]); Face3D.Add(Ptemp); Ptemp = new osPolygon(6); Ptemp.AddVertex(Vtemp[8]); Ptemp.AddVertex(Vtemp[7]); Ptemp.AddVertex(Vtemp[11]); Face3D.Add(Ptemp); Ptemp = new osPolygon(7); Ptemp.AddVertex(Vtemp[9]); Ptemp.AddVertex(Vtemp[8]); Ptemp.AddVertex(Vtemp[11]); Face3D.Add(Ptemp); Ptemp = new osPolygon(8); Ptemp.AddVertex(Vtemp[10]); Ptemp.AddVertex(Vtemp[9]); Ptemp.AddVertex(Vtemp[11]); Face3D.Add(Ptemp); Ptemp = new osPolygon(9); Ptemp.AddVertex(Vtemp[6]); Ptemp.AddVertex(Vtemp[10]); Ptemp.AddVertex(Vtemp[11]); Face3D.Add(Ptemp); Ptemp = new osPolygon(10); Ptemp.AddVertex(Vtemp[1]); Ptemp.AddVertex(Vtemp[0]); Ptemp.AddVertex(Vtemp[6]); Face3D.Add(Ptemp); Ptemp = new osPolygon(11); Ptemp.AddVertex(Vtemp[1]); Ptemp.AddVertex(Vtemp[6]); Ptemp.AddVertex(Vtemp[7]); Face3D.Add(Ptemp); Ptemp = new osPolygon(12); Ptemp.AddVertex(Vtemp[2]); Ptemp.AddVertex(Vtemp[1]); Ptemp.AddVertex(Vtemp[7]); Face3D.Add(Ptemp); Ptemp = new osPolygon(13); Ptemp.AddVertex(Vtemp[2]); Ptemp.AddVertex(Vtemp[7]); Ptemp.AddVertex(Vtemp[8]); Face3D.Add(Ptemp); Ptemp = new osPolygon(14); Ptemp.AddVertex(Vtemp[3]); Ptemp.AddVertex(Vtemp[2]); Ptemp.AddVertex(Vtemp[8]); Face3D.Add(Ptemp); Ptemp = new osPolygon(15); Ptemp.AddVertex(Vtemp[3]); Ptemp.AddVertex(Vtemp[8]); Ptemp.AddVertex(Vtemp[9]); Face3D.Add(Ptemp); Ptemp = new osPolygon(16); Ptemp.AddVertex(Vtemp[4]); Ptemp.AddVertex(Vtemp[3]); Ptemp.AddVertex(Vtemp[9]); Face3D.Add(Ptemp); Ptemp = new osPolygon(17); Ptemp.AddVertex(Vtemp[4]); Ptemp.AddVertex(Vtemp[9]); Ptemp.AddVertex(Vtemp[10]); Face3D.Add(Ptemp); Ptemp = new osPolygon(18); Ptemp.AddVertex(Vtemp[0]); Ptemp.AddVertex(Vtemp[4]); Ptemp.AddVertex(Vtemp[10]); Face3D.Add(Ptemp); Ptemp = new osPolygon(19); Ptemp.AddVertex(Vtemp[0]); Ptemp.AddVertex(Vtemp[10]); Ptemp.AddVertex(Vtemp[6]); Face3D.Add(Ptemp); List<Vector3D> temp = new List<Vector3D>(); for (int i = 0; i < Face3D.Count; i++) { Face3D[i].SetCenter(); temp.Add(Face3D[i].center); } for (int i = 0; i < temp.Count; i++) Dual.Add(new Vertex(temp[i].X, temp[i].Y, temp[i].Z)); Dual[0].SetNeighbor(Dual[1]); Dual[0].SetNeighbor(Dual[4]); Dual[0].SetNeighbor(Dual[10]); Dual[1].SetNeighbor(Dual[0]); Dual[1].SetNeighbor(Dual[2]); Dual[1].SetNeighbor(Dual[12]); Dual[2].SetNeighbor(Dual[1]); Dual[2].SetNeighbor(Dual[3]); Dual[2].SetNeighbor(Dual[14]); Dual[3].SetNeighbor(Dual[2]); Dual[3].SetNeighbor(Dual[4]); Dual[3].SetNeighbor(Dual[16]); Dual[4].SetNeighbor(Dual[3]); Dual[4].SetNeighbor(Dual[0]); Dual[4].SetNeighbor(Dual[18]); Dual[5].SetNeighbor(Dual[9]); Dual[5].SetNeighbor(Dual[6]); Dual[5].SetNeighbor(Dual[11]); Dual[6].SetNeighbor(Dual[5]); Dual[6].SetNeighbor(Dual[7]); Dual[6].SetNeighbor(Dual[13]); Dual[7].SetNeighbor(Dual[6]); Dual[7].SetNeighbor(Dual[8]); Dual[7].SetNeighbor(Dual[15]); Dual[8].SetNeighbor(Dual[7]); Dual[8].SetNeighbor(Dual[9]); Dual[8].SetNeighbor(Dual[17]); Dual[9].SetNeighbor(Dual[8]); Dual[9].SetNeighbor(Dual[5]); Dual[9].SetNeighbor(Dual[19]); Dual[10].SetNeighbor(Dual[0]); Dual[10].SetNeighbor(Dual[11]); Dual[10].SetNeighbor(Dual[19]); Dual[11].SetNeighbor(Dual[5]); Dual[11].SetNeighbor(Dual[10]); Dual[11].SetNeighbor(Dual[12]); Dual[12].SetNeighbor(Dual[1]); Dual[12].SetNeighbor(Dual[11]); Dual[12].SetNeighbor(Dual[13]); Dual[13].SetNeighbor(Dual[6]); Dual[13].SetNeighbor(Dual[12]); Dual[13].SetNeighbor(Dual[14]); Dual[14].SetNeighbor(Dual[2]); Dual[14].SetNeighbor(Dual[13]); Dual[14].SetNeighbor(Dual[15]); Dual[15].SetNeighbor(Dual[7]); Dual[15].SetNeighbor(Dual[14]); Dual[15].SetNeighbor(Dual[16]); Dual[16].SetNeighbor(Dual[3]); Dual[16].SetNeighbor(Dual[15]); Dual[16].SetNeighbor(Dual[17]); Dual[17].SetNeighbor(Dual[8]); Dual[17].SetNeighbor(Dual[16]); Dual[17].SetNeighbor(Dual[18]); Dual[18].SetNeighbor(Dual[4]); Dual[18].SetNeighbor(Dual[17]); Dual[18].SetNeighbor(Dual[19]); Dual[19].SetNeighbor(Dual[9]); Dual[19].SetNeighbor(Dual[18]); Dual[19].SetNeighbor(Dual[10]); }
//頂点2D座標を設定(vector2D) public void Set2D(Vector2D vtr2D) { center2D = vtr2D; }