java - 与 Java 的 GeneralPath 等效的 c# 是什么?
解决方案
等效(如果使用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);
推荐阅读
- c# - 下载和使用 ffmpeg.dll
- youtube - 获取付费频道订阅背后的 YouTube 社区帖子
- linux - glibc 在尝试统计不存在的文件时使进程崩溃
- swift - 尝试将 Watch App 上传到 AppStore 时出现错误 ITMS-90164
- scala - 使用 Circe 解析 Json 中的自定义键
- java - 忽略搜索字段中的大小写
- php - mysql 从搜索词中显示用户列表,但排除登录用户
- python - 使用选择时 Altair 工具提示不起作用
- python - 使用 docker-compose 运行 Django 应用程序 - 应用迁移会导致 db 停止响应查询,但不会引发错误
- javascript - 快速字谜检查算法 (HackerRank)