首页 > 代码库 > [转载]关于AutoCAD.NET的辅助方法

[转载]关于AutoCAD.NET的辅助方法

转载自:http://www.cnblogs.com/milian/p/3315000.html

求中点坐标:

        /// <summary>
        /// 中点
        /// </summary>
        /// <param name="StartPoint">起点</param>
        /// <param name="EndPoint">终点</param>
        /// <returns></returns>
        public Point3d Midpoint(Point3d StartPoint, Point3d EndPoint)
        {
            Double x = (StartPoint.X + EndPoint.X)/2;
            Double y = (StartPoint.Y + EndPoint.Y) / 2;
            Double z = (StartPoint.Z + EndPoint.Z) / 2;
            return new Point3d(x, y, z);
        }

  求点到直线的距离:

         /// <summary>
        /// 获得空间点到空间线段的垂直距离
        /// </summary>
        /// <param name="line">直线</param>
        /// <param name="pt">空间的点</param>
        /// <param name="Decimal">小数位数</param>
        /// <returns>点到直线垂直距离</returns>
        public Double Distance(Line line, Point3d pt, Int16 Decimal)
        {
            Double t;
            if (line != null)
            {
                Point3d StartPoint = line.StartPoint;
                Point3d EndPoint = line.EndPoint;
                //直线方向向量
                Point3d dir = new Point3d(StartPoint.X - EndPoint.X, StartPoint.Y - EndPoint.Y, StartPoint.Z - EndPoint.Z);
                //过点且与直线垂直的平面
                t = -(Double)(dir.X * (StartPoint.X - pt.X) + dir.Y * (StartPoint.Y - pt.Y) + dir.Z * (StartPoint.Z - pt.Z)) / (dir.X * dir.X + dir.Y * dir.Y + dir.Z * dir.Z);
                //过点的垂直于直线的平面与该直线的交点
                Point3d fp = new Point3d(StartPoint.X + dir.X * t, StartPoint.Y + dir.Y * t, StartPoint.Z + dir.Z * t);
                return Distance(pt, fp, Decimal);
            }
            else
            {
                return 0;
            }
        }

   求空间两点之间距离:

         /// <summary>
        /// 空间两点之间距离
        /// </summary>
        /// <param name="point1">点1</param>
        /// <param name="point2">点2</param>
        /// <param name="Decimal">小数位数</param>
        /// <returns></returns>
        public Double Distance(Point3d point1, Point3d point2, Int16 Decimal)
        {
            Double a = point1.X - point2.X;
            Double b = point1.Y - point2.Y;
            Double c = point1.Z - point2.Z;
            Double r = a * a + b * b + c * c;
            return Math.Round(Math.Abs(Math.Sqrt(r)), Decimal);
        } 

   判断两条直线是否重合:

        /// <summary>
        /// 判断两条直线是否重合
        /// </summary>
        /// <param name="line1">线1</param>
        /// <param name="line2">线2</param>
        /// <param name="allowance">容差</param>
        /// <returns></returns>
        public Boolean Coincide(Line line1, Line line2, Double allowance)
        {
            LineSegment3d l1 = new LineSegment3d(line1.StartPoint, line1.EndPoint);
            LineSegment3d l2 = new LineSegment3d(line2.StartPoint, line2.EndPoint);
            Tolerance tol = new Tolerance(allowance, allowance);           
            return l1.IsColinearTo(l2, tol);
        }

   判断点是否在直线上:  

         /// <summary>
        /// 判断点是否在直线上
        /// </summary>
        /// <param name="line">直线</param>
        /// <param name="point1">点</param>
        /// <param name="allowance">容差</param>
        /// <returns></returns>
        public Boolean Coincide(Line line, Point3d point1, Double allowance)
        {
            Point3d p1 = line.StartPoint;
            Point3d p2 = line.EndPoint;
            //叉积是否为allowance,判断是否在同一直线上
            if (Math.Abs((p1.X - point1.X) * (p2.Y - point1.Y) - (p2.X - point1.X) * (p1.Y - point1.Y)) < allowance)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

   判断点是否重合:

        /// <summary>
        /// 判断点是否重合
        /// </summary>
        /// <param name="p1">点1</param>
        /// <param name="p2">点2</param>
        /// <param name="allowance">容差</param>
        /// <returns></returns>
        public Boolean Coincide(Point3d p1, Point3d p2, Double allowance)
        {
            if (p1.DistanceTo(p2) < allowance)
                return true;
            else
                return false;
        } 

[转载]关于AutoCAD.NET的辅助方法