Example #1
0
        public BoundingBox(Point[] corners)
        {
            // compute the fourth point of the rotated rect, pt4 = pt3 - pt2 + p1
            this.corners = new Point[4];
            for (int i = 0; i < 3; i++)
            {
                this.corners[i] = corners[i];
            }
            this.corners[3] = PA.Add(PA.Subtract(corners[2], corners[1]), corners[0]);

            // reorder points
            this.corners = reOderPoints(this.corners);
            this.length  = PA.Norm(this.corners[0], this.corners[1]);
            this.width   = PA.Norm(this.corners[0], this.corners[3]);
            this.angle   = Math.Atan2(this.corners[1].Y - this.corners[0].Y,
                                      this.corners[1].X - this.corners[0].X) / Math.PI * 180;
            this.angle = this.angle > 90 ? this.angle - 180 : this.angle; // (-90, 90]
            // compute the center of rotated rect
            int xc = 0, yc = 0;

            for (int i = 0; i < 4; i++)
            {
                xc += this.corners[i].X;
                yc += this.corners[i].Y;
            }
            this.center = new Point(xc / 4, yc / 4);
        }
Example #2
0
        public static Point Project2Vector(Point pt1, Point pt2, Point pt3)
        {
            // project a vector v1 formed by pt1-pt3 into another vector v2 formed by pt1-pt2
            Point  v1            = PA.Subtract(pt3, pt1);
            Point  v2            = PA.Subtract(pt2, pt1);
            float  scale         = (float)(PA.Dot(v1, v2) / PA.Norm(v2) / PA.Norm(v2));
            PointF projected_pt3 = PA.Add(PA.Multiply(PA.Int2Float(v2), scale), pt1);

            return(PA.Float2Int(projected_pt3));
        }
Example #3
0
        public void ShiftCenterTo(Point pt)
        {
            Point transpt = PA.Subtract(pt, this.center);

            for (int i = 0; i < 4; i++)
            {
                this.corners[i] = PA.Add(this.corners[i], transpt);
            }
            this.center = pt;
        }
Example #4
0
        public static Point Project2NormalVector(Point pt1, Point pt2, Point pt3)
        {
            /* project a vector v1 formed by pt1-pt3 into the direction of
             * the normal vector of another vector v2 formed by pt1-pt2 */

            // compute the unit normal vector of vector v2
            Point  v2          = PA.Subtract(pt2, pt1);
            float  magnitude   = (float)(PA.Norm(pt1, pt2));
            PointF unit_normal = new PointF(v2.Y / magnitude, -v2.X / magnitude);
            // project v1 into the normal vector
            Point  v1           = PA.Subtract(pt3, pt1);
            PointF projected    = PA.Multiply(unit_normal, (float)PA.Dot(v1, unit_normal));
            PointF projected_v3 = PA.Add(projected, pt1);

            return(PA.Float2Int(projected_v3));
        }
Example #5
0
        public void ShiftDirection(int ind, int step = 3)
        {
            // ind = 0 --> move left
            // ind = 1 --> move right
            // ind = 2 --> move up
            // ind = 3 --> move down
            Point transpt = new Point(0, 0);

            switch (ind)
            {
            case 0: transpt = new Point(-step, 0); break;

            case 1: transpt = new Point(step, 0); break;

            case 2: transpt = new Point(0, -step); break;

            case 3: transpt = new Point(0, step); break;
            }
            for (int i = 0; i < 4; i++)
            {
                this.corners[i] = PA.Add(this.corners[i], transpt);
            }
            this.center = PA.Add(this.center, transpt);
        }