Пример #1
0
        public bool Intersect(Plane3D b, out Line3D intersection)
        {
            intersection = null;

            if (IsParallel(b))
            {
                return(false);
            }

            Point3D  p;
            Vector3D v1 = Normal.CrossProduct(b.Normal);
            Vector3D v2 = new Vector3D(v1.X * v1.X, v1.Y * v1.Y, v1.Z * v1.Z);
            double   w1 = -Distance;
            double   w2 = -b.Distance;
            double   id;

            if ((v2.Z > v2.Y) && (v2.Z > v2.X) && (v2.Z > Double.Epsilon))
            {
                // point on XY plane
                id = 1.0 / v1.Z;
                p  = new Point3D(Normal.Y * w2 - b.Normal.Y * w1,
                                 b.Normal.X * w1 - Normal.X * w2,
                                 0.0);
            }
            else if ((v2.Y > v2.X) && (v2.Y > Double.Epsilon))
            {
                // point on XZ plane
                id = -1.0 / v1.Y;
                p  = new Point3D(Normal.Z * w2 - b.Normal.Z * w1,
                                 0.0,
                                 b.Normal.Y * w1 - Normal.Y * w2);
            }
            else if (v2.X > Double.Epsilon)
            {
                // point on YZ plane
                id = 1.0 / v1.X;
                p  = new Point3D(0.0,
                                 Normal.Z * w2 - b.Normal.Z * w1,
                                 b.Normal.Y * w1 - Normal.Y * w2);
            }
            else
            {
                return(false);
            }

            p   = (p.ToVector() * id).ToPoint();
            id  = 1.0 / Math.Sqrt(v2.X + v2.Y + v2.Z);
            v1 *= id;

            intersection = new Line3D(p, p.ToVector() + v1);

            return(true);
        }
Пример #2
0
        /// <summary>
        /// Polygon constructor.
        /// </summary>
        /// <param name="vertices">Vertices of the polygon</param>
        /// <param name="name">Name of the polygon.</param>
        public Polygon3D(IEnumerable <Point3D> vertices, string name) : base(Reorder(vertices, name))
        {
            // All exception tests are in the method Reorder
            Plane  = new Plane(this[1], this[0], this[2]);
            Normal = Plane.Normal;
            Center = Point3D.Centroid(this);
            Radius = this.Max(point => point.DistanceTo(Center));
            Name   = name;

            var edges = this.Select((t, i) => new Line3D(t, this[++i % Count]));

            Edges = new ReadOnlyCollection <Line3D>(edges.ToList());

            var borderNormals = Edges.Select(edge => Normal.CrossProduct(edge.Direction).Negate());

            v_EdgeNormals = new ReadOnlyCollection <UnitVector3D>(borderNormals.ToList());

            var offsets = this.Select((t, i) => - t.ToVector3D().DotProduct(v_EdgeNormals[i]));

            v_D = new ReadOnlyCollection <double>(offsets.ToList());
        }