How to fit overlapping elements of fixed width within a certain space? - unity3d

How can I evenly fit UI elements of a certain fixed width inside a fixed space on the screen? I want to calculate the position to the place the elements inside the space, without scaling or rotating the elements.
For eg: I want to fit the 4 cards which are 500 units in width inside the white space in the bottom which is 1000 units wide.
expected result: the four cards are placed such that they occupy 1000 units.
The expected result if there are eight cards.

card_offset = (total_width - card_width) / (number_of_cards - 1) * card_index;
Example:
1st card offset = (1000 - 500) / (4 - 1) * 0 = 0
2st card offset = (1000 - 500) / (4 - 1) * 1 = 167
3st card offset = (1000 - 500) / (4 - 1) * 2 = 334
4st card offset = (1000 - 500) / (4 - 1) * 3 = 500

Related

Prevent plotting random points near to the existing lines [flutter]

So I have an application where I'm generating random offsets. The generation and plotting process is working just fine but sometimes the new points generated are very near to the point line. How can I update those points such that they don't appear near to an already plotted line? Here is my current code for generating the values:
// list to store the offsets
final List<Offset> offsets = [];
// generating 5 random offsets
for (int i = 0; i < points; i++) {
// width random ranges from 20% of width to 80% of width
double randomWidth =
Random().nextInt((width * .6).toInt()) + (width * .2);
// height random ranges from 20% of height to 80% of height
double randomHeight =
Random().nextInt((height * .6).toInt()) + (height * .2);
// adding the offset to the list
offsets.add(Offset(randomWidth, randomHeight));
// generating the lines polygon
canvas.drawPoints(ui.PointMode.polygon, offsets, paint);
}
Here is the output that I don't want where the points are near to another line:
These are good ones:
Are there any algorithms or something to help with this?

How to avoid generating Offsets near to other Offsets [flutter[

So I have this application where I'm generating random offsets to plot a random shape on the canvas. This is how the code looks like for generating random offsets:
// generating 5 random offsets
for (int i = 0; i < points; i++) {
// width random ranges from 20% of width to 80% of width
double randomWidth =
Random().nextInt((width * .6).toInt()) + (width * .2);
// height random ranges from 20% of height to 80% of height
double randomHeight =
Random().nextInt((height * .6).toInt()) + (height * .2);
// adding the offset to the list
offsets.add(Offset(randomWidth, randomHeight));
}
// generating the lines polygon
canvas.drawPoints(ui.PointMode.polygon, offsets, paint);
This code seems to be working fine but there is an issue. Sometimes the points seems to be plotted very near to other points and sometimes on the other plotted line. What I want is to have all the points maintain some distance from each other so that they are not close to each other. These are the plots that I don't want:

How to calculate a percentile based on race position where 1st place should equal 100%

I have a table with Raceid, OverallPosition int and OverallCompetitors int.
How do i calculated a percentage for each competitor where 1st place = 100% and last is > 0%
OverallPercentileCalc = 100 - (cast(OverallPosition - 1 as decimal(10,4)) / cast(OverallCompetitors as decimal(10,4)) * 100)

Audio waveform and drawing grid lines - lines drifting over time

I'm creating a simple audio playback MacOS application in swift. I have a waveform drawn on the screen (using https://github.com/benoit-pereira-da-silva/SoundWaveForm) along with grid lines for each bar/measure (gray lines), and grid lines for each quarter note (red lines). See image at the end of the post.
The duration of 1 bar and the total number of bars in the audio file are calculated like so:
let quantify1Bar = (60000 / BPM) * 4 // Milliseconds per bar
let totalBars = totalAudioDuration * 1000 / quantify1Bar
Then I'm calculating the pixel distance between grid lines:
let pxSpacing1Bar = (pxTimelineWidth / totalBars) * zoomLevel
let pxSpacingQuarter = (pxTimelineWidth / totalBars) * zoomLevel
pxTimelineWidth is 1560 pixels
My zoomLevel variable just divides or multiplies itself when the "-" and "+" buttons are clicked (i.e. if zoomLevel is 1, you see the full duration of the audio waveform...if zoomLevel is 2, you see half of the audio length... 4 = a quarter length, etc:
zoomLevel = zoomLevel / 2 (zoom out)
zoomLevel = zoomLevel * 2 (zoom in)
I'm drawing the grid lines like this:
let pathBar = NSBezierPath()
let pathQuarter = NSBezierPath()
// Draw lines for every bar
for i in 0...totalBarLines {
pathBar.move(to: NSPoint(x: i * pxSpacing1Bar, y: 0))
pathBar.line(to: NSPoint(x: i * pxSpacing1Bar, y: 100))
pathBar.lineWidth = 1
NSColor.darkGray.setFill()
NSColor.darkGray.setStroke()
pathBar.stroke()
}
// Draw 1/4 note lines
for i in 0...totalQuarterLines {
if i == 0 {
// Do nothing - skip the first line of every bar
} else if i % 4 != 0 {
pathQuarter.move(to: NSPoint(x: i * pxSpacingQuarter, y: 10))
pathQuarter.line(to: NSPoint(x: i * pxSpacingQuarter, y: 90))
pathQuarter.lineWidth = 1
NSColor.red.setFill()
NSColor.red.setStroke()
pathQuarter.stroke()
}
}
My issue is that on higher zoom levels, the grid lines have a noticeable drift even over a short period of time, and each drift is greater than the previous drift (see image). This seems simple enough, is my zoomLevel variable causing this during the grid line drawing loops?
AudioGridLines

iText7 Bug: Endless loop in ImageRenderer

i came across an endless-loop-issue due to floating point arithmetic in iText7 DotNet ImageRenderer.
I've added an image with 2464x3692 pixels to an PDF page area of 523x770 pixels, causing the Layout() function to loop endlessly because every iteration returned LayoutResult.NOTHING.
The main issue is that the image height is calculated inside the Layout-function using the following statement:
Line 90: height = (float)width / imageWidth * imageHeight;
Due to the floating point operation the height got a value of something like 770.0001.
This is greater than the area height, causing the loop to return LayoutResult.NOTHING, so the image gets autoscaled again.
Inside the AutoScale-function only the image width is checked, which IS inside the area boundaries and so nothing is scaled.
I've fixed this issue by changing the following code inside ImageRenderer.cs:
90 - height = (float)width / imageWidth * imageHeight;
90 + float? propHeight = RetrieveHeight();
91 + height = propHeight == null ? (float)width / imageWidth * imageHeight : propHeight.Value;
214 - SetProperty(Property.HEIGHT, UnitValue.CreatePointValue(area.GetBBox().GetHeight()));
215 + SetProperty(Property.HEIGHT, area.GetBBox().GetHeight());
ImageRenderer now uses the height that is set inside AutoScale without recalculating it if not necessary.
Hopefully this will be fixed in one of the next releases. :)
Regards
Yosh