一、绘图模块结构
渲染模块,也即是绘图模块,由以上相关类和结构组成。以下对每个组件进行描述分析:
■绘图类(CDRraw)
这个类负责渲染画面,提供了一系列的操作用于画面的绘制:
- 在指定位置绘制字符串
- 在指定区域绘制字符串
- 在指定位置绘制矩形
- 在指定位置填充矩形
- 指定绘制符号
- 指定擦除画面背景色
■符号枚举(CSymbol)
提供了绘制的基本符号。
■矩形结构(CRect)
这个结构由位置和大小决定,表示工作区平面内的一个区域,常用于设定字符串的绘制区域,此区域内的字符串被视为有效,超过此范围的字符串将不被绘制。
■位置结构(CPoint)
这个结构由控制台列和行的位置决定,表示工作区平面内的一个点,常用于设定绘制的位置。
■尺寸结构(CSize)
这个结构由宽高大小决定,表示工作区某个区域的大小,常用于设定矩形的大小等。
■字符串处理类(CText)
考虑到中英文构成的字符串的情况,在绘制字符串的时候要考虑字符串的字节长度问题,这个类提供了字符串的截断和换行等字符串处理功能。
■矩阵结构(CMatrix)
这个结构表示由一个二维数组构成的矩阵,提供了矩阵基本的运算(加,减,乘)和矩阵的旋转(顺时针90度旋转 、180度旋转,270度旋转和逆时针90度旋转、180度旋转、270度旋转)功能。
- using System;
- using System.Text;
- namespace CGraphics
- {
- /// <summary>
- /// 字符串处理类
- /// </summary>
- sealed internal class CText
- {
- /// <summary>
- /// 获取字符串字节长度
- /// </summary>
- /// <param name="text"></param>
- /// <returns></returns>
- public static Int32 getLength(String text)
- {
- Byte[] bytes;
- Int32 len = 0;
- for (Int32 i = 0; i < text.Length; i++)
- {
- bytes = Encoding.Default.GetBytes(text.Substring(i, 1));
- len += bytes.Length > 1 ? 2 : 1;
- }
- return len;
- }
- /// <summary>
- /// 按字节长度截字符串
- /// </summary>
- /// <param name="text">字符串</param>
- /// <param name="len">字节长度</param>
- /// <returns></returns>
- public static String cutText(String text, Int32 len)
- {
- if (len < 0 || len > getLength(text))
- {
- throw new ArgumentOutOfRangeException();
- }
- Int32 charLen = 0;
- StringBuilder strb = new StringBuilder();
- for (Int32 i = 0; i < text.Length && charLen < len; i++)
- {
- charLen += getLength(text.Substring(i, 1));
- strb.Append(text[i]);
- }
- return strb.ToString();
- }
- /// <summary>
- /// 按字符串索引并截取相应字节长度
- /// </summary>
- /// <param name="text">字符串</param>
- /// <param name="index">字符串索引</param>
- /// <param name="len">字节长度</param>
- /// <returns></returns>
- public static String indexOfText(String text, Int32 index, Int32 len)
- {
- if (index < 0 || index > text.Length - 1)
- {
- throw new ArgumentOutOfRangeException();
- }
- if (len < 0 || len > getLength(text))
- {
- throw new ArgumentOutOfRangeException();
- }
- text = text.Substring(index, text.Length - index);
- return cutText(text, len);
- }
- /// <summary>
- /// 字符串换行
- /// </summary>
- /// <param name="text">字符串</param>
- /// <param name="cols">一列为2字节</param>
- /// <returns></returns>
- public static String lineBreakText(String text, Int32 cols)
- {
- if (cols < 0)
- {
- throw new ArgumentOutOfRangeException();
- }
- Int32 len = 0;
- Int32 charLen = 0;
- StringBuilder strb = new StringBuilder();
- for (Int32 i = 0; i < text.Length; i++)
- {
- len = getLength(text.Substring(i, 1));
- charLen += len;
- if (charLen > (cols <<1))
- {
- strb.Append(Environment.NewLine);
- charLen = len;
- }
- strb.Append(text[i]);
- }
- return strb.ToString();
- }
- }
- }
- using System;
- using System.Text;
- namespace CGraphics
- {
- /// <summary>
- /// 符号表 每个符号2字节
- /// </summary>
- public enum CSymbol
- {
- /// <summary>
- /// 缺省符号
- /// </summary>
- DEFAULT = (UInt16)0xA1A1,
- /// <summary>
- /// @@
- /// </summary>
- AT = (UInt16)0x4040,
- /// <summary>
- /// ##
- /// </summary>
- WELL = (UInt16)0x2323,
- /// <summary>
- /// ※
- /// </summary>
- RICE = (UInt16)0xA1F9,
- /// <summary>
- /// ☆
- /// </summary>
- STAR_EMPTY = (UInt16)0xA1EE,
- /// <summary>
- /// ★
- /// </summary>
- STAR_SOLID = (UInt16)0xA1EF,
- /// <summary>
- /// ○
- /// </summary>
- RING_EMPTY = (UInt16)0xA1F0,
- /// <summary>
- /// ●
- /// </summary>
- RING_SOLID = (UInt16)0xA1F1,
- /// <summary>
- /// ◇
- /// </summary>
- RHOMB_EMPTY = (UInt16)0xA1F3,
- /// <summary>
- /// ◆
- /// </summary>
- RHOMB_SOLID = (UInt16)0xA1F4,
- /// <summary>
- /// □
- /// </summary>
- RECT_EMPTY = (UInt16)0xA1F5,
- /// <summary>
- /// ■
- /// </summary>
- RECT_SOLID = (UInt16)0xA1F6,
- /// <summary>
- /// △
- /// </summary>
- TRIANGLE_EMPTY = (UInt16)0xA1F7,
- /// <summary>
- /// ▲
- /// </summary>
- TRIANGLE_SOLID = (UInt16)0xA1F8
- }
- /// <summary>
- /// 符号辅助类
- /// </summary>
- internal sealed class CSymbolHelper
- {
- /// <summary>
- /// 根据符号值获取字符串表示
- /// </summary>
- /// <param name="symbol"></param>
- /// <returns></returns>
- public static String getStringFromSymbol(CSymbol symbol)
- {
- UInt16 symbolVal = (UInt16)symbol;
- Byte[] bytes = { (Byte)((symbolVal & 0xFF00) >> 8), (Byte)(symbolVal & 0x00FF) };
- return Encoding.Default.GetString(bytes);
- }
- /// <summary>
- /// 获取符号十六进制表示
- /// </summary>
- /// <param name="symbol"></param>
- /// <returns></returns>
- public static String getSymbolHex(String symbol)
- {
- if (CText.getLength(symbol) > 2 || symbol == "")
- {
- throw new ArgumentOutOfRangeException("符号不能为空且长度不能超过2字节!");
- }
- String hex = "0x";
- Byte[] bytes = Encoding.Default.GetBytes(symbol);
- foreach (Byte b in bytes)
- {
- hex += String.Format("{0:X}", b);
- }
- return hex;
- }
- }
- }
- using System;
- namespace CGraphics
- {
- /// <summary>
- /// 尺寸结构
- /// </summary>
- public struct CSize
- {
- private Int32 m_width;
- private Int32 m_height;
- public CSize(Int32 width, Int32 height)
- {
- if (width <= 0 || height <= 0)
- {
- throw new ArgumentOutOfRangeException("尺寸大小不合法!");
- }
- this.m_width = width;
- this.m_height = height;
- }
- public Int32 getWidth()
- {
- return this.m_width;
- }
- public void setWidth(Int32 width)
- {
- this.m_width = width;
- }
- public Int32 getHeight()
- {
- return this.m_height;
- }
- public void setHeight(Int32 height)
- {
- this.m_height = height;
- }
- }
- }
- using System;
- namespace CGraphics
- {
- /// <summary>
- /// 矩形结构
- /// </summary>
- public struct CRect
- {
- /// <summary>
- /// 左上角列坐标
- /// </summary>
- private Int32 m_x;
- /// <summary>
- /// 左上角行坐标
- /// </summary>
- private Int32 m_y;
- /// <summary>
- /// 矩形宽度
- /// </summary>
- private Int32 m_width;
- /// <summary>
- /// 矩形高度
- /// </summary>
- private Int32 m_height;
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="x"></param>
- /// <param name="y"></param>
- /// <param name="w"></param>
- /// <param name="h"></param>
- public CRect(Int32 x, Int32 y, Int32 w, Int32 h)
- {
- if (w <= 0 || h <= 0)
- {
- throw new ArgumentOutOfRangeException();
- }
- this.m_x = x;
- this.m_y = y;
- this.m_width = w;
- this.m_height = h;
- }
- public CRect(CPoint point, Int32 w, Int32 h)
- {
- this.m_x = point.getX();
- this.m_y = point.getY();
- this.m_width = w;
- this.m_height = h;
- }
- public CRect(Int32 x, Int32 y, CSize size)
- {
- this.m_x = x;
- this.m_y = y;
- this.m_width = size.getWidth();
- this.m_height = size.getHeight();
- }
- public CRect(CPoint point, CSize size)
- {
- this.m_x = point.getX();
- this.m_y = point.getY();
- this.m_width = size.getWidth();
- this.m_height = size.getHeight();
- }
- /// <summary>
- /// 获取左上角列坐标
- /// </summary>
- /// <returns></returns>
- public Int32 getX()
- {
- return this.m_x;
- }
- /// <summary>
- /// 获取左上角行坐标
- /// </summary>
- /// <returns></returns>
- public Int32 getY()
- {
- return this.m_y;
- }
- /// <summary>
- /// 获取矩形宽度
- /// </summary>
- /// <returns></returns>
- public Int32 getWidth()
- {
- return this.m_width;
- }
- /// <summary>
- /// 获取矩形高度
- /// </summary>
- /// <returns></returns>
- public Int32 getHeight()
- {
- return this.m_height;
- }
- /// <summary>
- /// 获取矩形
- /// </summary>
- /// <returns></returns>
- public CRect getCRect()
- {
- return new CRect(this.m_x, this.m_y, this.m_width, this.m_height);
- }
- /// <summary>
- /// 设置左上角列坐标
- /// </summary>
- /// <param name="x"></param>
- public void setX(Int32 x)
- {
- this.m_x = x;
- }
- /// <summary>
- /// 设置左上角行坐标
- /// </summary>
- /// <param name="y"></param>
- public void setY(Int32 y)
- {
- this.m_y = y;
- }
- /// <summary>
- /// 设置矩形宽度
- /// </summary>
- /// <param name="w"></param>
- public void setWidth(Int32 w)
- {
- if (w <= 0)
- {
- throw new ArgumentOutOfRangeException();
- }
- this.m_width = w;
- }
- /// <summary>
- /// 设置矩形高度
- /// </summary>
- /// <param name="h"></param>
- public void setHeight(Int32 h)
- {
- if (h <= 0)
- {
- throw new ArgumentOutOfRangeException();
- }
- this.m_height = h;
- }
- /// <summary>
- /// 设置矩形
- /// </summary>
- /// <param name="x"></param>
- /// <param name="y"></param>
- /// <param name="w"></param>
- /// <param name="h"></param>
- public void setCRect(Int32 x, Int32 y, Int32 w, Int32 h)
- {
- if (w <= 0 || h <= 0)
- {
- throw new ArgumentOutOfRangeException();
- }
- this.m_x = x;
- this.m_y = y;
- this.m_width = w;
- this.m_height = h;
- }
- /// <summary>
- /// 缩放
- /// </summary>
- /// <param name="flateX"></param>
- /// <param name="flateY"></param>
- public void inflate(Int32 flateX, Int32 flateY)
- {
- this.m_x -= flateX;
- this.m_y -= flateY;
- this.m_width +=flateX<<1;
- this.m_height +=flateY<<1;
- }
- /// <summary>
- /// 调整矩形位置与尺寸
- /// </summary>
- /// <param name="rect"></param>
- /// <returns></returns>
- public void adjustRect(ref CRect rect)
- {
- Int32 maxX = Console.WindowWidth>>1;
- Int32 maxY = Console.WindowHeight;
- if (rect.m_x < 0)
- {
- rect.m_x = 0;
- }
- else if (rect.m_x > maxX)
- {
- rect.m_x = maxX - 1;
- }
- if (rect.m_y < 0)
- {
- rect.m_y = 0;
- }
- else if (rect.m_y > maxY)
- {
- rect.m_y = maxY;
- }
- rect.m_x += rect.m_x;
- }
- public override string ToString()
- {
- return string.Format("[{0},{1},{2},{3}]", m_x, m_y, m_width, m_height);
- }
- }
- }
- using System;
- using System.Text;
- namespace CGraphics
- {
- /// <summary>
- /// 旋转方式
- /// </summary>
- public enum CRotateMode
- {
- /// <summary>
- /// 不旋转
- /// </summary>
- None,
- /// <summary>
- /// 顺时针旋转90度
- /// </summary>
- R90,
- /// <summary>
- /// 顺时针旋转180度
- /// </summary>
- R180,
- /// <summary>
- /// 顺时针旋转270度
- /// </summary>
- R270,
- /// <summary>
- /// 逆时针旋转90度
- /// </summary>
- L90,
- /// <summary>
- /// 逆时针旋转180度
- /// </summary>
- L180,
- /// <summary>
- /// 逆时针旋转270度
- /// </summary>
- L270
- }
- /// <summary>
- /// 矩阵结构
- /// </summary>
- public struct CMatrix
- {
- /// <summary>
- /// 矩阵元素
- /// </summary>
- private Int32[,] m_matrix;
- /// <summary>
- /// 矩阵行数
- /// </summary>
- private Int32 m_rows;
- /// <summary>
- /// 矩阵列数
- /// </summary>
- private Int32 m_cols;
- /// <summary>
- /// 矩阵总元素
- /// </summary>
- private Int32 m_totalCount;
- /// <summary>
- /// 旋转方式
- /// </summary>
- private CRotateMode m_rotate;
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="matrix"></param>
- public CMatrix(Int32[,] matrix)
- {
- this.m_matrix = matrix;
- this.m_rows = matrix.GetUpperBound(0) + 1;
- this.m_cols = matrix.GetUpperBound(1) + 1;
- this.m_totalCount = m_rows * m_cols;
- this.m_rotate = CRotateMode.None;
- }
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="row"></param>
- /// <param name="col"></param>
- public CMatrix(Int32 row, Int32 col)
- {
- this.m_matrix = new Int32[row, col];
- this.m_rows = m_matrix.GetUpperBound(0) + 1;
- this.m_cols = m_matrix.GetUpperBound(1) + 1;
- this.m_totalCount = m_rows * m_cols;
- this.m_rotate = CRotateMode.None;
- }
- /// <summary>
- /// 可以通过索引操作
- /// </summary>
- /// <param name="x"></param>
- /// <param name="y"></param>
- /// <returns></returns>
- public Int32 this[Int32 x, Int32 y]
- {
- get
- {
- if (x < 0 || y < 0 || x > m_rows - 1 || y > m_cols - 1)
- {
- throw new ArgumentOutOfRangeException();
- }
- return m_matrix[x, y];
- }
- set
- {
- if (x < 0 || y < 0 || x > m_rows - 1 || y > m_cols - 1)
- {
- throw new ArgumentOutOfRangeException();
- }
- m_matrix[x, y] = value;
- }
- }
- /// <summary>
- /// 获取矩阵数组
- /// </summary>
- /// <returns></returns>
- public Int32[,] getMatrixArray()
- {
- return this.m_matrix;
- }
- /// <summary>
- /// 设置矩阵
- /// </summary>
- /// <param name="matrix"></param>
- public void setMatrix(Int32[,] matrix)
- {
- this.m_matrix = matrix;
- this.m_rows = matrix.GetUpperBound(0) + 1;
- this.m_cols = matrix.GetUpperBound(1) + 1;
- this.m_totalCount = m_rows * m_cols;
- this.m_rotate = CRotateMode.None;
- }
- /// <summary>
- /// 获取矩阵行数
- /// </summary>
- /// <returns></returns>
- public Int32 getRows()
- {
- return this.m_rows;
- }
- /// <summary>
- /// 获取矩阵列数
- /// </summary>
- /// <returns></returns>
- public Int32 getCols()
- {
- return this.m_cols;
- }
- /// <summary>
- /// 获取矩阵元素总个数
- /// </summary>
- /// <returns></returns>
- public Int32 getTotalCount()
- {
- return m_totalCount;
- }
- /// <summary>
- /// 设置旋转方式
- /// </summary>
- /// <param name="mode"></param>
- /// <returns></returns>
- public void setRotateMode(CRotateMode mode)
- {
- this.m_rotate = mode;
- }
- /// <summary>
- /// 两矩阵相加
- /// </summary>
- /// <param name="left"></param>
- /// <param name="right"></param>
- /// <returns></returns>
- public static CMatrix operator +(CMatrix left, CMatrix right)
- {
- if ((left.getRows() != right.getRows()) ||
- (left.getCols() != right.getCols()))
- {
- throw new CMatrixException("error");
- }
- Int32 r = left.getRows();
- Int32 c = left.getCols();
- CMatrix newMatrix = new CMatrix(r, c);
- for (Int32 i = 0; i < r; i++)
- {
- for (Int32 j = 0; j < c; j++)
- {
- newMatrix[i, j] = left[i, j] + right[i, j];
- }
- }
- return newMatrix;
- }
- /// <summary>
- /// 矩阵相加
- /// </summary>
- /// <param name="right"></param>
- /// <returns></returns>
- public CMatrix add(CMatrix right)
- {
- return this + right;
- }
- /// <summary>
- /// 两矩阵相减
- /// </summary>
- /// <param name="left"></param>
- /// <param name="right"></param>
- /// <returns></returns>
- public static CMatrix operator -(CMatrix left, CMatrix right)
- {
- if ((left.getRows() != right.getRows()) ||
- (left.getCols() != right.getCols()))
- {
- throw new CMatrixException("error");
- }
- Int32 r = left.getRows();
- Int32 c = left.getCols();
- CMatrix newMatrix = new CMatrix(r, c);
- for (Int32 i = 0; i < r; i++)
- {
- for (Int32 j = 0; j < c; j++)
- {
- newMatrix[i, j] = left[i, j] - right[i, j];
- }
- }
- return newMatrix;
- }
- /// <summary>
- /// 矩阵相减
- /// </summary>
- /// <param name="right"></param>
- /// <returns></returns>
- public CMatrix sub(CMatrix right)
- {
- return this - right;
- }
- /// <summary>
- /// 两矩阵相乘
- /// </summary>
- /// <param name="left"></param>
- /// <param name="right"></param>
- /// <returns></returns>
- public static CMatrix operator *(CMatrix left, CMatrix right)
- {
- //左矩阵列数等于右矩阵的行数时才能执行相乘运算
- if (left.getCols() != right.getRows())
- {
- throw new CMatrixException("error");
- }
- Int32 lr = left.getRows();
- Int32 lc = left.getCols();
- Int32 rc = right.getCols();
- CMatrix newMatrix = new CMatrix(lr, rc);
- for (Int32 i = 0; i < lr; i++)
- {
- for (Int32 j = 0; j < rc; j++)
- {
- for (Int32 z = 0; z < lc; z++)
- {
- newMatrix[i, j] = newMatrix[i, j] + left[i, z] * right[z, j];
- }
- }
- }
- return newMatrix;
- }
- /// <summary>
- /// 矩阵相乘
- /// </summary>
- /// <param name="right"></param>
- /// <returns></returns>
- public CMatrix multiply(CMatrix right)
- {
- return this * right;
- }
- /// <summary>
- /// 矩阵旋转
- /// </summary>
- /// <returns></returns>
- public void rotate()
- {
- rotate(this.m_rotate);
- }
- /// <summary>
- /// 矩阵旋转
- /// </summary>
- /// <param name="mode">旋转模式</param>
- /// <returns></returns>
- public void rotate(CRotateMode mode)
- {
- switch (mode)
- {
- case CRotateMode.None:
- break;
- case CRotateMode.R90:
- rotateR90();
- break;
- case CRotateMode.R180:
- rotateR180();
- break;
- case CRotateMode.R270:
- rotateR270();
- break;
- case CRotateMode.L90:
- rotateL90();
- break;
- case CRotateMode.L180:
- rotateL180();
- break;
- case CRotateMode.L270:
- rotateL270();
- break;
- default:
- break;
- }
- }
- #region 矩阵旋转函数
- /// <summary>
- /// 顺时针旋转90
- /// </summary>
- /// <returns></returns>
- private void rotateR90()
- {
- Int32 r = this.getRows();
- Int32 c = this.getCols();
- CMatrix newMatrix = new CMatrix(c, r);
- for (Int32 i = 0; i < c; i++)
- {
- for (Int32 j = 0; j < r; j++)
- {
- newMatrix[i, j] = this[r - j - 1, i];
- }
- }
- this = newMatrix;
- }
- /// <summary>
- /// 顺时针旋转180
- /// </summary>
- /// <returns></returns>
- private void rotateR180()
- {
- Int32 r = this.getRows();
- Int32 c = this.getCols();
- CMatrix newMatrix = new CMatrix(r, c);
- for (Int32 i = 0; i < r; i++)
- {
- for (Int32 j = 0; j < c; j++)
- {
- newMatrix[i, j] = this[r - i - 1, c - j - 1];
- }
- }
- this = newMatrix;
- }
- /// <summary>
- /// 顺时针旋转270
- /// </summary>
- /// <returns></returns>
- private void rotateR270()
- {
- Int32 r = this.getRows();
- Int32 c = this.getCols();
- CMatrix newMatrix = new CMatrix(c, r);
- for (Int32 i = 0; i < c; i++)
- {
- for (Int32 j = 0; j < r; j++)
- {
- newMatrix[i, j] = this[j, c - i - 1];
- }
- }
- this = newMatrix;
- }
- /// <summary>
- /// 逆时针旋转90
- /// </summary>
- /// <returns></returns>
- private void rotateL90()
- {
- Int32 r = this.getRows();
- Int32 c = this.getCols();
- CMatrix newMatrix = new CMatrix(c, r);
- for (Int32 i = 0; i < c; i++)
- {
- for (Int32 j = 0; j < r; j++)
- {
- newMatrix[i, j] = this[j, c - i - 1];
- }
- }
- this = newMatrix;
- }
- /// <summary>
- /// 逆时针旋转180
- /// </summary>
- /// <returns></returns>
- private void rotateL180()
- {
- Int32 r = this.getRows();
- Int32 c = this.getCols();
- CMatrix newMatrix = new CMatrix(r, c);
- for (Int32 i = 0; i < r; i++)
- {
- for (Int32 j = 0; j < c; j++)
- {
- newMatrix[i, j] = this[r - i - 1, c - j - 1];
- }
- }
- this = newMatrix;
- }
- /// <summary>
- /// 逆时针旋转270
- /// </summary>
- /// <returns></returns>
- private void rotateL270()
- {
- Int32 r = this.getRows();
- Int32 c = this.getCols();
- CMatrix newMatrix = new CMatrix(c, r);
- for (Int32 i = 0; i < c; i++)
- {
- for (Int32 j = 0; j < r; j++)
- {
- newMatrix[i, j] = this[r - j - 1, i];
- }
- }
- this = newMatrix;
- }
- #endregion
- /// <summary>
- /// 输出矩阵
- /// </summary>
- /// <returns></returns>
- public override string ToString()
- {
- StringBuilder strB = new StringBuilder();
- for (Int32 i = 0; i < getRows(); i++)
- {
- for (Int32 j = 0; j < getCols(); j++)
- {
- strB.Append(m_matrix[i, j] + " ");
- }
- strB.Append(Environment.NewLine);
- }
- return strB.ToString();
- }
- }
- /// <summary>
- /// 自定义异常
- /// </summary>
- public class CMatrixException : Exception
- {
- public CMatrixException()
- : base()
- {
- }
- public CMatrixException(String msg)
- : base(msg)
- {
- }
- public String getMessage()
- {
- return base.Message;
- }
- }
- }
矩阵的运算:
设有有矩阵A和B,满足A+B、A-B运算的条件是A的行数与B的行数相等,A的列数与B的列数相等;满足A*B运算的条件是A的列数与B的行数相等。
矩阵的旋转:
(A) (B)
a11 a12 a31 a21 a11
a21 a22 ===顺时针90度=>> a32 a22 a12 ===>> B[i,j]=A[r-j-1,i]
a31 a32
(A) (B)
a11 a12 a32 a32
a21 a22 ===顺时针180度=>> a22 a21 ===>> B[i,j]=A[r - i - 1, c - j - 1]
a31 a32 a12 a11
(A) (B)
a11 a12 a12 a22 a32
a21 a22 ===顺时针270度=>> a11 a21 a32 ===>> B[i,j]=A[j, c - i - 1]
a31 a32
逆时针大同小异,就不在描述了。其中r为矩阵行数,c为矩阵列数;i,j为矩阵某行某列。当然,这个旋转算法并不高效,只是满足旋转基本要求。
- using System;
- using System.Text;
- namespace CGraphics
- {
- /// <summary>
- /// 绘图类
- /// </summary>
- public sealed class CDraw
- {
- /// <summary>
- /// 绘制样式符号
- /// </summary>
- private CSymbol m_symbol;
- /// <summary>
- /// 绘制背景颜色
- /// </summary>
- private ConsoleColor m_backcolor;
- /// <summary>
- /// 构造函数
- /// </summary>
- public CDraw()
- {
- this.m_symbol = CSymbol.RECT_EMPTY;
- this.m_backcolor = ConsoleColor.Black;
- }
- #region 绘制字符串
- /// <summary>
- /// 绘制字符串
- /// </summary>
- /// <param name="text">字符串</param>
- /// <param name="rect">字符串绘制范围</param>
- /// <param name="color">前景色</param>
- public void drawText(String text, CRect rect, ConsoleColor color)
- {
- rect.adjustRect(ref rect);
- //不处理空字符串
- if (text == "")
- {
- return;
- }
- //反色处理
- Console.BackgroundColor = this.m_backcolor;
- Console.ForegroundColor = this.m_backcolor == color ? (ConsoleColor)(15 - (Int32)this.m_backcolor) : color;
- Console.SetCursorPosition(rect.getX(), rect.getY());
- //获取绘制范围字节数
- Int32 charLen = rect.getWidth() * rect.getHeight()<<1;
- //字符串字节数
- Int32 textLen = CText.getLength(text);
- //截断字符串适应绘制范围
- text = CText.cutText(text, textLen > charLen ? charLen : textLen);
- //字符串换行
- text = CText.lineBreakText(text, rect.getWidth());
- String[] texts = text.Split(Environment.NewLine.ToCharArray());
- Int32 i = 0;
- foreach (String s in texts)
- {
- if (s != "")
- {
- Console.SetCursorPosition(rect.getX(), rect.getY() + i);
- Console.Write(s);
- i++;
- }
- }
- Console.SetCursorPosition(0, 0);
- }
- /// <summary>
- /// 绘制字符串
- /// </summary>
- /// <param name="text">字符串</param>
- /// <param name="x">x</param>
- /// <param name="y">y</param>
- /// <param name="width">绘制范围宽度</param>
- /// <param name="height">绘制范围高度</param>
- /// <param name="color">颜色</param>
- public void drawText(String text, Int32 x, Int32 y, Int32 width, Int32 height, ConsoleColor color)
- {
- drawText(text, new CRect(x, y, width, height), color);
- }
- /// <summary>
- /// 绘制字符串
- /// </summary>
- /// <param name="text">字符串</param>
- /// <param name="point">位置</param>
- /// <param name="width">绘制范围宽度</param>
- /// <param name="height">绘制范围高度</param>
- /// <param name="color">颜色</param>
- public void drawText(String text, CPoint point, Int32 width, Int32 height, ConsoleColor color)
- {
- drawText(text, new CRect(point, width, height), color);
- }
- /// <summary>
- /// 绘制字符串
- /// </summary>
- /// <param name="text">字符串</param>
- /// <param name="x">x</param>
- /// <param name="y">y</param>
- /// <param name="size">绘制范围尺寸</param>
- /// <param name="color">颜色</param>
- public void drawText(String text, Int32 x, Int32 y, CSize size, ConsoleColor color)
- {
- drawText(text, new CRect(x, y, size), color);
- }
- /// <summary>
- /// 绘制字符串
- /// </summary>
- /// <param name="text">字符串</param>
- /// <param name="point">位置</param>
- /// <param name="size">尺寸</param>
- /// <param name="color">颜色</param>
- public void drawText(String text, CPoint point, CSize size, ConsoleColor color)
- {
- drawText(text, new CRect(point, size), color);
- }
- /// <summary>
- /// 绘制字符串
- /// </summary>
- /// <param name="text">字符串</param>
- /// <param name="x">光标列位置</param>
- /// <param name="y">光标行位置</param>
- /// <param name="color">前景色</param>
- public void drawText(String text, Int32 x, Int32 y, ConsoleColor color)
- {
- //不处理空字符串
- if (text == "")
- {
- return;
- }
- //反色处理
- Console.BackgroundColor = this.m_backcolor;
- Console.ForegroundColor = this.m_backcolor == color ? (ConsoleColor)(15 - (Int32)this.m_backcolor) : color;
- Console.SetCursorPosition(x >= Console.WindowWidth ? Console.WindowWidth - 1 : x, y);
- //绘制
- Console.Write(text);
- Console.SetCursorPosition(0, 0);
- }
- /// <summary>
- /// 绘制字符串
- /// </summary>
- /// <param name="text"></param>
- /// <param name="point"></param>
- /// <param name="color"></param>
- public void drawText(String text, CPoint point, ConsoleColor color)
- {
- drawText(text, point.getX(), point.getY(), color);
- }
- #endregion
- #region 绘制矩形
- /// <summary>
- /// 绘制矩形
- /// </summary>
- /// <param name="rect">矩形</param>
- /// <param name="color">前景色</param>
- public void drawRect(CRect rect, ConsoleColor color)
- {
- //调整矩形
- rect.adjustRect(ref rect);
- int x = rect.getX();
- int y = rect.getY();
- int width = rect.getWidth();
- int height = rect.getHeight();
- //根据符号设置颜色
- if (m_symbol == CSymbol.DEFAULT)
- {
- Console.BackgroundColor = color;
- }
- else
- {
- Console.BackgroundColor = this.m_backcolor;
- //反色处理
- Console.ForegroundColor = this.m_backcolor == color ? (ConsoleColor)(15 - (Int32)this.m_backcolor) : color;
- }
- //获取符号字符串
- String charSymbol = CSymbolHelper.getStringFromSymbol(m_symbol);
- //绘制矩形列
- for (Int32 i = 0; i < width; i++)
- {
- Int32 ix = x + (i <<1);
- if (ix >= Console.WindowWidth)
- ix = Console.WindowWidth - 1;
- Console.SetCursorPosition(ix, y);
- Console.Write(charSymbol);
- Console.SetCursorPosition(ix, y + height - 1);
- Console.Write(charSymbol);
- }
- //绘制矩形行
- for (Int32 i = 0; i < height; i++)
- {
- Int32 ix = x + (width <<1) - 2;
- if (ix >= Console.WindowWidth)
- ix = Console.WindowWidth - 1;
- Console.SetCursorPosition(x, y + i);
- Console.Write(charSymbol);
- Console.SetCursorPosition(ix, y + i);
- Console.Write(charSymbol);
- }
- Console.SetCursorPosition(0, 0);
- }
- /// <summary>
- /// 绘制矩形
- /// </summary>
- /// <param name="x">列</param>
- /// <param name="y">行</param>
- /// <param name="width">宽度</param>
- /// <param name="height">高度</param>
- /// <param name="color">颜色</param>
- public void drawRect(Int32 x, Int32 y, Int32 width, Int32 height, ConsoleColor color)
- {
- drawRect(new CRect(x, y, width, height), color);
- }
- /// <summary>
- /// 绘制矩形
- /// </summary>
- /// <param name="point">位置</param>
- /// <param name="size">尺寸</param>
- /// <param name="color">颜色</param>
- public void drawRect(CPoint point, CSize size, ConsoleColor color)
- {
- drawRect(new CRect(point, size), color);
- }
- /// <summary>
- /// 绘制矩形
- /// </summary>
- /// <param name="x">x</param>
- /// <param name="y">y</param>
- /// <param name="size">尺寸</param>
- /// <param name="color">颜色</param>
- public void drawRect(Int32 x, Int32 y, CSize size, ConsoleColor color)
- {
- drawRect(new CRect(x, y, size), color);
- }
- /// <summary>
- /// 绘制矩形
- /// </summary>
- /// <param name="point">位置</param>
- /// <param name="width">宽</param>
- /// <param name="height">高</param>
- /// <param name="color">颜色</param>
- public void drawRect(CPoint point, Int32 width, Int32 height, ConsoleColor color)
- {
- drawRect(new CRect(point, width, height), color);
- }
- #endregion
- #region 填充矩形
- /// <summary>
- /// 填充矩形
- /// </summary>
- /// <param name="rect">矩形</param>
- /// <param name="color">前景色</param>
- public void fillRect(CRect rect, ConsoleColor color)
- {
- //调整矩形
- rect.adjustRect(ref rect);
- int x = rect.getX();
- int y = rect.getY();
- int width = rect.getWidth();
- int height = rect.getHeight();
- //根据符号设置颜色
- if (m_symbol == CSymbol.DEFAULT)
- {
- Console.BackgroundColor = color;
- }
- else
- {
- Console.BackgroundColor = this.m_backcolor;
- Console.ForegroundColor = this.m_backcolor == color ? (ConsoleColor)(15 - (Int32)this.m_backcolor) : color;
- }
- //获取符号字符串
- String charSymbol = CSymbolHelper.getStringFromSymbol(m_symbol);
- StringBuilder strb = new StringBuilder();
- for (Int32 i = 0; i < width; i++)
- {
- strb.Append(charSymbol);
- }
- for (Int32 i = 0; i < height; i++)
- {
- Console.SetCursorPosition(x, y + i);
- Console.Write(strb.ToString());
- }
- Console.SetCursorPosition(0, 0);
- }
- /// <summary>
- /// 填充矩形
- /// </summary>
- /// <param name="x">列</param>
- /// <param name="y">行</param>
- /// <param name="width">宽度</param>
- /// <param name="height">高度</param>
- /// <param name="color">颜色</param>
- public void fillRect(Int32 x, Int32 y, Int32 width, Int32 height, ConsoleColor color)
- {
- fillRect(new CRect(x, y, width, height), color);
- }
- /// <summary>
- /// 填充矩形
- /// </summary>
- /// <param name="point">位置</param>
- /// <param name="size">尺寸</param>
- /// <param name="color">颜色</param>
- public void fillRect(CPoint point, CSize size, ConsoleColor color)
- {
- fillRect(new CRect(point, size), color);
- }
- /// <summary>
- /// 填充矩形
- /// </summary>
- /// <param name="x">x</param>
- /// <param name="y">y</param>
- /// <param name="size">尺寸</param>
- /// <param name="color">颜色</param>
- public void fillRect(Int32 x, Int32 y, CSize size, ConsoleColor color)
- {
- fillRect(new CRect(x, y, size), color);
- }
- /// <summary>
- /// 填充矩形
- /// </summary>
- /// <param name="point">位置</param>
- /// <param name="width">宽</param>
- /// <param name="height">高</param>
- /// <param name="color">颜色</param>
- public void fillRect(CPoint point, Int32 width, Int32 height, ConsoleColor color)
- {
- fillRect(new CRect(point, width, height), color);
- }
- #endregion
- /// <summary>
- /// 用指定背景色清屏
- /// </summary>
- /// <param name="color"></param>
- public void clear(ConsoleColor color)
- {
- this.m_symbol = CSymbol.DEFAULT;
- this.m_backcolor = color;
- fillRect(0, 0, Console.WindowWidth>>1, Console.WindowHeight, color);
- }
- /// <summary>
- /// 设置背景颜色
- /// </summary>
- /// <param name="color"></param>
- public void setBackcolor(ConsoleColor color)
- {
- this.m_backcolor = color;
- }
- /// <summary>
- /// 获取背景颜色
- /// </summary>
- /// <returns></returns>
- public ConsoleColor getBackcolor()
- {
- return this.m_backcolor;
- }
- /// <summary>
- /// 设置控制台绘制符号
- /// </summary>
- /// <param name="symbol"></param>
- public void setDrawSymbol(CSymbol symbol)
- {
- this.m_symbol = symbol;
- }
- /// <summary>
- /// 获取控制台绘制符号
- /// </summary>
- /// <returns></returns>
- public CSymbol getDrawSymbol()
- {
- return this.m_symbol;
- }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- //初始化一个绘图对象
- CDraw draw = new CDraw();
- //绘制字符串
- draw.drawText("测试点结构", 2, 1, ConsoleColor.Blue);
- //初始化一个点point1
- CPoint point1 = new CPoint(1, 2);
- //初始化一个点point2
- CPoint point2 = new CPoint(3, 4);
- //绘制point1
- draw.drawText("点1:" + point1, 4, 2, ConsoleColor.White);
- //绘制point2
- draw.drawText("点2:" + point2, 4, 3, ConsoleColor.White);
- //绘制两点相加
- draw.drawText("点1+点2:" + (point1 + point2), 4, 4, ConsoleColor.White);
- //绘制两点相减
- draw.drawText("点1-点2:" + (point1 - point2), 4, 5, ConsoleColor.White);
- //绘制两点是否相等
- draw.drawText("点1==点2:" + (point1 == point2), 4, 6, ConsoleColor.White);
- //绘制两点是否不等
- draw.drawText("点1!=点2:" + (point1 != point2), 4, 7, ConsoleColor.White);
- //绘制字符串
- draw.drawText("测试矩形结构", 1, 9, 2, 3, ConsoleColor.Red);
- //初始化一个矩形
- CRect rect = new CRect(2, 2, 5, 8);
- draw.drawText("矩形:" + rect, 4, 12, ConsoleColor.White);
- //缩放矩形
- rect.inflate(1, 1);
- draw.drawText("矩形缩放[1,1]:" + rect, 4, 13, ConsoleColor.White);
- //设置绘制符号
- draw.setDrawSymbol(CSymbol.RECT_SOLID);
- draw.drawRect(15, 0, 25, 25, ConsoleColor.White);
- //绘制字符串
- draw.drawText("测试矩阵结构", 32, 1, ConsoleColor.Green);
- //初始化一个矩阵
- CMatrix matrix1 = new CMatrix(3, 3);
- matrix1[0, 0] = 1;
- matrix1[2, 2] = 3;
- matrix1[0, 1] = 1;
- //初始化一个矩阵
- CMatrix matrix2 = new CMatrix(new Int32[3, 3]
- {
- {1,2,3},
- {4,5,6},
- {7,8,9}
- });
- //初始化一个矩阵
- CMatrix matrix3 = new CMatrix(new Int32[2, 3]
- {
- {1,2,3},
- {4,5,6}
- });
- //绘制矩阵
- draw.drawText("矩阵1:", 32, 2, ConsoleColor.White);
- draw.drawText(matrix1.ToString(), 16, 3, 5, 3, ConsoleColor.White);
- //绘制矩阵
- draw.drawText("矩阵2:", 42, 2, ConsoleColor.White);
- draw.drawText(matrix2.ToString(), 21, 3, 5, 3, ConsoleColor.White);
- //绘制矩阵
- draw.drawText("矩阵3:", 52, 2, ConsoleColor.White);
- draw.drawText(matrix3.ToString(), 26, 3, 5, 3, ConsoleColor.White);
- //绘制矩阵
- draw.drawText("矩阵1+矩阵2:", 32, 7, ConsoleColor.White);
- draw.drawText((matrix1 + matrix2).ToString(), 16, 8, 5, 3, ConsoleColor.White);
- //绘制矩阵
- draw.drawText("矩阵2-矩阵1:", 48, 7, ConsoleColor.White);
- draw.drawText((matrix2 - matrix1).ToString(), 24, 8, 5, 3, ConsoleColor.White);
- //绘制矩阵
- draw.drawText("矩阵3*矩阵2:", 64, 7, ConsoleColor.White);
- draw.drawText((matrix3 * matrix2).ToString(), 32, 8, 7, 3, ConsoleColor.White);
- //顺时针旋转矩阵90度
- matrix2.rotate(CRotateMode.R90);
- draw.drawText("顺时针90度旋转矩阵2:", 32, 12, ConsoleColor.White);
- draw.drawText(matrix2.ToString(), 16, 13,5,3, ConsoleColor.White);
- //为了测试先恢复矩阵状态
- matrix2.rotate(CRotateMode.L90);
- //顺时针旋转矩阵180度
- matrix2.rotate(CRotateMode.R180);
- draw.drawText("顺时针180度旋转矩阵2:", 54, 12, ConsoleColor.White);
- draw.drawText(matrix2.ToString(), 27, 13, 5, 3, ConsoleColor.White);
- //为了测试先恢复矩阵状态
- matrix2.rotate(CRotateMode.L180);
- //顺时针旋转矩阵270度
- matrix2.rotate(CRotateMode.R270);
- draw.drawText("顺时针270度旋转矩阵2:", 32, 17, ConsoleColor.White);
- draw.drawText(matrix2.ToString(), 16, 18, 5, 3, ConsoleColor.White);
- matrix3.rotate(CRotateMode.L90);
- draw.drawText("逆时针90度旋转矩阵3:", 54, 17, ConsoleColor.White);
- draw.drawText(matrix3.ToString(), 27, 18, 4, 4, ConsoleColor.White);
- //设置绘制符号
- draw.setDrawSymbol(CSymbol.RECT_EMPTY);
- //填充矩形
- draw.fillRect(2,15,5,5,ConsoleColor.Yellow);
- //设置绘制符号
- draw.setDrawSymbol(CSymbol.RHOMB_SOLID);
- //填充矩形
- draw.fillRect(3, 16, 2, 3, ConsoleColor.Blue);
- //设置绘制符号
- draw.setDrawSymbol(CSymbol.RECT_SOLID);
- //填充矩形
- draw.fillRect(7, 17, 4, 3, ConsoleColor.Blue);
- //绘制一段文字
- draw.drawText("C#游戏编程:《控制台小游戏系列》之《四、游戏渲染模块》",2,21,12,5,ConsoleColor.Cyan);
- Console.ReadLine();
- }
- }