-1

I have 5 vertices of a Rectangle. I want to inflate the rectangle to a certain no. say x. How can I dot that? The rectangle is a custom struct not System.Drawing.Rectangle object. Please suggest.

WAQ
  • 2,556
  • 6
  • 45
  • 86
  • 2
    A two-dimensional rectangle has four vertices, and a three-dimensional rectangle has eight. Hm, 3D then? And what is that supposed to mean: _inflate the rectangle to a certain no_ ?? Enlarge the side by a factor? – TaW Jan 12 '15 at 10:58
  • 1st and 5th vertex is basically the same to make it a closed Rectangle shape. So, its a two-dimensional rectangle. – WAQ Jan 12 '15 at 10:59
  • I assume it is rotated (or else the question would be trivial). I would calculate the midpoint and the angles of the diagonals. then the distance form MP to each corner can be multipied by the factor to find th new corners.. – TaW Jan 12 '15 at 11:08
  • Can you show your code and a simple example? – Rufus L Jan 12 '15 at 12:15
  • `y = mx + b`. You can average the coordinates (X and Y, respectively) of the pairs of diagonally opposite vertexes to find the center. Then find `m` and `b` for both diagonals. Finally, use the line equation to extend each vertex to a new location some specific "inflation distance" (you haven't explained how you want to specify that "inflate distance", but you can do it a variety of ways, accounting for the rotation of the rectangle). The one answer already given is an okay start, but it uses an arbitrary "ratio" to define the inflation amount, and probably isn't general enough. – Peter Duniho Jan 12 '15 at 21:13

1 Answers1

0

This inflates rectangle relative to its center:

    struct Rect {
        public PointF p1 { get; set; }
        public PointF p2 { get; set; }
        public PointF p3 { get; set; }
        public PointF p4 { get; set; }
        public PointF p5 { get; set; }
    }

    void RectResize()
    {
        // input rectangle and sample vertices
        Rect input = new Rect();
        input.p1 = new PointF(80, 200);
        input.p2 = new PointF(160, 340);
        input.p3 = new PointF(470, 160);
        input.p4 = new PointF(390, 20);
        input.p5 = new PointF(80, 200);  // same as p1

        PointF[] r1 = { input.p1, input.p2, input.p3, input.p4 }; // conversion to array, easier to manipulate
        float ratio = .3F;  // inflation factor
        PointF center = new PointF(r1[0].X + (r1[2].X - r1[0].X) / 2, r1[0].Y + (r1[2].Y - r1[0].Y) / 2);
        PointF[] r2 = new PointF[4];  // output array
        for (int i = 0; i < 4; i++)
        {
            r2[i].X = center.X + (r1[i].X - center.X) * ratio;
            r2[i].Y = center.Y + (r1[i].Y - center.Y) * ratio;
        }

        // convert output to struct Rect if needed
        Rect output = new Rect();
        output.p1 = r2[0]; output.p2 = r2[1]; output.p3 = r2[2]; output.p4 = r2[3]; output.p5 = r2[0];

        // demo
        Graphics g = this.CreateGraphics();
        g.DrawPolygon(Pens.Blue, r1);
        g.DrawPolygon(Pens.Red, r2);
    }
Ponder Stibbons
  • 14,723
  • 2
  • 21
  • 24