Geben Sie diesen Code einfach ein und Sie können StreamGeometry.IsLine()
verwenden, um nach Zeilen zu suchen.
public static class StreamGeometryExtensions
{
public static bool IsLine(this StreamGeometry sg, double sampleRate = 0.2, decimal tolerance = 0
{
PathGeometry g = sg.GetFlattenedPathGeometry();
if (g.MayHaveCurves())
{
return false;
}
Point origin, originTangent;
Point end, endTangent;
g.GetPointAtFractionLength(0, out origin, out originTangent);
g.GetPointAtFractionLength(1, out end, out endTangent);
Vector originToEnd = end - origin;
for (double i = 0; i < 1; i += sampleRate)
{
Point current, currentTangent;
g.GetPointAtFractionLength(i, out current, out currentTangent);
Vector currentToEnd = end - current;
Vector originTocurrent = current - origin;
decimal l1 = (decimal)(originTocurrent.Length + currentToEnd.Length);
decimal l2 = (decimal)originToEnd.Length;
if (Math.Abs(l2 - l1) > (l2 * tolerance))
{
return false;
}
}
return true;
}
}
der Code „samples“ der Weg jedes X% (Beispiel 0,2 = 20%) und überprüft, ob der Ursprung, das Ende und die aktuellen Abtastpunkt auf einer Linie.
Der Code ist eine sehr allgemeine Lösung des Problems und kann für bestimmte Szenarien optimiert werden, in denen die Leistung wichtig ist.
gemessen ich die Leistung für folgende Strecken: (sample = 0,2, Toleranz = 0)
<Path Data="M 217,172 L 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300, 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300, 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300, 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300"></Path>
<Path Data="M 217,172 L 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20"></Path>
<Path Data="M 217,172 L 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300, 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300, 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300, 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300"></Path>
<Path Data="M 217,172 L 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300, 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300, 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300, 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300"></Path>
<Path Data="M 217,172 L 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300, 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300, 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300, 10 50, 50 30, 70 40, 100 300 10 50, 50 30, 70 40, 100 300"></Path>
<Path Data="M 217,172 L 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20, 20 20"></Path>
Auf einem i7 4790 bekam ich diese Zeiten:
80µs - False
390µs - True
86µs - False
82µs - False
69µs - False
355µs - True
Zu wenig Kontext. Woher kommen Ihre 'StreamGeometry' Objekte? Warum weißt du nicht schon welche Linien? Warum ist es wichtig, dass Sie wissen, welche Linien sind? Sie scheinen das [XY-Problem] zu haben (http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) ...Was versuchst du eigentlich zu tun, und wie hast du dich in eine Situation gebracht, in der du Informationen brauchst, die das Objekt normalerweise nicht bietet? Bearbeiten Sie Ihre Frage so, dass sie eine gute [mcve] enthält, die den Kontext anzeigt und _speziell_ erklärt, was Sie versucht haben und warum das nicht funktioniert hat. –
Ich bin wirklich hart gedrängt, irgendwelche Fragen zu finden, die @PeterDuniho nicht ausgelöst hat, außer vielleicht, was deine Lieblingsfarbe ist ... – Noctis
Meine Lieblingsfarbe ist transparent. Kannst du mir jetzt helfen? –