⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 common.cs

📁 苏金明编写的《用VB.NET和VC#.NET开发交互式CAD系统》一书的源代码
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.Drawing.Drawing2D ;

namespace VCSharp
{
	public class Const
	{
		public const float PI = 3.1416f;
		public const int PickRadius=3;
	}

	public enum DrawMode
	{
		Normal,Selec,Drag,Delete
	}

	public enum Type
	{
		Line,Circle,Arc,Text
	}

	public enum Style
	{
		Solid,Dash,Dot,DashDot,DashDotDot
	}

	[Serializable()]public class Module{

		public ArrayList ges=new ArrayList();
		public ArrayList geSels=new ArrayList();


		//判断拾取点是否位于包围矩形中
		public bool InBox(CBox aBox, PointF aPos){
			if (aPos.X > aBox.minX && aPos.Y > aBox.minY && aPos.X < aBox.maxX && aPos.Y < aBox.maxY) 
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		//计算点与点之间的距离
		public float DistPtoP(PointF p1, PointF p2){
			float dx,dy;
			dx = p2.X - p1.X;
			dy = p2.Y - p1.Y;
			float dist=(float)(Math.Sqrt((dx * dx) + (dy * dy)));
			return dist;
		}

		//计算点间连线与X轴正向之间的夹角
		public float GetAngle(PointF p1,PointF p2){
			float tansita,sita,dx;
			float angle=0;
			dx = Math.Abs(p2.X - p1.X);
			if (p2.X == p1.X){dx = 0.0001f;}
			tansita = Math.Abs(p2.Y - p1.Y) / dx;
			sita = (float)(Math.Atan(tansita));
			//如果终点横坐标大于、等于起点横坐标,并且终点纵坐标大于、等于起点纵坐标
			if (p2.X >= p1.X && p2.Y >= p1.Y){angle=sita;}
			//如果终点横坐标小于、等于起点横坐标,并且终点纵坐标大于、等于起点纵坐标
			if (p2.X <= p1.X && p2.Y >= p1.Y){angle=Const.PI - sita;}
			//如果终点横坐标小于、等于起点横坐标,并且终点纵坐标小于、等于起点纵坐标
			if (p2.X <= p1.X && p2.Y <= p1.Y) {angle=Const.PI + sita;}
			//如果终点横坐标大于、等于起点横坐标,并且终点纵坐标小于、等于起点纵坐标
			if (p2.X >= p1.X && p2.Y <= p1.Y) {angle=Const.PI * 2 - sita;}

			return angle;
		}

		public void DrawAll(Graphics g,ArrayList ges){
			for(int i = 0;i<ges.Count;i++){
				CGElement ge=(CGElement)(ges[i]);
				ge.Draw(g, DrawMode.Normal);
			}
		}

		public void DrawSel(Graphics g,ArrayList geSels){
			for(int i = 0;i<geSels.Count;i++)
			{
				CGElement ge=(CGElement)(geSels[i]);
				ge.Draw(g, DrawMode.Selec);
			}
		}

		public void Coordinate(Graphics g,ArrayList ges,
			            float viewDX,float viewDY,float viewScale)
		{
			g.TranslateTransform(viewDX, viewDY, MatrixOrder.Append);
			g.ScaleTransform(viewScale, viewScale, MatrixOrder.Append);
			g.Clear(Color.White);
			DrawAll(g,ges);
		}

		//页面坐标转换为通用坐标
		public PointF PagetoWorld(PointF ep){
			Form1 f=new Form1();
			Rectangle rect=f.ClientRectangle;
			float viewDX=rect.Width/2;
			float viewDY=rect.Height/2;
			PointF p=new PointF();
			p.X = ep.X - viewDX;
			p.Y = viewDY-ep.Y;

			return p;
		}

		//通用坐标转换为页面坐标
		public PointF WorldtoPage(PointF pp){
			Form1 f=new Form1();
			Rectangle rect=f.ClientRectangle;
			float viewDX=rect.Width/2;
			float viewDY=rect.Height/2;
			PointF p=new PointF();
			p.X = pp.X + viewDX;
			p.Y = -pp.Y + viewDY;
			return p;
		}

		//计算旋转变换以后点的坐标
		public PointF pRotate(PointF baseP,PointF Pos,float angle){
			PointF pr=new PointF();
			float cosv;
			float sinv;

			cosv = (float)(Math.Cos(angle));
			sinv = (float)(Math.Sin(angle));
			pr.X = Pos.X * cosv - Pos.Y * sinv + (1 - cosv) * baseP.X + baseP.Y * sinv;
			pr.Y = sinv * Pos.X + cosv * Pos.Y + (1 - cosv) * baseP.Y - sinv * baseP.X;
			return pr;
		}

		//计算镜像以后点的坐标
		public PointF pMirror(PointF Pos1,PointF Pos2, PointF Pos){
			PointF pm=new PointF();
			float Angle;
			float cos2v,sin2v;
			float x1,y1;
			float x2,y2;
			float aa;
			float desX,desY;
			x1 =Pos1 .X;
			y1 =Pos1.Y;
			x2 =Pos2 .X;
			y2 = Pos2.Y;
			if (x2 == x1) {
				aa = 10000000;
			}
			else
			{
				aa = (x2 * y1 - x1 * y2) / (x2 - x1);
			}
			Angle = GetAngle(Pos1, Pos2);
			cos2v = (float)((Math.Cos(Angle * 2)));
			sin2v = (float)((Math.Sin(Angle * 2)));
			desX = Pos.X * cos2v + Pos.Y * sin2v - aa * sin2v;
			desY = Pos.X * sin2v - Pos.Y * cos2v + aa * cos2v + aa;
			pm.X = desX;
			pm.Y = desY;
			return pm;
		}

		public void ScaleZoom(Graphics g, float dx, float dy,ArrayList geSels){
			for (int i = 0;i<geSels.Count;i++){
				((CGElement)(geSels[i])).Draw(g, DrawMode.Delete);
				((CGElement)(geSels[i])).Scale(g, dx, dy);
				((CGElement)(geSels[i])).Draw(g, DrawMode.Selec);
			}
		}
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -