Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何为给定顶点的矩形形状充气_C#_System.drawing_Rectangles - Fatal编程技术网

C# 如何为给定顶点的矩形形状充气

C# 如何为给定顶点的矩形形状充气,c#,system.drawing,rectangles,C#,System.drawing,Rectangles,我有一个矩形的5个顶点。我想把长方形膨胀到一定的数值,比如说x。我怎么能把它打点呢?矩形是自定义结构,而不是System.Drawing.rectangle对象。请建议。此矩形相对于其中心膨胀: struct Rect { public PointF p1 { get; set; } public PointF p2 { get; set; } public PointF p3 { get; set; } public Po

我有一个矩形的5个顶点。我想把长方形膨胀到一定的数值,比如说x。我怎么能把它打点呢?矩形是自定义结构,而不是System.Drawing.rectangle对象。请建议。

此矩形相对于其中心膨胀:

    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);
    }
struct Rect{
公共点p1{get;set;}
公共点f p2{get;set;}
公共点f p3{get;set;}
公共点f p4{get;set;}
公共点f p5{get;set;}
}
void rect resize()
{
//输入矩形和采样顶点
Rect输入=新的Rect();
input.p1=新的点F(80200);
input.p2=新的点f(160340);
input.p3=新的点F(470160);
input.p4=新的点F(390,20);
input.p5=新的点F(80200);//与p1相同
PointF[]r1={input.p1,input.p2,input.p3,input.p4};//转换为数组,更容易操作
浮动比率=.3F;//通货膨胀系数
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=新的PointF[4];//输出数组
对于(int i=0;i<4;i++)
{
r2[i].X=center.X+(r1[i].X-center.X)*比值;
r2[i].Y=center.Y+(r1[i].Y-center.Y)*比值;
}
//如果需要,将输出转换为struct Rect
Rect输出=新的Rect();
output.p1=r2[0];output.p2=r2[1];output.p3=r2[2];output.p4=r2[3];output.p5=r2[0];
//演示
Graphics g=this.CreateGraphics();
g、 DrawPolygon(蓝色钢笔,r1);
g、 DrawPolygon(Pens.Red,r2);
}

二维矩形有四个顶点,三维矩形有八个顶点。嗯,那是3D吗?这意味着什么:将矩形膨胀到一定的数值??将侧面放大一倍?第一个和第五个顶点基本相同,使其成为闭合矩形。所以,它是一个二维矩形。我假设它是旋转的(否则问题就很简单)。我会计算对角线的中点和角度。然后,从MP到每个角的距离可以乘以因子,以找到新的角。您能展示您的代码和一个简单的示例吗?
y=mx+b
。可以平均对角顶点对的坐标(分别为X和Y)以找到中心。然后找到两条对角线的
m
b
。最后,使用直线方程将每个顶点延伸到一个新位置,即某个特定的“膨胀距离”(您没有解释如何指定“膨胀距离”,但可以通过多种方式来指定,考虑到矩形的旋转)。已经给出的一个答案是一个好的开始,但它使用了一个任意的“比率”来定义通胀量,而且可能不够笼统。