algorithm

How pick colors for a pie-chart?

I have some code that generates image of a pie chart. It's a general purpose class, so any number of slices can be given as input. Now I have problem picking good colors for the slices. Is there some algorithm that is good at that? Or maybe I should just hand-pick and list fixed colors? But how many. Maybe 10 colors and hope there will ...

Tickmark algorithm for a graph axis

I'm looking for an algorithm that places tick marks on an axis, given a range to display, a width to display it in, and a function to measure a string width for a tick mark. For example, given that I need to display between 1e-6 and 5e-6 and a width to display in pixels, the algorithm would determine that I should put tickmarks (for exa...

What is the optimal algorithm design for a water-saving urinal?

At work, we have one of those nasty communal urinals. There is no flush handle. Rather, it has a motion sensor that sometimes triggers when you stand in front of it and sometimes doesn't. When it triggers, a tank fills, which when full is used to flush the urinal. In my many trips before this nastraption, I have pondered both what the a...

How is the photoshop cutout filter implemented?

Photoshop has a lot of cool artistic filters, and I'd love to understand the underlying algorithms. One algorithm that's particularly interesting is the Cutout filter (number 2 at the link above). It has three tunable parameters, Number of Levels, Edge Simplicity, and Edge Fidelity. Number of levels appears to drive a straightforward ...

Is it possible to write a function like next_permutation but that only permutes r values, instead of n?

std::next_permutation (and std::prev_permutation) permute all values in the range [first, last) given for a total of n! permutations (assuming that all elements are unique). is it possible to write a function like this: template<class Iter> bool next_permutation(Iter first, Iter last, Iter choice_last); That permutes the elements in ...

Algorithm/pattern for selecting sub-collections using LINQ and C#

I have a C# collection of strings. Each string is a sentence that can appear on a page. I also have a collection of page breaks which is a collection of int's. representing the index where the collection of strings are split to a new page. Example: Each 10 items in the string collection is a page so the collection of page breaks would b...

Pseudo Random Generator with same output

I came across an article about Car remote entry system at http://auto.howstuffworks.com/remote-entry2.htm In the third bullet, author says, Both the transmitter and the receiver use the same pseudo-random number generator. When the transmitter sends a 40-bit code, it uses the pseudo-random number generator to pick a new code, which i...

random number with ratio 1:2

I have to generate two random sets of matrices Each containing 3 digit numbers ranging from 2 - 10 like that matrix 1: 994,878,129,121 matrix 2: 272,794,378,212 the numbers in both matrices have to be greater then 100 and less then 999 BUT the mean for both matrices has to be in the ratio of 1:2 or 2:3 what ever c...

Random points inside a Polygon

I have a 4 side convex Polygon defined by 4 points in 2D, and I want to be able to generate random points inside it. If it really simplifies the problem, I can limit the polygon to a parallelogram, but a more general answer is prefered. Generating random points until one is inside the polygon wouldn't work because it's really unpredict...

Meta-composition during music performances

A couple of weeks ago, my piano teacher and I were bouncing ideas off of each other concerning meta-composing music software. The idea was this: There is a system taking midi input from a bunch of instruments, and pushes output to the speakers and lights. The software running on this system analyzes the midi data it's getting, and deter...

Sort Four Points in Clockwise Order

Four 2D points in an array. I need to sort them in clockwise order. I think it can be done with just one swap operation but I have not been able to put this down formally. Edit: The four points are a convex polygon in my case. Edit: The four points are the vertices of a convex polygon. They need not be in order. ...

Algorithm: Voyage planning

I need to plan a voyage connecting n locations in the sea with a specified origin and specified destination with following constraints. The voyage has to touch all locations. If there is a reservation from A to B then a has to be touched before B The time spend at each location varies (depends upon the reservations to that location) Eac...

Math: Factorisation

Omitting details of methods to calculate primes, and methods of factorisation. Why bother to factorise ? What are its applications ? ...

algorithm for index numbers of triangular matrix coefficients

Hi all, I think this must be simple but I can't get it right... I have an MxM triangular matrix, the coefficients of which are stored in a vector, row by row. For example: M = [ m00 m01 m02 m03 ] [ m11 m12 m12 ] [ m22 m23 ] [ m33 ] is stored as coef[ m00 m01 m02 m03 m11 m12 m13 m22 m23...

A good reference card / cheat sheet with the basic sort algorithms in C?

I've been looking (without great luck) for the perfect reference card with all the basic sorting algos in C (or maybe in pseudo code). Wikipedia is a terrific source of info but this time I'm looking for something definitely more portable (pocket size if possible) and of course printable. Any suggestion would be much appreciated! ...

What is an Efficient algorithm to find Area of Overlapping Rectangles

My situation Input: a set of rectangles each rect is comprised of 4 doubles like this: (x0,y0,x1,y1) they are not "rotated" at any angle, all they are "normal" rectangles that go "up/down" and "left/right" with respect to the screen they are randomly placed - they may be touching at the edges, overlapping , or not have any contact I w...

How do I get an array of repeated characters from a string using LINQ?

If I have the following string: string s = "abcdefghab"; Then how do I get a string (or char[]) that has just the characters that are repeated in the original string using C# and LINQ. In my example I want to end up with "ab". Although not necessary, I was trying to do this in a single line of LINQ and had so far come up with: s.ToC...

Algorithm to tell if two arrays have identical members.

What's the best algorithm for comparing two arrays to see if they have the same members? Assume there are no duplicates, the members can be in any order, and that neither is sorted. compare( [a, b, c, d], [b, a, d, c] ) ==> true compare( [a, b, e], [a, b, c] ) ==> false compare( [a, b, c], [a, b] ) ==> false ...

Algorithm to find if two sets intersect

Let's say I have two arrays: int ArrayA[] = {5, 17, 150, 230, 285}; int ArrayB[] = {7, 11, 57, 110, 230, 250}; Both arrays are sorted and can be any size. I am looking for an efficient algorithm to find if the arrays contain any duplicated elements between them. I just want a true/false answer, I don't care which element is sh...

Adjacent number algorithm grouper

By which I mean this: Given the input set of numbers: 1,2,3,4,5 becomes "1-5". 1,2,3,5,7,9,10,11,12,14 becomes "1-3, 5, 7, 9-12, 14" This is the best I managed to come up with: [C#] Which feels a little sloppy to me, so the question is, is there somehow more readable and/or elegant solution to this? public static string[] FormatIn...