英文:
What is the c# equivalent to Java's GeneralPath?
问题
以下是翻译好的内容:
"Say I need to covert this java program into a C# program.
What is the C# equivalent to java's GeneralPath found in the package java.awt.geom?"
英文:
Say I need to covert this java program into a C# program.
What is the C# equivalent to java's GeneralPath found in the package java.awt.geom?
答案1
得分: 2
以下是翻译好的内容:
等效的(如果使用 System.Drawing)是:
System.Drawing.Drawing2D.GraphicsPath
你可以这样绘制线条:
var gp1 = new GraphicsPath();
gp1.AddLines(new Point[]
{
    new Point(50, 10),
    new Point(70, 80),
    new Point(90, 40),
    new Point(10, 40),
    new Point(50, 80)
});
gp1.CloseFigure();
e.Graphics.DrawPath(new Pen(new SolidBrush(Color.Red)), gp1);
GraphisPath 没有二次贝塞尔曲线,但你可以将其拟合为三次贝塞尔曲线。以下是执行此操作的扩展方法:
public static class GraphicsPathExtensions
{
    public static void AddQuadraticBezier(
        this GraphicsPath graphicsPath, PointF start, PointF ctrl, PointF end)
    {
        var c1 = new PointF(start.X, start.Y);
        c1.X += (2.0f / 3.0f) * (ctrl.X - start.X);
        c1.Y += (2.0f / 3.0f) * (ctrl.Y - start.Y);
        var c2 = new PointF(ctrl.X, ctrl.Y);
        c2.X += (end.X - ctrl.X) / 3.0f;
        c2.Y += (end.Y - ctrl.Y) / 3.0f;
        graphicsPath.AddBezier(start, c1, c2, end);
    }
    public static void AddQuadraticBezier(
        this GraphicsPath graphicsPath, Point start, Point ctrl, Point end)
    {
        graphicsPath.AddQuadraticBezier(
            new PointF(start.X, start.Y),
            new PointF(ctrl.X, ctrl.Y),
            new PointF(end.X, end.Y));
    }
}
你可以这样调用它:
var gp6 = new GraphicsPath();
gp6.AddQuadraticBezier(
    new Point(120, 180),
    new Point(150, 120),
    new Point(180, 180));
gp6.CloseFigure();
e.Graphics.DrawPath(new Pen(new SolidBrush(Color.Red)), gp6);
以下是所有示例的示例:
var gp7 = new GraphicsPath();
gp7.AddBezier(
    new Point(220, 150),
    new Point(240, 130),
    new Point(280, 160),
    new Point(300, 140));
gp7.AddLine(
    new Point(300, 140),
    new Point(300, 180));
gp7.AddQuadraticBezier(
    new Point(300, 180),
    new Point(260, 160),
    new Point(220, 180));
gp7.CloseFigure();
e.Graphics.DrawPath(new Pen(new SolidBrush(Color.Red)), gp7);
英文:
The equivalent (if using System.Drawing) is:
System.Drawing.Drawing2D.GraphicsPath
You can do lines as so:
var gp1 = new GraphicsPath();
gp1.AddLines(new Point[]
{
	new Point(50, 10),
	new Point(70, 80),
	new Point(90, 40),
	new Point(10, 40),
	new Point(50, 80)
});
gp1.CloseFigure();
e.Graphics.DrawPath(new Pen(new SolidBrush(Color.Red)), gp1);
GraphisPath does not have a Quadratic Bezier, but you can fit it in to a Cubic Bezier. Here is an extension to do that:
public static class GraphicsPathExtensions
{
	public static void AddQuadraticBezier(
		this GraphicsPath graphicsPath, PointF start, PointF ctrl, PointF end)
	{
		var c1 = new PointF(start.X, start.Y);
		c1.X += (2.0f / 3.0f) * (ctrl.X - start.X);
		c1.Y += (2.0f / 3.0f) * (ctrl.Y - start.Y);
		var c2 = new PointF(ctrl.X, ctrl.Y);
		c2.X += (end.X - ctrl.X) / 3.0f;
		c2.Y += (end.Y - ctrl.Y) / 3.0f;
		graphicsPath.AddBezier(start, c1, c2, end);
	}
	public static void AddQuadraticBezier(
		this GraphicsPath graphicsPath, Point start, Point ctrl, Point end)
	{
		graphicsPath.AddQuadraticBezier(
			new PointF(start.X, start.Y),
			new PointF(ctrl.X, ctrl.Y),
			new PointF(end.X, end.Y));
	}
}
You could call it as so:
var gp6 = new GraphicsPath();
gp6.AddQuadraticBezier(
	new Point(120, 180),
	new Point(150, 120),
	new Point(180, 180));
gp6.CloseFigure();
e.Graphics.DrawPath(new Pen(new SolidBrush(Color.Red)), gp6);
Here is an example of all of them:
var gp7 = new GraphicsPath();
gp7.AddBezier(
	new Point(220, 150),
	new Point(240, 130),
	new Point(280, 160),
	new Point(300, 140));
gp7.AddLine(
	new Point(300, 140),
	new Point(300, 180));
gp7.AddQuadraticBezier(
	new Point(300, 180),
	new Point(260, 160),
	new Point(220, 180));
gp7.CloseFigure();
e.Graphics.DrawPath(new Pen(new SolidBrush(Color.Red)), gp7);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论