首页 > 解决方案 > 与 Java 的 GeneralPath 等效的 c# 是什么?

问题描述

假设我需要将此java 程序转换为 C# 程序。

GeneralPath在包中找到的与 java 等效的 C# 是什么java.awt.geom

标签: javac#

解决方案


等效(如果使用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);

推荐阅读