DrawCircleClass.cs
上传用户:lslight
上传日期:2022-01-10
资源大小:14248k
文件大小:6k
源码类别:

DirextX编程

开发平台:

C#

  1. ///////////////////////////////////////////////////////////////////////
  2. //      ■■■■     ■■■■■       ■■■■       ■       ■      //
  3. //    ■                 ■         ■               ■       ■      //
  4. //    ■                 ■         ■    ■■■     ■       ■      //
  5. //    ■                 ■         ■       ■      ■       ■      //
  6. //      ■■■■         ■           ■■■■         ■■■■       //
  7. // Copyright (c) 三峡大学水利与环境学院 肖泽云. All rights reserved.  //
  8. ////////////////////////////////////////////////////////////////////////
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Text;
  12. using System.Drawing;
  13. using Microsoft.DirectX;
  14. using Microsoft.DirectX.Direct3D;
  15. namespace 绘制基本几何体
  16. {
  17.     class DrawCircleClass
  18.     {
  19.         /// <summary>
  20.         /// <param name="device">device:设备对象</param>
  21.         /// </summary>
  22.         public Device device;//定义设备
  23.         /// <summary>
  24.         /// <param name="circleCenterVectors">circleCenterVectors:圆心坐标</param>
  25.         /// </summary>
  26.         public Vector3 circleCenterVectors;//定义圆心坐标
  27.         ///<summary>
  28.         ///<param name="radius">radius:圆半径</param>
  29.         ///</summary>        
  30.         public float radius;//定义圆半径
  31.         /// <summary>
  32.         /// <param name="circleNormalVectors">circleNormalVectors:圆的法线向量</param>
  33.         /// </summary>
  34.         public Vector3 circleNormalVectors;//定义圆的法线向量
  35.         /// <summary>
  36.         /// <param name="mNumber">mNumber:沿切向的分块数目</param>
  37.         /// </summary>
  38.         public int mNumber;//定义沿切向的分块数目
  39.         /// <summary>
  40.         /// <param name="nNumber">nNumber:沿径向的分块数目</param>
  41.         /// </summary>
  42.         public int nNumber;//定义沿径向的分块数目
  43.         private CustomVertex.PositionColored[] vertices;//定义圆网格顶点
  44.         private int[] indexData;//定义圆网格中三角形索引
  45.         /// <summary>
  46.         /// 定义一个绘制圆对象
  47.         /// </summary>
  48.         /// <param name="_device">设备对象</param>
  49.         /// <param name="_circleCenterVectors">圆心坐标</param>
  50.         /// <param name="_radius">圆半径</param>
  51.         /// <param name="_circleNormalVectors">圆的法线向量</param>
  52.         /// <param name="_mNumber">沿切向的分块数目</param>
  53.         /// <param name="_nNumber">沿径向的分块数目</param>
  54.         public DrawCircleClass(Device _device, Vector3 _circleCenterVectors, float _radius, Vector3 _circleNormalVectors, int _mNumber, int _nNumber)
  55.         {
  56.             device = _device;
  57.             circleCenterVectors = _circleCenterVectors;
  58.             radius = _radius;
  59.             circleNormalVectors = _circleNormalVectors;
  60.             mNumber = _mNumber;
  61.             nNumber = _nNumber;
  62.             VertexDeclaration();//定义顶点
  63.             IndicesDeclaration();//定义索引
  64.         }
  65.         private void VertexDeclaration()//定义顶点
  66.         {
  67.             vertices = new CustomVertex.PositionColored[(mNumber + 1) * (nNumber + 1)];
  68.             float cosAlpha = circleNormalVectors.Y / circleNormalVectors.Length();
  69.             float sinAlpha = (float)Math.Sqrt(1 - cosAlpha * cosAlpha);
  70.             float sinBeta = circleNormalVectors.Z / (float)Math.Sqrt(circleNormalVectors.X * circleNormalVectors.X + circleNormalVectors.Z * circleNormalVectors.Z);
  71.             float cosBeta = (float)Math.Sqrt(1 - sinBeta * sinBeta);
  72.             Vector3 R0Vector = new Vector3();
  73.             R0Vector.X=radius*cosAlpha*cosBeta;
  74.             R0Vector.Y=-radius*sinAlpha;
  75.             R0Vector.Z=radius*cosAlpha*sinBeta;
  76.             float theta = (float)Math.PI*2 / mNumber;
  77.             for (int i = 0; i < mNumber + 1; i++)
  78.             {
  79.                 Vector4 tempRVector =Vector3.Transform(R0Vector,Matrix.RotationAxis(circleNormalVectors,theta*i));
  80.                 for (int j = 0; j < nNumber + 1; j++)
  81.                 {
  82.                     vertices[j + i * (nNumber + 1)].X = circleCenterVectors.X + tempRVector.X * j / nNumber;
  83.                     vertices[j + i * (nNumber + 1)].Y = circleCenterVectors.Y + tempRVector.Y * j / nNumber;
  84.                     vertices[j + i * (nNumber + 1)].Z = circleCenterVectors.Z + tempRVector.Z * j / nNumber;
  85.                     vertices[j + i * (nNumber + 1)].Color = Color.Yellow.ToArgb();
  86.                 }
  87.             }
  88.         }
  89.         private void IndicesDeclaration()//定义索引
  90.         {
  91.             indexData = new int[6 * mNumber * nNumber];
  92.             for (int i = 0; i < mNumber; i++)
  93.             {
  94.                 for (int j = 0; j < nNumber; j++)
  95.                 {
  96.                     indexData[6 * (j + i * nNumber)] = j + i * (nNumber + 1);
  97.                     indexData[6 * (j + i * nNumber) + 1] = j + i * (nNumber + 1) + 1;
  98.                     indexData[6 * (j + i * nNumber) + 2] = j + (i + 1) * (nNumber + 1);
  99.                     indexData[6 * (j + i * nNumber) + 3] = j + i * (nNumber + 1) + 1;
  100.                     indexData[6 * (j + i * nNumber) + 4] = j + (i + 1) * (nNumber + 1) + 1;
  101.                     indexData[6 * (j + i * nNumber) + 5] = j + (i + 1) * (nNumber + 1);
  102.                 }
  103.             }
  104.         }
  105.         /// <summary>
  106.         /// 绘制圆
  107.         /// </summary>
  108.         public void DrawCircle()
  109.         {
  110.             device.DrawIndexedUserPrimitives(PrimitiveType.TriangleList, 0, (mNumber + 1) * (nNumber + 1), 2 * mNumber * nNumber, indexData, false, vertices);
  111.         }
  112.     }
  113. }