How to know a StreamGeometry is a Line [closed] - c#
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a collection of StreamGeometrys. Each StreamGeometry could be any shape. I just want to pick up the StreamGeometrys that are lines. How can I know a StreamGeometry is a line.
Background:
I get collection of StreamGeometrys from an interface. The interface is given by other team and doesn't return the information that what the Geometry is. (I may discuss with them to make an update, but it involve the architecture change.) I have a function to group the Geometrys and get the outline of the group. Originally, I add them to a GeometryGroup, then I use Geometry.GetOutlinedPathGeometry to get the outline of the GeometryGroup. The behavior is good.
Problem:But when the Geometrys number increases to more than 200, GetOutlinedPathGeometry becomes extremely slow.
Current solution:Then I have to make a compromise to get an outline not that accurate. Instead of GetOutlinedPathGeometry, I use Geometry.Combine to combine the Geometrys. Geometry.Combine is mainly used for closed Geometry. When a Geometry is not closed, it will find the nearest points to make it closed. This behavior somehow reduce the complexity of the overall outline that GetOutlinedPathGeometry can get a result quicker.
How I get to this question:But one problem is that Geometry.Combine will omit Line Shapes. After combine all Geometrys, Lines are not in the group. So I want to find those lines and use GetOutlinedPathGeometry to get their outlines first. Then use their outlines to combine with others.
Also, even I have the Geometry type information, it could not help too much. Because Geometry.Combine will omit any Line Shapes that can be made of any Geometry type. Line, Path even Rectangle can all make a Line shape.
Other attempts:I tried to use GetOutlinedPathGeometry to get outline for each geometry first, then to combine. The performance improves a little bit but still very slow.
Just drop this code in and you can use StreamGeometry.IsLine() to check for lines.
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;
}
}
The code "samples" the path every X% (example 0.2 = 20%) and checks if the origin, the end and the current sample point are on a line.
The code is a very general solution to the problem and can definetly be optimized for specific scenarios where performance is important.
I measured the performance for following Paths: (sampleRate = 0.2, tolerance = 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>
On an i7 4790 i got these times:
80µs - False
390µs - True
86µs - False
82µs - False
69µs - False
355µs - True
Related
c# convert byte array of ascii values to integer array
I have a byte array of 50 bytes representing 5 integers as ascii characters values. Every integer value is represented as 10 bytes: byte[] receiveBytes = new byte[] { 20, 20, 20, 20, 20, 20, 20, 20, 20, 49, // 9 spaces then '1' 20, 20, 20, 20, 20, 20, 20, 20, 20, 50, // 9 spaces then '2' 20, 20, 20, 20, 20, 20, 49, 50, 51, 52, // 6 spaces then '1' '2' '3' '4' 20, 20, 20, 20, 20, 20, 53, 56, 48, 49, // 6 spaces then '5' '8' '0' '1' 20, 20, 20, 20, 20, 20, 20, 57, 57, 57}; // 7 spaces then '9' '9' '9' Please, notice that 20 is an ascii code of space and [48..57] are ascii codes of 0..9 digits. How can I convert the byte array to an integer array (int[] intvalues == [1, 2, 1234, 5801, 999])? I have tried first to convert byte array to string and then string to integer like this: string[] asciival = new string[10]; int[] intvalues = new int[5]; Byte[] receiveBytes = '20202020202020202049 //int value = 1 20202020202020202050 //int value = 2 20202020202049505152 //int value = 1234 20202020202053564849 //int value =5801 20202020202020575757';//int value = 999 asciival[0] = Encoding.ASCII.GetString(receiveBytes, 0, 10); asciival[1] = Encoding.ASCII.GetString(receiveBytes, 10, 10); intvalues[0] = int.Parse(asciival[0]); intvalues[1] = int.Parse(asciival[1]); But isn't there a simpler way to copy the byte array into the string array?
A for loop can simplify the writing: byte[] recv = new byte[]{ /* ... */ } int[] intvalues = new int[recv.Length / 10]; for(int pos = 0; pos < recv.Length; pos += 10) intvalues[pos / 10] = int.Parse(Encoding.ASCII.GetString(recv, pos, pos + 10));
I suggest using Linq: Split initial array on 10-items (i.e. 10-byte) chunks Filter digits ('0'..'9' ) in each chunk Aggergate digits into a single integer Implementation: using System.Linq; ... Byte[] receiveBytes = new byte[] { 20, 20, 20, 20, 20, 20, 20, 20, 20, 49, // 9 spaces then '1' 20, 20, 20, 20, 20, 20, 20, 20, 20, 50, // 9 spaces then '2' 20, 20, 20, 20, 20, 20, 49, 50, 51, 52, // 6 spaces then '1' '2' '3' '4' 20, 20, 20, 20, 20, 20, 53, 56, 48, 49, // 6 spaces then '5' '8' '0' '1' 20, 20, 20, 20, 20, 20, 20, 57, 57, 57}; // 7 spaces then '9' '9' '9' int[] intvalues = Enumerable.Range(0, receiveBytes.Length / 10) .Select(index => receiveBytes .Skip(index * 10) // Skip + Take: splitting on 10-items chunks .Take(10) .Where(b => b >= '0' && b <= '9') // just digits .Aggregate(0, (s, a) => s * 10 + a - '0')) .ToArray(); Test Console.Write(string.Join(", ", intvalues)); Outcome: 1, 2, 1234, 5801, 999 Please, notice, that 10 digits number can well overflow int since maximum int value (int.MaxValue) is 2147483647 only. To represent the initial byte[] as a string you can use Linq once again: var result = Enumerable .Range(0, receiveBytes.Length / 10) .Select(index => receiveBytes .Skip(index * 10) // Skip + Take: splitting on 10-items chunks .Take(10) .Select(b => b.ToString("00"))) // enforce leading "0" if necessary .Select(items => string.Concat(items)); string text = string.Join(Environment.NewLine, result); Console.Write(text); Outcome 20202020202020202049 20202020202020202050 20202020202049505152 20202020202053564849 20202020202020575757
You can try this :- using System; using System.Text; class Example { public static void Main() { // Define a string. String original = "ASCII Encoding"; // Instantiate an ASCII encoding object. ASCIIEncoding ascii = new ASCIIEncoding(); // Create an ASCII byte array. Byte[] bytes = ascii.GetBytes(original); // Display encoded bytes. Console.Write("Encoded bytes (in hex): "); foreach (var value in bytes) Console.Write("{0:X2} ", value); Console.WriteLine(); // Decode the bytes and display the resulting Unicode string. String decoded = ascii.GetString(bytes); Console.WriteLine("Decoded string: '{0}'", decoded); } }
Print an array as hex values in c#
I want to print an array as hex. In the following code 'buffer' has been filled by 'XBaseRead'. I can print in hex using a foreach loop and the format "{0:X2}" but when I use string.join method with the same format it prints in decimal. Here is the code: byte[] buffer = new byte[160]; XBaseFunctions.XBaseRead(df2500VENDR, buffer, 160, false, XBaseFunctions.O_FLAG._O_BINARY); foreach (byte i in buffer) { Console.Write("{0:X2} ", i); } Console.WriteLine("\n"); Console.WriteLine("{0:X2}", string.Join(", ", buffer)); Here is the output: -----------------Dump of raw buffer----------------- F0 F0 F0 F0 C1 F0 F1 F0 C1 40 C1 D5 E3 C8 D6 D5 E8 40 50 40 E2 D6 D5 E2 40 C9 D5 C3 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 F1 F4 F5 F0 40 E 6 40 F2 F1 E2 E3 40 E2 E3 5C C5 D9 C9 C5 6B 40 D7 C1 40 40 40 F1 F6 F5 F0 F2 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 40 00 00 00 00 00 0 0 00 00 00 00 00 00 F9 60 03 A7 7E 3E 7E 3F 00 00 00 00 00 00 00 00 40 40 40 40 240, 240, 240, 240, 193, 240, 241, 240, 193, 64, 193, 213, 227, 200, 214, 213, 2 32, 64, 80, 64, 226, 214, 213, 226, 64, 201, 213, 195, 64, 64, 64, 64, 64, 64, 6 4, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 241, 244, 245, 240, 64, 2 30, 64, 242, 241, 226, 227, 64, 226, 227, 92, 197, 217, 201, 197, 107, 64, 215, 193, 64, 64, 64, 241, 246, 245, 240, 242, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 , 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 , 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 249, 96, 3, 167, 126, 62, 126, 63, 0, 0, 0, 0, 0, 0, 0, 0, 64, 64, 64, 64 So the question is Why does the {0:X2} work for the foreach loop but no for the string.join method? Also, if someone could tell me how to limit each line to 16 bytes then I would appreciate that information too.
You should apply ToString("X2") on every byte first : Console.WriteLine(string.Join(", ", buffer.Select(b => b.ToString("X2"));
Because when you're doing a foreach loop, you're passing a byte to the formatted Console.WriteLine(). However, you're passing a string of the entire joined buffer in the other instance, as string.Join(", ", buffer) returns a string. foreach (byte i in buffer) { Console.Write("{0:X2} ", i); // <- A byte is being passed. } Console.WriteLine("{0:X2}", string.Join(", ", buffer)); // <- A string is being passed. I would do use this: var str = string.Join(", ", buffer.Select(x => string.Format("{0:X2}", x))); Console.WriteLine(str); ...or this: var str = string.Concat(buffer.Select(i => string.Format("{0:X2}, ", i))) Console.WriteLine(str); Although the first one doesn't give a trailing comma, so I would use that over the second method. Thought I'd share the different ways of accomplishing it, though. Hopefully you're familiar with Linq, so I'm not going to go into great detail as far as what's going on, as it's pretty self-explanatory if you understand Linq.
Printing 100 to 1 ?How?
print 10 number on each line , Below Is The Code Of What I Did But Still Not Successful , Help namespace ConsoleApplication2 { class Program { static void Main(string[] args) { int i; for (i =100; i>=1; i--) { Console.Write(i); } Console.ReadLine(); } } }
Try this: for (i = 100; i >=1; i--) { if(i%10==0) //if 10 numbers are printed Console.WriteLine(); //then line break Console.Write(i+" "); //print the number with a space character } Console.ReadLine(); It will print 10 number in each line. Result: 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 See result in ideone.
Try this static void Main(string[] args) { int i=100; for (i; i <=100; i--) { Console.Write(i); if(i==0) { break; } } Console.ReadLine(); } Edit Must use if(i==0){break;} otherwise this for loop does not end.
static void Main(string[] args) { for (int i = 100; i >0; i--) { if (i % 10 == 0) Console.WriteLine(); Console.Write(i); } Console.ReadLine(); }
This might do what you need: Enumerable.Range(1, 100).Reverse() .ToList() .ForEach(i => Console.Write(i % 10 == 1 ? i + "\r\n" : i + ", ")); Which will output: 100, 99, 98, 97, 96, 95, 94, 93, 92, 91 90, 89, 88, 87, 86, 85, 84, 83, 82, 81 80, 79, 78, 77, 76, 75, 74, 73, 72, 71 70, 69, 68, 67, 66, 65, 64, 63, 62, 61 60, 59, 58, 57, 56, 55, 54, 53, 52, 51 50, 49, 48, 47, 46, 45, 44, 43, 42, 41 40, 39, 38, 37, 36, 35, 34, 33, 32, 31 30, 29, 28, 27, 26, 25, 24, 23, 22, 21 20, 19, 18, 17, 16, 15, 14, 13, 12, 11 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
using System; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { int i; for (i = 100; i > 0; i--) { if(i%10==0)Console.WriteLine(); Console.Write(i); } Console.ReadLine(); } } } Updated Demo:http://ideone.com/st4i8n
Enumerable.Range(0, 10).Reverse().ToList().ForEach(n => { Enumerable.Range(n * 10, 10).Select(i => i + 1).Reverse().ToList().ForEach(i => Console.Write(i + " ")); Console.WriteLine(); }); // output: 100 99 98 97 96 95 94 93 92 91 // 90 89 88 87 86 85 84 83 82 81 // 80 79 78 77 76 75 74 73 72 71 // 70 69 68 67 66 65 64 63 62 61 // 60 59 58 57 56 55 54 53 52 51 // 50 49 48 47 46 45 44 43 42 41 // 40 39 38 37 36 35 34 33 32 31 // 30 29 28 27 26 25 24 23 22 21 // 20 19 18 17 16 15 14 13 12 11 // 10 9 8 7 6 5 4 3 2 1
HundredToOne() { for(int i=100;i>0;i--) { if(i%10 == 0) Console.WriteLine("\n"); Console.Write(i); } }
You can also use a StringBuilder to build the 10 numbers per line, but concatenating like so should yield sufficient results (since it's only 100 numbers) foreach (var number in Enumerable.Range(1, 100).ToArray().Reverse()) { Console.Write((number % 10 == 0) ? "\n" : (number.ToString() + " ")); }
Select first k elements in C# LINQ such that the sum of elements is less than S [duplicate]
This question already has answers here: Linq: How to query items from a collection until the sum reaches a certain value (2 answers) Closed 9 years ago. I have an array of numbers and a variable S. I want to select first k elements out of them using LINQ in C# such that the sum of k elements is less than S. For example: int[] Numbers = { 1, 4, 53, 23, 15, 12, 15, 25, 45, 13, 16, 76, 43, 82, 24 }; int S = 100; The result would be an array: {1, 4, 53, 23, 15}
Take a look at TakeWhile: int[] Numbers = { 1, 4, 53, 23, 15, 12, 15, 25, 45, 13, 16, 76, 43, 82, 24 }; int total = 0; var result = Numbers.TakeWhile(i => { total += i; return total < s });
Graphing (even with consecutive values)
I have this code: var temp = allsuminterestarray.OrderBy(i => int.Parse(i)).GroupBy(i => i); int[] forvaluescount = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50}; int j = 0; foreach (var val in temp) { Console.WriteLine(val.Key + " " + val.Count()); while ((forvaluescount[j] < Convert.ToInt32(val.Key)) && (forvaluescount[j] != Convert.ToInt32(val.Key))) { probabilities.Series["Series1"].Points.AddXY(forvaluescount[j], 0/trials); j++; } j++; probabilities.Series["Series1"].Points.AddXY(val.Key, val.Count()/trials); } NOTE: temp has pair values. For example: (11, 1), (14, 1), (18, 1), (19, 3), (20, 1), (21, 3), (22, 1), (24, 3), (27, 1), (29, 2), (30, 1), (31, 2) as X and Y values for a chart in C#. Basically what happens here is that say i have a value of 11 for val.Key. The code should work such that it will assign to a CHART values of 1-10 as X and 0 as Y. Then 11 as X with its corresponding Y value. The loop will continue such that there will be no missing values in between the values of temp. I believe there's nothing wrong with the code as it output consecutive values. The problem here is that even if i do the plotting of X and Y in order i get a graph like this: I was expecting a similar image to this (this was generated only with the values of temp: However there should be no missing plots of (X,0). Please help and thank you!