//誤差の範囲内かを判定 public static bool NearlyEcoal(Vector3D A, Vector3D B, float errorrange) { if (Math.Abs(A.X - B.X) < errorrange && Math.Abs(A.Y - B.Y) < errorrange && Math.Abs(A.Z - B.Z) < errorrange) return true; else return false; }
Vector3D[] vertex3D; //3次元上の頂点集合 #endregion Fields #region Constructors //コンストラクタ(引数:1辺の長さ) public Square(float length) { center = new Vector3D(0.0f, 0.0f, 0.0f); vertex3D = new Vector3D[VERTICES_NUM]; vertex2D = new Vector2D[VERTICES_NUM]; this.length = length; for (int i = 0; i < VERTICES_NUM; i++) { vertex2D[i] = new Vector2D(); vertex3D[i] = new Vector3D(); } brush = new SolidBrush(Color.FromArgb(255, 211, 225)); vertex3D[0].X = length / 2 + center.X; vertex3D[0].Y = length / 2 + center.Y; vertex3D[0].Z = center.Z; vertex3D[1].X = -length / 2 + center.X; vertex3D[1].Y = length / 2 + center.Y; vertex3D[1].Z = center.Z; vertex3D[2].X = -length / 2 + center.X; vertex3D[2].Y = -length / 2 + center.Y; vertex3D[2].Z = center.Z; vertex3D[3].X = length / 2 + center.X; vertex3D[3].Y = -length / 2 + center.Y; vertex3D[3].Z = center.Z; }
float w, h; //w:幅, h:高さ #endregion Fields #region Constructors //コンストラクタ public Camera() { Upward = new Vector3D(); Setposition = new Vector3D(); firstposition = new Vector3D(); LookatPoint = new Vector3D(); CameraX = new Vector3D(); CameraY = new Vector3D(); CameraZ = new Vector3D(); Light = new Vector3D(); rotatematrix = new Matrix(3); projectionmatrix = new Matrix(3); Upward.X = 0.0f; Upward.Y = 0.0f; Upward.Z = 1.0f; Setposition.X = firstposition.X = 20.0f; Setposition.Y = firstposition.Y = 30.0f; Setposition.Z = firstposition.Z = 50.0f; LookatPoint.X = 0.0f; LookatPoint.Y = 0.0f; LookatPoint.Z = 0.0f; temp2 = new Vector3D(-Setposition.X, -Setposition.Y, LookatPoint.Z); r = Setposition.Getnorm(); alpha = Math.Atan(Setposition.Y / Setposition.X); beta = Math.Asin((double)Setposition.Z / r); Light.X = 50.0f; Light.Y = 60.0f; Light.Z = 100.0f; angle = Math.PI / 5.0; aspect = 1.0f; nearZ = 10.0f; farZ = 20.0f; }
//等しいか判定 public static bool Ecoal(Vector3D A, Vector3D B) { if (A.X == B.X && A.Y == B.Y && A.Z == B.Z) return true; else return false; }
//ベクトル*行列 public static Vector3D operator *(Vector3D A, Matrix B) { Vector3D temp = new Vector3D(); temp.X = A.X * B.matrix[0, 0] + A.Y * B.matrix[1, 0] + A.Z * B.matrix[2, 0]; temp.Y = A.X * B.matrix[0, 1] + A.Y * B.matrix[1, 1] + A.Z * B.matrix[2, 1]; temp.Z = A.X * B.matrix[0, 2] + A.Y * B.matrix[1, 2] + A.Z * B.matrix[2, 2]; return temp; }
float MinY; //Y座標の最小値 #endregion Fields #region Constructors //コンストラクタ public Grid3D() { grid3D = new List<Vector3D>(); grid2D = new List<Vector2D>(); Axis3D = new Vector3D[4]; Axis2D = new Vector2D[4]; for (int i = 0; i < 4; i++) { Axis3D[i] = new Vector3D(); Axis2D[i] = new Vector2D(); } MaxX = 40.0f; MinX = -40.0f; MaxY = 40.0f; MinY = -40.0f; Xunit = 2.0f; Yunit = 2.0f; GridPen = new Pen(Color.Blue); GridPen.DashStyle = DashStyle.Dot; AxisPen = new Pen(Color.Blue); }
//3次元上の座標を作成 public void CreateGrid3D(Camera c) { Vector3D tempX = new Vector3D(); Vector3D tempY = new Vector3D(); Vector3D Grid3D1 = new Vector3D(); Vector3D Grid3D2 = new Vector3D(); Vector2D Grid2D1 = new Vector2D(); Vector2D Grid2D2 = new Vector2D(); grid3D.Clear(); grid2D.Clear(); for (float i = -Xunit; i > MinY; i -= Xunit) { Grid3D1.Set(i, MaxY, 0.0f); grid3D.Add(new Vector3D(Grid3D1)); Grid3D2.Set(i, MinY, 0.0f); grid3D.Add(new Vector3D(Grid3D2)); tempX = c.ViewTranslate(Grid3D1); tempX = c.ProjectionTranslate(tempX); Grid2D1 = c.ViewPortTranslate(tempX); grid2D.Add(new Vector2D(Grid2D1)); tempY = c.ViewTranslate(Grid3D2); tempY = c.ProjectionTranslate(tempY); Grid2D2 = c.ViewPortTranslate(tempY); grid2D.Add(new Vector2D(Grid2D2)); } for (float i = Xunit; i < MaxX; i += Xunit) { Grid3D1.Set(i, MaxY, 0.0f); grid3D.Add(new Vector3D(Grid3D1)); Grid3D2.Set(i, MinY, 0.0f); grid3D.Add(new Vector3D(Grid3D2)); tempX = c.ViewTranslate(Grid3D1); tempX = c.ProjectionTranslate(tempX); Grid2D1 = c.ViewPortTranslate(tempX); grid2D.Add(new Vector2D(Grid2D1)); tempY = c.ViewTranslate(Grid3D2); tempY = c.ProjectionTranslate(tempY); Grid2D2 = c.ViewPortTranslate(tempY); grid2D.Add(new Vector2D(Grid2D2)); } for (float i = -Yunit; i > MinY; i -= Yunit) { Grid3D1.Set(MaxX, i, 0.0f); grid3D.Add(new Vector3D(Grid3D1)); Grid3D2.Set(MinX, i, 0.0f); grid3D.Add(new Vector3D(Grid3D2)); tempX = c.ViewTranslate(Grid3D1); tempX = c.ProjectionTranslate(tempX); Grid2D1 = c.ViewPortTranslate(tempX); grid2D.Add(new Vector2D(Grid2D1)); tempY = c.ViewTranslate(Grid3D2); tempY = c.ProjectionTranslate(tempY); Grid2D2 = c.ViewPortTranslate(tempY); grid2D.Add(new Vector2D(Grid2D2)); } for (float i = Yunit; i < MaxY; i += Yunit) { Grid3D1.Set(MaxX, i, 0.0f); grid3D.Add(new Vector3D(Grid3D1)); Grid3D2.Set(MinX, i, 0.0f); grid3D.Add(new Vector3D(Grid3D2)); tempX = c.ViewTranslate(Grid3D1); tempX = c.ProjectionTranslate(tempX); Grid2D1 = c.ViewPortTranslate(tempX); grid2D.Add(new Vector2D(Grid2D1)); tempY = c.ViewTranslate(Grid3D2); tempY = c.ProjectionTranslate(tempY); Grid2D2 = c.ViewPortTranslate(tempY); grid2D.Add(new Vector2D(Grid2D2)); } Grid3D1.Set(MaxX, 0.0f, 0.0f); Axis3D[0] = new Vector3D(Grid3D1); Grid3D2.Set(MinX, 0.0f, 0.0f); Axis3D[1] = new Vector3D(Grid3D2); tempX = c.ViewTranslate(Grid3D1); tempX = c.ProjectionTranslate(tempX); Grid2D1 = c.ViewPortTranslate(tempX); Axis2D[0] = new Vector2D(Grid2D1); tempY = c.ViewTranslate(Grid3D2); tempY = c.ProjectionTranslate(tempY); Grid2D2 = c.ViewPortTranslate(tempY); Axis2D[1] = new Vector2D(Grid2D2); Grid3D1.Set(0.0f, MaxY, 0.0f); Axis3D[2] = new Vector3D(Grid3D1); Grid3D2.Set(0.0f, MinY, 0.0f); Axis3D[3] = new Vector3D(Grid3D2); tempX = c.ViewTranslate(Grid3D1); tempX = c.ProjectionTranslate(tempX); Grid2D1 = c.ViewPortTranslate(tempX); Axis2D[2] = new Vector2D(Grid2D1); tempY = c.ViewTranslate(Grid3D2); tempY = c.ProjectionTranslate(tempY); Grid2D2 = c.ViewPortTranslate(tempY); Axis2D[3] = new Vector2D(Grid2D2); }
//カメラの回転 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); }
//スクリーン座標変換 public Vector3D ViewTranslate(Vector3D A) { Vector3D P = new Vector3D(); P.X = Vector3D.Inner(A, CameraX) - Vector3D.Inner(Setposition, CameraX); P.Y = Vector3D.Inner(A, CameraY) - Vector3D.Inner(Setposition, CameraY); P.Z = Vector3D.Inner(A, CameraZ) - Vector3D.Inner(Setposition, CameraZ); return P; }
//ビューポート変換 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 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 SetCameraAxes() { CameraZ = LookatPoint - Setposition; CameraZ.Normalization(); CameraX = Vector3D.Outer(Upward, CameraZ); CameraX.Normalization(); CameraY = Vector3D.Outer(CameraZ, CameraX); }
//任意の点の回転 public void RotatePoint(Vector3D n, ref Vector3D v, float a) { rotatematrix.Set_Rotate_ArbAx(n, a); v = v * rotatematrix; }
//2つのベクトルの距離を取得 public float Getdistance(Vector3D A) { return (A - this).Getnorm(); }
//座標を設定 public Vector3D(Vector3D vtr3D) { this.Set(vtr3D.X, vtr3D.Y, vtr3D.Z); }
//座標軸より上か判定 public bool CheckIsUp() { Vector3D A = new Vector3D(0, 0, -1); return (Vector3D.Inner(A, -1 * Setposition) < 0); }
public static float Inner(Vector3D A, Vector3D B) { return A.X * B.X + A.Y * B.Y + A.Z * B.Z; }
//隠面処理 public void HiddenRemoval(Vector3D see) { Vector3D A = new Vector3D(); Vector3D B = new Vector3D(); Vector3D n = new Vector3D(); Vector3D S = new Vector3D(); double cos; for (int i = 0; i < FACES_NUM; i++) { A = face3D[i][0] - face3D[i][1]; B = face3D[i][2] - face3D[i][1]; n = Vector3D.Outer(A, B); S = face3D[i][1] - see; n.Normalization(); S.Normalization(); cos = Vector3D.Inner(S, n); if (cos > 0) { canSee[i] = true; brush[i] = new SolidBrush(Color.FromArgb((int)(basebrush[i].Color.R * cos), (int)(basebrush[i].Color.G * cos), (int)(basebrush[i].Color.B * cos))); } else canSee[i] = false; } }
//外積 public static Vector3D Outer(Vector3D A, Vector3D B) { Vector3D P = new Vector3D(); P.X = A.Y * B.Z - A.Z * B.Y; P.Y = A.Z * B.X - A.X * B.Z; P.Z = A.X * B.Y - A.Y * B.X; return P; }
//コンストラクタ(引数:1辺の長さ, brush) public Cube(float len, SolidBrush c) { center = new Vector3D(0.0f, 0.0f, 0.0f); vertex3D = new Vector3D[VERTICES_NUM]; vertex2D = new Vector2D[VERTICES_NUM]; face3D = new Vector3D[FACES_NUM][]; face2D = new Vector2D[FACES_NUM][]; canSee = new bool[FACES_NUM]; length = len; for (int i = 0; i < VERTICES_NUM; i++) { vertex2D[i] = new Vector2D(); vertex3D[i] = new Vector3D(); } for (int i = 0; i < FACES_NUM; i++) { face2D[i] = new Vector2D[4]; face3D[i] = new Vector3D[4]; } for (int i = 0; i < FACES_NUM; i++) { for (int j = 0; j < 4; j++) { face2D[i][j] = new Vector2D(); face3D[i][j] = new Vector3D(); } } brush = new SolidBrush[FACES_NUM]; basebrush = new SolidBrush[FACES_NUM]; for (int i = 0; i < basebrush.Length; i++) basebrush[i] = c; vertex3D[0].X = length / 2 + center.X; vertex3D[0].Y = length / 2 + center.Y; vertex3D[0].Z = length / 2 + center.Z; vertex3D[1].X = -length / 2 + center.X; vertex3D[1].Y = length / 2 + center.Y; vertex3D[1].Z = length / 2 + center.Z; vertex3D[2].X = -length / 2 + center.X; vertex3D[2].Y = -length / 2 + center.Y; vertex3D[2].Z = length / 2 + center.Z; vertex3D[3].X = length / 2 + center.X; vertex3D[3].Y = -length / 2 + center.Y; vertex3D[3].Z = length / 2 + center.Z; vertex3D[4].X = length / 2 + center.X; vertex3D[4].Y = length / 2 + center.Y; vertex3D[4].Z = -length / 2 + center.Z; vertex3D[5].X = -length / 2 + center.X; vertex3D[5].Y = length / 2 + center.Y; vertex3D[5].Z = -length / 2 + center.Z; vertex3D[6].X = -length / 2 + center.X; vertex3D[6].Y = -length / 2 + center.Y; vertex3D[6].Z = -length / 2 + center.Z; vertex3D[7].X = length / 2 + center.X; vertex3D[7].Y = -length / 2 + center.Y; vertex3D[7].Z = -length / 2 + center.Z; SetFace3D(); }
//軸回転行列の生成 public void Set_Rotate_ArbAx(Vector3D n, double angle) { this.matrix[0, 0] = (float)(n.X * n.X + (1 - n.X * n.X) * Math.Cos(angle)); this.matrix[1, 0] = (float)(n.X * n.Y * (1 - Math.Cos(angle)) - n.Z * Math.Sin(angle)); this.matrix[2, 0] = (float)(n.Z * n.X * (1 - Math.Cos(angle)) + n.Y * Math.Sin(angle)); this.matrix[0, 1] = (float)(n.X * n.Y * (1 - Math.Cos(angle)) + n.Z * Math.Sin(angle)); this.matrix[1, 1] = (float)(n.Y * n.Y + (1 - n.Y * n.Y) * Math.Cos(angle)); this.matrix[2, 1] = (float)(n.Y * n.Z * (1 - Math.Cos(angle)) - n.X * Math.Sin(angle)); this.matrix[0, 2] = (float)(n.Z * n.X * (1 - Math.Cos(angle)) - n.Y * Math.Sin(angle)); this.matrix[1, 2] = (float)(n.Y * n.Z * (1 - Math.Cos(angle)) + n.X * Math.Sin(angle)); this.matrix[2, 2] = (float)(n.Z * n.Z + (1 - n.Z * n.Z) * Math.Cos(angle)); }
//射影変換 public Vector3D ProjectionTranslate(Vector3D A) { Vector3D P = new Vector3D(); SetProjectionMatrix(); P = A * projectionmatrix; P.Z += (farZ * nearZ) / (farZ - nearZ); P.X = P.X / A.Z; P.Y = P.Y / A.Z; P.Z = P.Z / A.Z; return P; }