GraphicsPath.Widen()
.Net のメソッドの反対が必要です。
public GraphicsPath Widen()
Widen()
メソッドは負のパラメーターを受け入れないため、メソッドに相当するものが必要ですInset
。
public GraphicsPath Inset()
オープン ソースの Inkscape アプリケーション (www.Inkscape.org) でこれを行うには、メニューに移動して [パス / インセット] を選択します (インセット量は [Inkscape プロパティ] ダイアログに保存されます)。Inkscape はオープン ソースであるため、C#.Net でこれを実行できるはずですが、一生 Inkscape C++ ソースをたどることはできません (必要なのはこの 1 つの関数だけなので、C++ の学習を正当化することはできません)。これを完了するために)。
基本的に、次のシグネチャを持つ GraphicsPath 拡張メソッドが必要です。
public static GraphicsPath Inset(this GraphicsPath original, float amount)
{
//implementation
}
署名が述べているように、渡された量だけGraphicsPath
オブジェクトと.Inset()
パスを受け取ります... Inkscapeが今日行っているように。問題を単純化すると、問題の GraphicsPaths はすべて.PolyBezier
メソッドから作成されます (他には何もありません)。したがって、完全を期す場合を除き、四角形、楕円形、またはその他の形状を考慮する必要はありません。
残念ながら、私は C++ コードの経験がないので、Inkscape に含まれる C++ ロジックに従うことはほぼ不可能です。
.
[編集:] リクエストに応じて、「MakeOffset」Inkscape コードを次に示します。2 番目のパラメーター (double dec) は Inset の負の値であり、そのパラメーターの絶対値は形状を取り込む量です。
ここには多くの依存関係があることを知っています。Inkscape のソース ファイルをもっと見る必要がある場合は、ここにあります: http://sourceforge.net/projects/inkscape/files/inkscape/0.48/
int
Shape::MakeOffset (Shape * a, double dec, JoinType join, double miter, bool do_profile, double cx, double cy, double radius, Geom::Matrix *i2doc)
{
Reset (0, 0);
MakeBackData(a->_has_back_data);
bool done_something = false;
if (dec == 0)
{
_pts = a->_pts;
if (numberOfPoints() > maxPt)
{
maxPt = numberOfPoints();
if (_has_points_data) {
pData.resize(maxPt);
_point_data_initialised = false;
_bbox_up_to_date = false;
}
}
_aretes = a->_aretes;
if (numberOfEdges() > maxAr)
{
maxAr = numberOfEdges();
if (_has_edges_data)
eData.resize(maxAr);
if (_has_sweep_src_data)
swsData.resize(maxAr);
if (_has_sweep_dest_data)
swdData.resize(maxAr);
if (_has_raster_data)
swrData.resize(maxAr);
if (_has_back_data)
ebData.resize(maxAr);
}
return 0;
}
if (a->numberOfPoints() <= 1 || a->numberOfEdges() <= 1 || a->type != shape_polygon)
return shape_input_err;
a->SortEdges ();
a->MakeSweepDestData (true);
a->MakeSweepSrcData (true);
for (int i = 0; i < a->numberOfEdges(); i++)
{
// int stP=a->swsData[i].stPt/*,enP=a->swsData[i].enPt*/;
int stB = -1, enB = -1;
if (dec > 0)
{
stB = a->CycleNextAt (a->getEdge(i).st, i);
enB = a->CyclePrevAt (a->getEdge(i).en, i);
}
else
{
stB = a->CyclePrevAt (a->getEdge(i).st, i);
enB = a->CycleNextAt (a->getEdge(i).en, i);
}
Geom::Point stD, seD, enD;
double stL, seL, enL;
stD = a->getEdge(stB).dx;
seD = a->getEdge(i).dx;
enD = a->getEdge(enB).dx;
stL = sqrt (dot(stD,stD));
seL = sqrt (dot(seD,seD));
enL = sqrt (dot(enD,enD));
MiscNormalize (stD);
MiscNormalize (enD);
MiscNormalize (seD);
Geom::Point ptP;
int stNo, enNo;
ptP = a->getPoint(a->getEdge(i).st).x;
double this_dec;
if (do_profile && i2doc) {
double alpha = 1;
double x = (Geom::L2(ptP * (*i2doc) - Geom::Point(cx,cy))/radius);
if (x > 1) {
this_dec = 0;
} else if (x <= 0) {
this_dec = dec;
} else {
this_dec = dec * (0.5 * cos (M_PI * (pow(x, alpha))) + 0.5);
}
} else {
this_dec = dec;
}
if (this_dec != 0)
done_something = true;
int usePathID=-1;
int usePieceID=0;
double useT=0.0;
if ( a->_has_back_data ) {
if ( a->ebData[i].pathID >= 0 && a->ebData[stB].pathID == a->ebData[i].pathID && a->ebData[stB].pieceID == a->ebData[i].pieceID
&& a->ebData[stB].tEn == a->ebData[i].tSt ) {
usePathID=a->ebData[i].pathID;
usePieceID=a->ebData[i].pieceID;
useT=a->ebData[i].tSt;
} else {
usePathID=a->ebData[i].pathID;
usePieceID=0;
useT=0;
}
}
if (dec > 0)
{
Path::DoRightJoin (this, this_dec, join, ptP, stD, seD, miter, stL, seL,
stNo, enNo,usePathID,usePieceID,useT);
a->swsData[i].stPt = enNo;
a->swsData[stB].enPt = stNo;
}
else
{
Path::DoLeftJoin (this, -this_dec, join, ptP, stD, seD, miter, stL, seL,
stNo, enNo,usePathID,usePieceID,useT);
a->swsData[i].stPt = enNo;
a->swsData[stB].enPt = stNo;
}
}
if (dec < 0)
{
for (int i = 0; i < numberOfEdges(); i++)
Inverse (i);
}
if ( _has_back_data ) {
for (int i = 0; i < a->numberOfEdges(); i++)
{
int nEd=AddEdge (a->swsData[i].stPt, a->swsData[i].enPt);
ebData[nEd]=a->ebData[i];
}
} else {
for (int i = 0; i < a->numberOfEdges(); i++)
{
AddEdge (a->swsData[i].stPt, a->swsData[i].enPt);
}
}
a->MakeSweepSrcData (false);
a->MakeSweepDestData (false);
return (done_something? 0 : shape_nothing_to_do);
}
.
[編集]
@Simon Mourier - すばらしい仕事です。コードもきれいで読みやすいものでした! よくやった、サー。ただし、いくつか質問があります。
まず、Amount の正の数は何を表しているのでしょうか。Offset メソッドの場合、正は「アウトセット」、負は「インセット」になると考えていましたが、あなたの例は逆のようです。
次に、いくつかの基本的なテスト (サンプルを拡張するだけ) を行ったところ、奇妙な点がいくつか見つかりました。
オフセットが大きくなると、cool の "l" に何が起こるかを次に示します (このような単純な文字の場合、問題が発生するのは確かです!)。
...そしてそれを再現するコード:
private void Form1_Paint(object sender, PaintEventArgs e)
{
GraphicsPath path = new GraphicsPath();
path.AddString("cool", new FontFamily("Arial"), 0, 200, new PointF(), StringFormat.GenericDefault);
GraphicsPath offset1 = path.Offset(32);
e.Graphics.DrawPath(new Pen(Color.Black, 1), path);
e.Graphics.DrawPath(new Pen(Color.Red, 1), offset1);
}
最後に、少し違うものを。これは Wingdings の「S」の文字です (涙のしずくのように見えます)。
コードは次のとおりです。
private void Form1_Paint(object sender, PaintEventArgs e)
{
GraphicsPath path = new GraphicsPath();
path.AddString("S", new FontFamily("Wingdings"), 0, 200, new PointF(), StringFormat.GenericDefault);
GraphicsPath offset1 = path.Offset(20);
e.Graphics.DrawPath(new Pen(Color.Black, 1), path);
e.Graphics.DrawPath(new Pen(Color.Red, 1), offset1);
}
ほら、近すぎて泣きたくなる。しかし、それでもうまくいきません。
挿入ベクトルがいつ交差するかを確認し、その時点を過ぎて挿入を停止することで修正できると思います。Inset の量が非常に大きい (またはパスが非常に小さい) ために何も残っていない場合、パス自体を元に戻して再度拡張するのではなく、パスが消える (null になる) 必要があります。
繰り返しますが、私はあなたが行ったことを決して非難しているわけではありませんが、これらの例で何が起こっているのか知っているかどうか疑問に思っていました.
(PS - 拡張メソッドにするために「this」キーワードを追加したため、これらのサンプルを実行するには、メソッド (パラメーター) 表記を使用してコードを呼び出す必要がある場合があります)
.
@RAN Ran は、GraphicsPath ネイティブ メソッドを再利用して、同様の出力を作成します。男、これは大変です。二人ともとても近いです。
以下は、Wingdings の文字「S」を使用した両方の例のスクリーン ショットです。
左が @Simon、右が @Ran です。
これは、Inkscape で「インセット」を実行した後の同じティア ドロップ「S」文字です。インセットはきれいです:
ちなみに、@Ran のテストのコードは次のとおりです。
private void Form1_Paint(object sender, PaintEventArgs e)
{
GraphicsPath path = new GraphicsPath();
path.AddString("S", new FontFamily("Wingdings"), 0, 200, new PointF(), StringFormat.GenericDefault);
e.Graphics.DrawPath(new Pen(Color.Black, 1), path);
GraphicsPath offset1 = path.Shrink(20);
e.Graphics.DrawPath(new Pen(Color.Red, 1), offset1);
}