Permutations/Combinations of bounded numbers in C# [duplicate] - c#

This question already has answers here:
Listing all permutations of a string/integer
(28 answers)
How can I create cartesian product of vector of vectors?
(9 answers)
Closed 8 years ago.
Here's something which has been bugging me for the past two days.
I need to populate an initial configuration(/state)-space for a fixpoint algorithm.
In this statespace, each transition weight has a vector of weights, and different bounds may apply to each of the weights in this vector.
This is currently defined as an example transition weight being for example (5,-1,-1)
The bounds for each weight correspond to the index of the weight vector itself, for example the upper bounds for these weights, assuming the lower bound is 0 for all is given by (5,3,3)
Now, to set up the initial configuration space, i need to have every combination of weights available in the beginning.
(0,0,0) (0,0,1) (0,1,0) (1,0,0)... and so on, each of them going to their max bounds.
Now, if i was dealing with a 3-weighted system this would be trivial, but i need to support n-dimensional vectors in my code.
So, any ideas as to how i would accomplish populating this configuration space? (I'm using C# currently)

Here's the code for generating all ntuples implemented in Javascript. It's self-explanatory, but if you need further explanation, I'd be glad to help (I actually tried to write the algorithm in pseudocode but what I wrote ended up looking like the comments)

Related

Multithreading and get X sub matrixes [closed]

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 months ago.
Improve this question
I'm trying to get into multithreading by trying to do matrix multiplication and my problem is, how I would get all sub matrixes from a matrix.
My matrix variable is a int[,].
Example, if I have a matrix by 100 x 100, how would i get 10 of 10 x 10 sub matrix. And is it possible that user can choose to how many equal parts to cut up the matrix even if I the matrix is not a square ex. 400 x 300?
Is it even the right way to do it, by calculate on the sub matrixes and then add them together when done?
how would i get 10 of 10 x 10 sub matrix
You would do a double loop, copying each value from the original matrix to the new sub matrix.
Is it even the right way to do it, by calculate on the sub matrixs and then add them together when done?
The normal way to multiply matrices is with a triple loop, as shown in this answer. It should be fairly trivial to convert the outer loop to a parallel.For loop, since all calculations are independent from each other. This avoids any need to process individual sub matrices, and let the framework deal with partitioning the work.
However, things like this is typically fairly cache sensitive. A matrix will be stored in memory as sequential values, either row or column major. Accessing sequential values will be very cache friendly, but accessing non sequential values will not be. So you might want to copy a full row/column to a temporary array to ensure all subsequent accesses are sequential. If using a parallel loop you should probably use one of the overloads that give you a thread local array to use. There more things one can do with cache optimizations, and SIMD intrinstics but that is probably best left as a later exercise.
There are algorithms with a lower algorithmic complexity that does work on submatrices, but in my experience it will be fairly tricky to make this actually faster in c# than a cache-optimized triple loop.
Keep in mind to measure the performance of your method. I would also suggest comparing your performance with some well optimized existing library to get some sense of how performant your implementation is.

C# winforms - How can I get all the points on the Bezier curve or the equation drawn with DrawBeziers? [duplicate]

This question already has answers here:
How can I tell if a point is on a quadratic Bézier curve within a given tolerance?
(3 answers)
Closed 7 months ago.
I'm doing some computer graphics program. I need to draw a bezier curve and then determine if a point is on this curve. I've defined 4 points(2 endpoints, 2 control points) and plotted a bezier curve using DrawBeziers. So how can I determine if a point is on the drawn curve?
Should I get all the points on the Bezier curve and check if a point in all points of the curve?
Should I get the equation of the curve and check a point can make the
equation true?
Should I use DrawBeziers method to draw it?
How can I implement this feature?
I'm looking forward to everyone's answers, it would be better if the answers could be explained in detail. Thanks in advance.
I would assume that the goal is to check if the user clicked on the curve or not. The mathematical curve will be infinitely thin, so what you really are after is to compute the distance to the curve. The link provides the math. Once you have the distance, just check if it is lower than the line thickness.
A potentially more accurate way would be to draw the curve, and any other graphics to a internal bitmap, using a unique color for each object, and without any anti-aliasing. That will give you an easy way to lookup the clicked object, while supporting overlapping shapes in a natural way. Also consider using a wider line-thickness when drawing to make it easier to select objects. But it will be somewhat costly to redraw the internal bitmap.
Another alternative would be to convert your spline into a polyline, and check the distance to each line segment. This is probably least accurate, but should be very simple to implement if you already types for line segments.

Measuring waist of a person using Microsoft Kinect [closed]

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 am trying to create an application which is able to accurately measure the body parameters of a person like height, shoulder width and waist.
Currently I have been able to determine the height and the shoulder width of a person using skeletal tracking.
Can anybody help me out regarding how to measure the waist of a person using a Kinect!
I am coding in C# in Visual Studio.
Thanks a lot in advance!
It is hard to give you the exact code, right now, but the recipe:
First you need to understand what it entails. Every person has different proportions. Someone has a wide waist, but fit (athletic), someone has a wide waist, but has also big belly (fat figure), another has a wasp waist. Such variations are many and many...
So, you have to shoot waist in time during rotation around its axis. Then the measured width values convert to a model. After that you will read circumference of the waist plan (like from a blueprint).
EDIT:
Detailed:
If a person turns around (you know it, because the waist witdh values changes...front-left-back-rigth-front and many samples between each part of rotation) gives you the measures in time for the pattern.
Split whole time of rotation to number of samples. Each sample will determine the proportional angle of the turn. (8 samples per rotation means one sample is 45° [360°/8=45°]). Now imagine the circle. Split it into 8 circle chords. Each chord have length of the value measured during the rotation.
If the sample count is good enough, now you can reckon the circumference of the polygon. If the count of samples is too low, you can interpolate (or use another solution) the "missing" samples. The more samples you have, the more accurate result you have.

Algorithm to create area filled points on Map [duplicate]

This question already has answers here:
Polygon enclosing a set of points
(4 answers)
Closed 7 years ago.
I have been working with a huge Map project in .NET. During working at that project, I encountered a problem. I have a plenty points with my coordinates (latitude,longitude), and I need to draw a polygon which could "fill" my points. My question is, how to choose points being outside to draw polygon by them. I attached a image file to clarify my problem -> http://postimg.org/image/fwsf0v285/
It sounds like you want a polygon that surrounds the data points. There are two algorithms available.
First is a "Convex Hull". This is a classic algorithm of computation geometry and will produce the polygon you have drawn. Imagine your data points are nails in a board, it produces a polygon resembling an elastic band that has been put around the outer data points. Convex Hulls are fast (or should be).
Convex Hulls really are common, I know I've coded them up to work on the Earth's surface (ie. to take into account the Earth's curvature). Here's a simpler Euclidean example (from stackoverflow) that should get you started:
Translating concave hull algorithm to c#
The alternative is an "Alpha Shape" which is sometimes known as a "Concave Hull". Basically it allows for concave (inward) curves in the side of the polygon that are larger than a specified dimension ('alpha'). For example a 'C' of data points should produce a 'C shape', whilst a convex hull would give you something more like an 'O' as will cut off the concave hollow.
Alpha shapes are much more involved as they require the calculation of a Delauney Triangulation.

Where can I find an algorithm for generating digital camouflage pattern? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 years ago.
Improve this question
Here is a sample of what I'm looking to reproduce.
I thought about tiling an image but that would create a detectable pattern.
I also thought about a random pattern using 4 or 5 colors but this is not a random pattern.
Thanks!
The military uses a fractal pattern called MARPAT, which I understand is highly effective compared to other known camo patterns. However, they have patented it, and I'm not aware of any way to find out the specifics.
Your best bet is probably Perlin noise, though I'm not sure how effective it would be as actual camouflage if you printed it out and tried to hide with it. You should be able to generate something that looks a lot like military camouflage, which is probably what you're trying to do.
What I would do:
choose a few colors for your camo
create a blank canvas
seed a number of points using your colors (change random pixels to a random color)
fill in the gaps using the algorithm below.
You want this to be random but you also want some clustering for those stripes/blobs in your example. So, by filling in the gaps from the seed points outwards, you can use the surrounding pixels to influence the color decision. If a pixel is surrounded by green, then it should be more likely to be green than yellow. So, for every pixel moving outwards from the seed points:
Consider your surrounding (8, 24, etc) pixels and use those to determine the chances for each color. Each color gets assigned a range of numbers between 0 and 1 (For example, green might be .23 - .57). The sum of the ranges should include all numbers between 0 and 1.
Use a random number generator to choose a number between 0 and 1. Whatever color range the number falls into is what color that pixel should be.
Find an adjacent blank pixel, repeat.
Totally untested, but it works in my head? Haha.
EDIT And if you'd like the larger boxes that digital camo actually has (as opposed to single pixels) group the pixels into groups of 9, 16, 25, 36, etc.
Try using Perlin Noise
Along the lines of Mr E's answer, if you want to go the procedural route, look at various fractal algorithms.
Here's a map generator that uses erosion, I could imagine you layering several of those and maybe skewing things horizontally for your image: http://forums.tigsource.com/index.php?topic=5174.180
There's all sorts of other pattern generators - simulating termites, forest fires, crystal growth, etc.
Here are some examples: http://neekatave.com/ca/examples/ffire/index.php
You will see links in there to Star Logo, which is an educational MIT project that has a bunch more examples (warning, you may lose a lot of time clicking around there :)

Categories