DrawCircleClass.cs
上传用户:lslight
上传日期:2022-01-10
资源大小:14248k
文件大小:6k
- ///////////////////////////////////////////////////////////////////////
- // ■■■■ ■■■■■ ■■■■ ■ ■ //
- // ■ ■ ■ ■ ■ //
- // ■ ■ ■ ■■■ ■ ■ //
- // ■ ■ ■ ■ ■ ■ //
- // ■■■■ ■ ■■■■ ■■■■ //
- // Copyright (c) 三峡大学水利与环境学院 肖泽云. All rights reserved. //
- ////////////////////////////////////////////////////////////////////////
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Drawing;
- using Microsoft.DirectX;
- using Microsoft.DirectX.Direct3D;
- namespace 绘制基本几何体
- {
- class DrawCircleClass
- {
- /// <summary>
- /// <param name="device">device:设备对象</param>
- /// </summary>
- public Device device;//定义设备
- /// <summary>
- /// <param name="circleCenterVectors">circleCenterVectors:圆心坐标</param>
- /// </summary>
- public Vector3 circleCenterVectors;//定义圆心坐标
- ///<summary>
- ///<param name="radius">radius:圆半径</param>
- ///</summary>
- public float radius;//定义圆半径
- /// <summary>
- /// <param name="circleNormalVectors">circleNormalVectors:圆的法线向量</param>
- /// </summary>
- public Vector3 circleNormalVectors;//定义圆的法线向量
- /// <summary>
- /// <param name="mNumber">mNumber:沿切向的分块数目</param>
- /// </summary>
- public int mNumber;//定义沿切向的分块数目
- /// <summary>
- /// <param name="nNumber">nNumber:沿径向的分块数目</param>
- /// </summary>
- public int nNumber;//定义沿径向的分块数目
- private CustomVertex.PositionColored[] vertices;//定义圆网格顶点
- private int[] indexData;//定义圆网格中三角形索引
- /// <summary>
- /// 定义一个绘制圆对象
- /// </summary>
- /// <param name="_device">设备对象</param>
- /// <param name="_circleCenterVectors">圆心坐标</param>
- /// <param name="_radius">圆半径</param>
- /// <param name="_circleNormalVectors">圆的法线向量</param>
- /// <param name="_mNumber">沿切向的分块数目</param>
- /// <param name="_nNumber">沿径向的分块数目</param>
- public DrawCircleClass(Device _device, Vector3 _circleCenterVectors, float _radius, Vector3 _circleNormalVectors, int _mNumber, int _nNumber)
- {
- device = _device;
- circleCenterVectors = _circleCenterVectors;
- radius = _radius;
- circleNormalVectors = _circleNormalVectors;
- mNumber = _mNumber;
- nNumber = _nNumber;
- VertexDeclaration();//定义顶点
- IndicesDeclaration();//定义索引
- }
- private void VertexDeclaration()//定义顶点
- {
- vertices = new CustomVertex.PositionColored[(mNumber + 1) * (nNumber + 1)];
- float cosAlpha = circleNormalVectors.Y / circleNormalVectors.Length();
- float sinAlpha = (float)Math.Sqrt(1 - cosAlpha * cosAlpha);
- float sinBeta = circleNormalVectors.Z / (float)Math.Sqrt(circleNormalVectors.X * circleNormalVectors.X + circleNormalVectors.Z * circleNormalVectors.Z);
- float cosBeta = (float)Math.Sqrt(1 - sinBeta * sinBeta);
- Vector3 R0Vector = new Vector3();
- R0Vector.X=radius*cosAlpha*cosBeta;
- R0Vector.Y=-radius*sinAlpha;
- R0Vector.Z=radius*cosAlpha*sinBeta;
- float theta = (float)Math.PI*2 / mNumber;
- for (int i = 0; i < mNumber + 1; i++)
- {
- Vector4 tempRVector =Vector3.Transform(R0Vector,Matrix.RotationAxis(circleNormalVectors,theta*i));
- for (int j = 0; j < nNumber + 1; j++)
- {
- vertices[j + i * (nNumber + 1)].X = circleCenterVectors.X + tempRVector.X * j / nNumber;
- vertices[j + i * (nNumber + 1)].Y = circleCenterVectors.Y + tempRVector.Y * j / nNumber;
- vertices[j + i * (nNumber + 1)].Z = circleCenterVectors.Z + tempRVector.Z * j / nNumber;
- vertices[j + i * (nNumber + 1)].Color = Color.Yellow.ToArgb();
- }
- }
- }
- private void IndicesDeclaration()//定义索引
- {
- indexData = new int[6 * mNumber * nNumber];
- for (int i = 0; i < mNumber; i++)
- {
- for (int j = 0; j < nNumber; j++)
- {
- indexData[6 * (j + i * nNumber)] = j + i * (nNumber + 1);
- indexData[6 * (j + i * nNumber) + 1] = j + i * (nNumber + 1) + 1;
- indexData[6 * (j + i * nNumber) + 2] = j + (i + 1) * (nNumber + 1);
- indexData[6 * (j + i * nNumber) + 3] = j + i * (nNumber + 1) + 1;
- indexData[6 * (j + i * nNumber) + 4] = j + (i + 1) * (nNumber + 1) + 1;
- indexData[6 * (j + i * nNumber) + 5] = j + (i + 1) * (nNumber + 1);
- }
- }
- }
- /// <summary>
- /// 绘制圆
- /// </summary>
- public void DrawCircle()
- {
- device.DrawIndexedUserPrimitives(PrimitiveType.TriangleList, 0, (mNumber + 1) * (nNumber + 1), 2 * mNumber * nNumber, indexData, false, vertices);
- }
- }
- }