如何在两个矩形不垂直对齐时填充它们之间的空间。因此,如果一个矩形向上移动,则它和下一个矩形之间的间隙(在沿相同路径的所有矩形中)看起来很平滑。
示例:
当前:
期望:
我的当前绘图代码:
编辑3: 在研究了一些问题后,我开发了自己的解决方案,虽然不是我想要的解决方案,但希望它可以帮助其他人。现在只需要将其转换为圆角即可。
代码:
产生:
示例:
当前:
![Ugly gap](https://istack.dev59.com/UIHIH.webp)
![Desired look](https://istack.dev59.com/J6HAQ.webp)
public override void Draw(Graphics g)
{
for (int i = 0; i < this._Children.Count; i++)
{
this._Children[i].Draw(g);
if (i != 0 && i + 1 < this._Children.Count)
{
if (this._Children[i].Y != this._Children[i + 1].Y)
{
//Draw circle in gap between?
}
}
}
base.Draw(g);
}
底层绘制:
g.FillRectangle(this.Color, this.X, this.Y, this.Size.Width, this.Size.Height);
g.DrawRectangle(this.Outline, this.X, this.Y, this.Size.Width, this.Size.Height);
修改后:
在遵循Jim和评论者的建议后,我得到了以下内容。数学似乎有误,而且根本没有达到我想要的效果。
GraphicsPath path = new GraphicsPath();
for (int i = 0; i < this._Children.Count; i++)
{
path.AddRectangle(this._Children[i].GetBoundingBox());
if (i < this._Children.Count)
{
Block block = i == 0 ? (Block)this : this._Children[i - 1];
float x = this._Children[i].X < block.X ? this._Children[i].X : block.X;
float y = this._Children[i].Y > block.X ? this._Children[i].Y : block.Y;
float width = System.Math.Abs(this._Children[i].X - block.X);
float height = System.Math.Abs(this._Children[i].Y - block.Y);
path.AddEllipse(x, y, width, height);
}
}
g.FillPath(this._Children[0].Color, path);
g.DrawPath(this._Children[0].Outline, path);
base.Draw(g);
第二次编辑:一直在按照大家的建议进行编辑。现在Jim的代码可以工作了,但只有在你向上移动并开始向右移动时才能绘制。
现在根据TAW的建议,我得到了ArgumentInvalid,我认为这是因为rGap矩形的高度为0。
我按照TAW的实现方式进行了实现:
for (int i = 0; i < this._Children.Count; i++)
{
this._Children[i].Draw(g);
if (i + 1 < this._Children.Count)
{
Block block = i == 0 ? (Block)this : this._Children[i + 1];
Rectangle rec = this._Children[i].GetBoundingBox();
Rectangle rec2 = block.GetBoundingBox();
Rectangle rGap = new Rectangle(Math.Min(rec.X, rec2.X), Math.Min(rec.Y, rec2.Y), 2 * Math.Abs(rec.Left - rec2.Left), 2 * Math.Abs(rec2.Top - rec.Top));
GraphicsPath gp = new GraphicsPath();
gp.AddRectangle(rec);
gp.AddRectangle(rec2);
gp.AddArc(rGap, 0, 360);
gp.FillMode = FillMode.Winding;
g.DrawPath(this._Children[i].Outline, gp);
g.FillPath(this._Children[i].Color, gp);
}
}
base.Draw(g);
编辑3: 在研究了一些问题后,我开发了自己的解决方案,虽然不是我想要的解决方案,但希望它可以帮助其他人。现在只需要将其转换为圆角即可。
代码:
for (int i = 0; i < this._Children.Count; i++)
{
this._Children[i].Draw(g);
Block block = i - 1 < 0 ? (Block)this : this._Children[i - 1];
Rectangle rec = this._Children[i].GetBoundingBox();
Rectangle rec2 = block.GetBoundingBox();
Direction dir = this._Children[i].GetDirection(true);
Direction dir2 = block.GetDirection(true);
int minX = Math.Min(rec.X, rec2.X);
int minY = Math.Min(rec.Y, rec2.Y);
int maxX = Math.Max(rec.X, rec2.X);
int maxY = Math.Max(rec.Y, rec2.Y);
int diffX = maxX - minX;
int diffY = maxY - minY;
int width = this._Children[i].Size.Width;
int height = this._Children[i].Size.Height;
Rectangle fillRec = default(Rectangle);
if ((dir == Direction.Right && dir2 == Direction.Down) || (dir == Direction.Up && dir2 == Direction.Left))
{
fillRec = new Rectangle(minX + width, minY, diffX, diffY);
}
else if ((dir == Direction.Down && dir2 == Direction.Left) || (dir == Direction.Right && dir2 == Direction.Up))
{
fillRec = new Rectangle(minX + width, (maxY + height) - diffY, diffX, diffY);
}
else if ((dir == Direction.Up && dir2 == Direction.Right) || (dir == Direction.Left && dir2 == Direction.Down))
{
fillRec = new Rectangle(minX, minY, diffX, diffY);
}
else if ((dir == Direction.Left && dir2 == Direction.Up) || (dir == Direction.Down && dir2 == Direction.Right))
{
fillRec = new Rectangle(minX, (maxY + height) - diffY, diffX, diffY);
}
if (fillRec != default(Rectangle))
{
g.FillRectangle(this._Children[i].Color, fillRec);
g.DrawRectangle(this._Children[i].Outline, fillRec);
}
}
base.Draw(g);
产生:
![输入图像描述](https://istack.dev59.com/K4BsA.webp)