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

📄 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;
		}
    }
}

⌨️ 快捷键说明

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