Get possible number of Desktop Icons - desktop

I've my screen resolution set to 1024 x 768 pixels and the icon size is 32x32 and default icon spacing (not changed). how can I calculate possible number of desktop icons that can fit into that resolution?

It's a little more complex actually and should be:
numColIcon = (Screen.Width-Icon.HorizontalSpacing) / (Icon.Width + Icon.HorizontalSpacing)
numRowIcon = (Screen.Height-Icon.VerticalSpacing) / (Icon.height + Icon.VerticalSpacing)
numTotalIcon = numColIcon * numRowIcon
You need to account for one more spacing which comes as the last "column" or "row". The number of padding rows and columns will always be N+1 where N is the number of object rows and columns.
The parentheses are important for proper calculation (Divide is higher precedence than minus)
In your example then:
numRowIcon = (768 - 43) / (32 + 43) will give 9 (rounded down or truncated)

Simple:
numColIcon = Screen.Width / (Icon.Width + Icon.HorizontalSpacing)
numRowIcon = Screen.height / (Icon.height + Icon.VerticalSpacing)
numTotalIcon = numColIcon * numRowIcon

Related

Convert decibel range to byte range

Greeting,
I'm trying to find a formula to convert a range where:
min = -100db and max = -30db
to:
min = 0 and max = 255
for example: -60db = ?
Seems easy but it makes my head spin.
Assuming you mean the result to stay in terms of db, you're asking for a simple linear interpolation:
f(x) = ymin + (x - xmin)*(ymax - ymin)/(xmax - xmin)
or in your case,
f(x) = 0 + (x + 100)*(255 - 0)/(-30 + 100)
f(-60 db) = 145.714
If instead you're talking about converting db to a scale factor with which to multiply an audio signal, then it's a bit more complex. For example, to multiply an audio signal by 0 is negative infinity db. So (at the very least) you'd have to special case that.

getting equivalent pixel of two different wide resolutions

I want to get the equivalent pixel location of two different wide resolutions.
Here is an example.
In a 1366x768 resolution, the desired pixel is located in row 120 and column 300.
I want to convert it to a lower resolution and get the equivalent of 120x300 point from the original to the converted one.
Use percent.
e.g. 120/1366=60/683=x ~ 0.0878 and 300/768=25/64=y ~ 0.3906. Now simply multiply these percent with your desired resolution.
For example if you have the resolution 800x600 and want this position just multiply.
x = 800 * 0.0878 = 70.24
y = 600 * 0.3906 = 234.36
This works because the position got kindof 'normalized' so that it lays between 0 and 1. Whatever you multiply this with will have same 'dimensions'. e.g. assume we want the position 400x300 from screen 800x600 in another screen so that it has same ratios. We can do similiar to your problem up there:
x = 400 / 800 = 0.5
y = 300 / 600 = 0.5
To get the position for any other screen we multiply the result from there with the resolution.
Percentage

Calculating proportion with negative float values

I'd like to know if there's any way in C++ to calculate a proportion involving possibily negative values in both vars and extremes.
My goal is to sync a float text input widget with fixed extremes ( eg the user can input any double value between A (min) and B (max) with A,B=any_constant_real_number ) with a slider who can only slide between 0 and 100 ( to simplify ).
If A and B are positive everything is trivial. as
val_slider = ((val_textin-A)*100)/(B-A)
but as A and B can be assumed real it looks to me the only possibility is to use several if/cases, or huge formulas involving a lot of abs() and checks over 0-divisions, whose are quite error prone and very cost intense compared to such an easy task.
Is there any faster and shorter way to achieve the same in c/c++/stl?
Pardon my bad english. Any hint? Thank you.
Your formula should work fine with negative values of A and B as well, as log as A < B.
Example, if you want the user to be able to enter values from -100 to 100, and map these to a slider which goes from 0 - 100, when the user enters -90 you get:
((-90 - A) * 100) / (B - A) = ((-90 - (-100)) * 100) / (100 - (-100))
= 10 * 100 / 200
= 5
An input value of 50 results in a slider value of:
((50 - A) * 100) / (B - A) = ((50 - (-100)) * 100) / (100 - (-100))
= 150 * 100 / 200
= 75
I don't know C++, but I do know Math, so try:
val_slider = 100 * ( val_textin - A ) / ( B - A )
Hey wait. That's exactly what you have. Test case..
A=-200, B=+200, val_texin = 100 (75% of bar, right?)
val_slider = 100 * ( 100 - -200 ) / ( 200 - - 200 )
= ( 300 ) / ( 400 ) * 100
= 75
See, you got it right. The only thing that COULD happen is B==A, but that can't be accounted for with math and requires a single IF. If they are equal, val_slider is exactly B (or A, as they are equal).

Finding size of perfect quad tree

I need to find the size of a perfect quad tree.
This means I have 1 root node that splits into 4 nodes that splits into 4 nodes etc.
so a quad tree of height 1 would be size 1
height 2 = size 5 (1 + 4)
height 3 = size 21 (1 + 4 + 16)
height 4 = size 85 (1 + 4 + 16 + 64)
etc..
I know that the size of a perfect binary tree can be found with: size = 2^(height+1)-1
So I believe that a similar equation exists for quad tree.
So what is it?
This is a geometric series. So the relevant formula is:
S = a * (1 - r^n) / (1 - r)
where a is the first value, r is the common ratio, n is the number of terms, and ^ denotes "to-the-power-of".
For a quad tree the algorithm is
((4^depth)-1)/3
For example with depth 3 you get
(64-1)/3 = 21
and if you count three layers you get
1 + 4 + 16 = 21
In my implementation I have even divided it into two arrays
where the size for all nodes that arent leave nodes is
((4^(depth-1))-1)/3
and leave nodes is
4^(depth-1)
I do these calculations at compile time with meta programming for pow, and a template argument for the depth. So i just allocate my nodes in two arrays.
Just in case anyone will need a code sample (in swift3)
public func tileCount(forLevelRange levelRange: Range<UInt>) -> UInt64 {
var tileCount: UInt64 = 0
for level in levelRange.lowerBound ..< levelRange.upperBound {
tileCount += UInt64(pow(Double(1 << level), 2) )
}
return tileCount
}

When drawing an ellipse or circle with OpenGL, how many vertices should we use?

Should we just blindly use 360 vertices? 720 seems to work better, but where do we stop?
It depends on how much error you can tolerate (i.e. the visual quality) and the size of the circle (ellipse). A bigger circle will need more points to achieve the same quality. You can work out exactly how many points you need for a given error with a bit of maths.
If you consider the circle represented by a series of line segments, the end points of the line segments lie exactly on the circle (ignoring the pixel grid). The biggest deviation between the real circle and our line segment representation occurs in the center of each line segment, and this error is the same for all of the line segments.
Looking at the first segment from the x-axis going anti-clockwise, its two endpoints are:
A = (r, 0)
B = (r . cos(th), r . sin(th))
where r is the radius of the circle and th is the angle covered by each line segment (e.g. if we have 720 points then each line segment covers 0.5 degree so th would be 0.5 degrees).
The midpoint of this line segment is at
M = A + (B - A) / 2
= (r, 0) + (r (cos(th) - 1) / 2, r . sin(th) / 2)
= (r / 2) . (1 + cos(th), sin(th))
and the distance from the origin to the point is
l = (r / 2) . sqrt((1 + cos(th))^2 + (sin(th))^2)
= (r / 2) . sqrt(2) . sqrt(1 + cos(th))
If our line segment representation were perfect then this length should be equal to the radius (the midpoint of the line segment should fall on the circle). Normally there'll be some error and this point will be slightly less than the radius. The error is
e = r - l
= r . (1 - sqrt(2) . sqrt(1 + cos(th)) / 2)
Rearranging so we have th in terms of e and r
2 . e / r = 2 - sqrt(2) . sqrt(1 + cos(th))
sqrt(2) . sqrt(1 + cos(th)) = 2 . (1 - e / r)
1 + cos(th) = 2 . (1 - e / r)^2
th = arccos(2 . (1 - e / r)^2 - 1)
This lets us calculate the maximum angle we can have between each point to achieve a certain error. For example, say we're drawing a circle with a radius of 100 pixels and we want a maximum error of 0.5 pixels. We can calculate
th = arccos(2 . (1 - 0.5 / 100)^2 - 1))
= 11.46 degrees
This corresponds to ceil(360 / 11.46) = 32 points. So if we draw a circle of radius 100 using 32 points our worst pixel will be off by less than a half which should mean every pixel we draw will be in the correct place (ignoring aliasing).
This kind of analysis can be performed for ellipses too, but in the spirit of all good maths that is left as an exercise for the reader ;) (the only difference is determining where the maximum error occurs).
as many as the resolution you are using requires, or as many as the visual result requires an accurate representation. It's difficult to say, and mostly depends on what you want to achieve. In a CAD program, having a circle visually similar to an octagon could be annoying. On the other hand, if you are programming a game on the iphone, if the wheel of a car looks like an octagon it's not a big deal.
A possible strategy you could use is to evaluate the length of each segment with respect to the resolution of the current view, and if longer than, say, 3 pixels, increase the number of vertexes you use, but only for the visible segments. This way, you increase the resolution when zooming in, but you don't have to describe vertexes you are not going to draw.