I am trying to solve this little puzzle (the algorithm): I have N image icons and I want to distribute them evenly on users screen. Say, I put them in a table. If there is one image, there will be one cell in a table. If two - one row with two columns, if three - one row and three columns, if four - two rows, two columns... and so on until row space is gone and since then the table should only grow in columns without adding extra rows.
I'm trying to figure an algorithm for this and perhaps this is something that has a solution already somewhere?
My attempt is so far something like this:
obtain_max_rows()
obtain_visible_columns()
if (number_of_pictures > max_rows*max_columns)
{
columns = roundup(number_of_pictures/max_rows)
for(max_rows){generate row;for columns{generate column}}
}
else
{
**here comes to trouble...**
}
This logic is bit silly though - it somehow needs to think cases where there are 12 pictures on first screen and 2 on the other trying to balance it say 8/6 or somehow like that.
I think I found your answer:
typedef struct{int width, height;} rectangle;
...
rectangle findOptimalDivision(int numberOfCuts){
int x = numberOfCuts, y = (int)sqrt(numberOfCuts);
while (x%y) { // when y = 1 it will always exit out (anything can be divided by 1)
y--;
}
return (rectangle){y,numberOfCuts/y};
}
This should give you the height and width of the table (or rectangle) in terms of cells.
I tested this with 1 - 100 as the parameter and it seems to give the proper outputs.
Message me if you have any problems.
Related
I want to show the most recent 10 bins for box plot.
If a filter is applied to the bar chart or line chart, the box plot should show the most recent 10 records according to those filters.
I made dimension by date(ordinal). But I am unable to get the result.
I didn’t get how to do it with a fake group. I am new to dc.js.
The pic of scenario is attached. Let me know if anyone need more detail to help me.
in image i tried some solution by time scale.
You can do this with two fake groups, one to remove the empty box plots, and one to take the last N elements of the resulting data.
Removing empty box plots:
function remove_empty_array_bins(group) {
return {
all: function() {
return group.all().filter(d => d.value.length);
}
};
}
This just filters the bins, removing any where the .value array is of length 0.
Taking the last N elements:
function cap_group(group, N) {
return {
all: function() {
var all = group.all();
return all.slice(all.length - N);
}
};
}
This is essentially what the cap mixin does, except without creating a bin for "others" (which is somewhat tricky).
We fetch the data from the original group, see how long it is, and then slice that array from all.length - N to the end.
Chain these fake together when passing them to the chart:
chart
.group(cap_group(remove_empty_array_bins(closeGroup), 5))
I'm using 5 instead of 10 because I have a smaller data set to work with.
Demo fiddle.
This example uses a "real" time scale rather than ordinal dates. There are a few ways to do ordinal dates, but if your group is still sorted from low to high dates, this should still work.
If not, you'll have to edit your question to include an example of the code you are using to generate the ordinal date group.
I have one json example below:
{"Year":2018,"Month":1,"ApplicationName":"application1","ASI":12.0,"AEI":11.0},
{"Year":2018,"Month":2,"ApplicationName":"application2","ASI":24.0,"AEI":12.0}
I want to show a ring chart with two slices:
total of ASI
total of AEI
How can I get crossfilter to produce the two bins for the two columns?
The accessor used by reduceSum is a general function; you can put anything you want in there.
Thus,
group.reduceSum(function(d) { return d.ASI + d.AEI; });
will instruct the group to sum up all the ASIs and all the AEIs from the rows which fall into each bin.
I have a case where I have to add figures in another figure, i.e., nested figures.
My figure which will contain other figure extends org.eclipse.draw2d.Figure.
I want to change the height of this figure when it contains more than 4 figures, so that all the nested figures are visible at once.
So, in the paintFigure(Graphics graphics) method, I do this:
Rectangle r = getBounds();
if( getChildren().size() > 4 ) {
setBounds(new Rectangle(r.x, r.y, r.width, r.height + getChildren().size()-4)*10));
}
But this doesn't change the height. Well it does change it, for the time being, but when it is called again the next time, it again has the previous height, not the updated one.
Hence, visually the figure height doesn't change at all.
Any ideas on what am I doing wrong?
Or to do this in a different and/or better way?
I was updating the height in the paintFigure(), it was changing to previous height because it was taking values from the model. I changed the height in the model, and it worked fine.
I'm starting to go into random world generating, I have an idea on how random number generating works (Actually pseudorandom numbers), but I don't have a good idea of how to make the world look "nice", in other words not just place blocks based on a random x, y that it gives me, but make it look smooth.
This will be a 1 time generation per world. So everything is created at start.
I was thinking of an algorithm a few moments ago, but the problem is that it would just use be an endless amount of nested if loops, which would probably take a more than the necessary time. I was thinking of the following:
Choose a random location on the map and place the spawn point in that location.
Start building the street based on the spawn location, like if the spawn location is 16
spaces near the edge of the world build a house, otherwise start building a street.
Based on the previously generated street's place structures around.
Place misc.
Conceptualizing the algorithm isn't much of a problem, what I'm having difficulty with is starting the actual code from step 2 and below. Based on the above algorithm or an algorithm you think of, how would you start the code? I'm not asking for the actual code to be made, just an idea of how it would look.
I know this question isn't precise and can have multiple answers, but I've seen many questions similar to this one having a strange approach.
hmm looks like planar(or cubic) map filling. from my point of view firstly you need some databases
struct _object
{
string name,help,info; // texts for later use
int siz[3]; // grid size of object
int pos[3]; // placement pos (center or what ever)
// other stuff like: mesh-id,color,hit points,...
};
struct _dependency
{
int objid
List<int> near; // can be near this objects (can add probability)
List<int> far; // cannot be near this objects (can add probability,min distance)
};
List<_object> object; // DBS of all object types
List<_dependency> depend; // DBS of object dependency
Then you need to initialize this DBS from ini files or whatever. After that you need to create world map. For simplicity let it by only a single squared town and single layer(no sub-terrain), size and placement can be random.
List<_object> obj; // DBS of placed objects, could be lesser derivate of previous _object to conserve memory requirements
const int N=100;
int[N][N] map; // placement map, containing placed obj id, or -1 for empty space
so now you need some town generating function that fills map[N][N]:
void genere()
{
int i,j,x,y,xx,yy,objid,objix;
int _min_complexity=N/10; // this can also be random
int _max_complexity=N; // this can also be random
// clear map
for (i=0;i<N;i++)
for (j=0;j<N;j++)
map[i][j]=-1;
int complexity=_min_complexity+random(_max_complexity-_min_complexity);
for (i=0;i<complexity;)
{
// random placenet position
x=random(N);
y=random(N);
// random object, should take in mind object[].near and closest objects in map[y][x]
objid=random(object.num);
if (check if map[][] is empty enough to place object[objid] to x,y,z)
if (check if near x,y position is not bad type of object already object[].far)
{
// add new object to list
objix=obj.add(object[objid]);
// add new object to map
int *siz=obj[objix].siz
int *pos=obj[objix].pos
x+=pos[0];
y+=pos[1];
for (yy=0;yy<siz[1];yy++)
for (xx=0;xx<siz[0];xx++)
map[y+yy][x+xx]=objid;
i++;
}
}
}
also the position can be double[3] + orientation matrix and map coordinates will than be aligned to grid. There are many ways to tweak this code, its just an starting template. Hope it helps a little.
P.S.
List<type> l;
l.num - number of items in list
l[i] - i item from list
i=l.add(a) - add new item a to list and returns its index
I'm writing a table of contents to a standard 8.5in x 11in page. Orientation (landscape versus portrait) is a variable. I can write my TOC to an inner region when x-inch margins are applied to the page (where x is variable).
The raw data is a table with two columns: Topic and Page (i.e. { "Animals" , 1 } , { "Big Plants" , 2 } , { "Small Plants" , 2 } ). This is not a nested TOC - there are no "subtopics". All topics are at the same level of importance and font size is fixed for all text.
I want to allow for 1 or more columns of TOC per page and I'm allowing multiple pages of TOC if needed. The layout is completely dependent on text in the Topic column. If the topics are short, you can imagine putting 2 columns in portrait orientation, or 3 columns in landscape orentation. If there are long topic names, then maybe only 1 column will fit (if very long, then its ok to use multiple lines for a topic). If there are many topics, then I might spill over to multiple pages. The goal is to put as much TOC info as possible on each page.
I realize this is a hard problem. There's a number of details that I haven't explored (i.e. do all pages have to have the same number of columns?). I'm just looking for a start...something simple enough to implement in an hour or two that does the job. Anything semi-intelligent is better than forcing a 1-column TOC with character counts to determine how many rows to place on a page.
First you need a few variables:
Line_hight (inches per TOC line)
Max_TOC_width (inches of the longest TOC name)
Max_pagenum_width (inches width of the largest page number when printed)
Left, Right, Top, Bottom _border (inches of the border around the page)
Then it's pretty easy to calculated.
Lines_Per_Page = Floor( (Page_Height - Top_Border - Bottom_Border) / Line_Height )
Columns_Per_Page = Floor( (Page_Width - Left_Border - Right_Border) / (Max_TOC_Width + Max_PageNum_Width) )
Total_TOC_Per_Page = Lines_Per_Page * Columns_Per_Page
FYI: Floor( ) means round down to the nearest integer. Floor(5.9) = 5, Floor(0.1) = 0
Assuming you want all columns to be the same width:
Do one pass through the table finding the (printed) width of the longest TOC entry.
Divide the page width minus margins and take the floor to figure out how many columns you can fit.
Divide the page height minus margins by the height of a line and take the floor to figure out how many lines per column.
Repeat steps 2 and 3 in the other page orientation (e.g., landscape).
Choose the one that gives the most slots (rows times columns).
The math is slightly more complicated, since you need to account for a "gutter" between the columns. This is easily accomplished by padding your widest value.