How to avoid generating Offsets near to other Offsets [flutter[ - 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:

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?

Dividing long and lat coordinates into sub-coordinates(smaller squares)?

I have 2 long, lat points of a rectangle(bottom left and top right) and I want to divide this rectangle into smaller ones based on a base area (long and lat) I already have. I already know that I can't deal with long and lat as distance measured with meters and kilometres but degrees on an approximation of Earth's surface shape.
The points taken is extracted by leaflet with a 4326 SRID and so are the original points. I need the centre of the "smaller squares" or the long and lat coordinates.
For example, this is my base rectangle 24.639567,46.782406 24.641452,46.785413 and for the rectangle, I want to divide 24.584749,46.612782 24.603323,46.653809.
First, let's turn your two points into a leaflet bounds object:
const bounds - L.latLngBounds(point1, point2)
Now let's pick a sample interval, meaning how many sub-rectangles across the width and height of your bounds. For example, a sampling size of 10 would give 100 sub-rectangles (10 x 10), though if your sub-rectangles don't need the same aspect-ratio as your main bounds, you could choose two separate sampling intervals (one for x and one for y)
const samplingInterval = 10 // easy to change
To properly interpolate through your main bounds, we'll grab the corners of it, as well as the width in longitude degrees, and height in latitude degrees, called dLat and dLng (for delta):
const sw = bounds.getSouthWest();
const nw = bounds.getNorthWest();
const ne = bounds.getNorthEast();
const dLat = ne.lat - sw.lat;
const dLng = ne.lng - nw.lng;
Now we can build an array of new bounds extrapolated from the original:
let subBounds = [];
for (let i = 0; i < samplingInterval - 1; i++){
for (let j = 1; j < samplingInterval; j++){
const corner1 = [
sw.lat + (dLat * i) / samplingInterval,
sw.lng + (dLng * j) / samplingInterval
];
const corner2 = [
sw.lat + (dLat * (i + 1)) / samplingInterval,
sw.lng + (dLng * (j + 1)) / samplingInterval
];
subBounds.push(L.latLngBounds(corner1, corner2));
}
}
Now to get the centers of these bounds, you can call .getCenter() on them:
const centerPoints = subBounds.map(bounds => bounds.getCenter());
Working codesandbox

How can i create random rectangles (automatically)?

I want to create many rectangles. This should be done automatically. How can I do this without typing thousands of values in my code? Is there an solution?
In my code I wrote every single coordinate point (4 points of each rectangle) manually in my vector "V".
Also how to connect them. "F"
And the value of each rectangle. "C"
My code is
clc
clear all
figure;
V = [0,0;1,0;1,1;0,1;5,5;10,5;10,10;5,10;2,2;4,2;4,4;2,4];
F = [1,2,3,4;5,6,7,8;9,10,11,12];%Dieser Vektor sagt mir in welcher Reihenfolge die Punkte
C = [50;24;99];
patch('Faces',F,'Vertices',V,'FaceVertexCData',C,'FaceColor','flat','EdgeColor','none') %Befehl fürs "zeichnen"
colormap(parula)
colorbar
You can use the following function to create a random rectangle, by randomly generating an (x, y) position for the bottom left corner, and randomly generating a width and height -
function rect = createRandomRectangle(maxX, maxY, minHeight, maxHeight, minWidth, maxWidth)
bottom = maxY * rand;
left = maxX * rand;
height = minHeight + rand * (maxHeight - minHeight);
width = minWidth + rand * (maxWidth - minWidth);
rect = [
left, bottom
left, bottom + height
left + width, bottom + height
left + width, bottom
];
end
Then you just need to take care of creating your V, F, C matrices (by calling createRandomRectangle in a loop) and plotting them.

Trouble with the assignment of values to pixels

I'm currently trying to write a function in MatLab which loops over each pixel, takes the mean intensity of the pixels within a radius around it and then applies that intensity to the central pixel, effectively blurring the image.
I start by declaring the function and finding the maximum width and height of the image, nx and ny:
function [] = immean(IMAGE, r)
[nx, ny] = size(IMAGE);
I then create a completely black image of the same size as the image variable IMAGE. This is so that I can store the value of each pixel, once the mean intensity of its neighbourhood has been found.
average = zeros(size(IMAGE));
I then loop through the image:
for x = 1:nx
for y = 1:ny
and apply a series of if-statements to deal with cases where the radius of the circle around the pixel does not fit the image. (For example, a pixel at (1,1) with a radius of 5 would have a starting point of -4, which would cause an error):
if x-r <= 0
startx = 1;
else
startx = x-r;
end
if x+r > nx
endx = nx;
else
endx = x+r;
end
if y-r <= 0
starty = 1;
else
starty = y-r;
end
if y+r > ny
endy = ny;
else
endy = y+r;
end
This effectively creates a square of values that may fall under the domain of the circular sample, which speeds up the program dramatically. After that, I iterate through the values within this square and find any pixels which fall within the radius of the central pixel. The intensities of these pixels are then added to a variable called total and the count pixelcount increments:
total = 0;
pixelcount = 0;
for xp = startx : endx
for yp = starty : endy
if (x-xp)^2 + (y-yp)^2 <= r^2
total = total + uint64(IMAGE(xp, yp));
pixelcount = pixelcount + 1;
end
end
end
I then find the mean intensity of the circular sample of pixels, by dividing total by pixelcount and then plug that value into the appropriate pixel of the completely black image average:
mean = total / pixelcount;
average(x,y) = mean;
The trouble is: this isn't working. Instead of a blurred version of the original image, I get an entirely white image instead. I'm not sure why - when I take the ; from the last line, it shows me that mean constitutes many values - it's not like they're all 255. So I figure that there must be something wrong with the assignment line average(x,y) = mean;, but I can't find out what that is.
Can anyone see why this is going wrong?

Generate random GeoJSON polygons

Is there a way or tool to generate random GeoJSON polygons of specific size withing bounding box? Specifically I want to populate mongodb with a lot of random polygons and test specific functionality.
You could do it programmatically using the bounding box coordinates to generate the random bounding box coords for the rectangles.
For example, if your bounding box is [[100,100],[200,200]] you could do the following:
// generate a random width and height
// (e.g. with random numbers between 1 and 50)
var width = Math.floor(Math.random() * 50) + 1;
var height = Math.floor(Math.random() * 50) + 1;
// generate a random position that allows the rectangle to fit within the bounding box walls
// 100 is used in the calculation as 100 is the width and height of the example bounding box
var upperX = Math.floor(Math.random() * (100-width)) + 1;
var upperY = Math.floor(Math.random() * (100-height)) + 1;
var lowerX = upperX + width;
var lowerY = upperY + height;
var bounds = [[upperX, upperY], [lowerX, lowerY]];
// create rectangle
L.rectangle(bounds, {color: "#ff7800", weight: 1}).addTo(map);
// loop through above code some chosen number of times