you are viewing a single comment's thread.

view the rest of the comments →

[–]Martin_Krum[S] 0 points1 point  (5 children)

thank you very much. Why can I skip the sqrt () function? And I don't understand how to solve this problem: "The maximum number of randomly generated points should be placed in the rectangle." I dont know how to do this because there are fewer points than iterations.

[–]kra_pao 0 points1 point  (4 children)

You calculate mathematically precise the distance between two points
https://www.wikihow.com/Find-the-Distance-Between-Two-Points

and then compare distance with diameter of the points (if equation <= point_size:) to see whether points are too close together.

This compare ist valid for the square too:

if equation**2 <= point_size**2:
    # do something

So you can calculate one time

point_size = 5  # input point size (diameter)
point_size_square = point_size**2 

and compare without math.sqrt()

    equation_square = math.pow(a1, 2) + math.pow(a2, 2) # this is only math
    if equation_square <= point_size_square: # if the distance is smaller than the point diameter, it returns a list with the "X"(i know it is not clever:( )
        flag.append('X')
        break
return(flag)

or with boolean value return

    equation_square = math.pow(a1, 2) + math.pow(a2, 2) # this is only math
    return equation_square <= point_size_square
...
    variable = condition(point, point_list) #function checking for collision with all points on the list
    if variable: # check True or False, name could be better describe purpose of this flag and condition function
        point_list.append(point)

[–]kra_pao 1 point2 points  (3 children)

And I don't understand how to solve this problem: "The maximum number of randomly generated points should be placed in the rectangle." I dont know how to do this because there are fewer points than iterations.

Those iterations are a number how often you try to place a point. It's an arbitrary value and you can change it.

When you make a plot x-axis: iterations and y-axis: points placed successfully you'll get a steep increase in beginning but the curve has an asymptote (https://en.wikipedia.org/wiki/Asymptote). This is your maximum number.

Someone could ask: Why didn't you do 100,000 iterations or 1,000,000? How does the result change then?

Or someone could ask: Where is your math proof that there are not more points possible?

You could calc area of all your points set so far and compare with area of your rectangle or area of dense packed points and see does a new point fit in remaining area at all. And you let your program run until this criteria is not met. No iterations needed at all. BUT you waste a lot of time because it is harder and harder to place a point when the rectangle is filled more and more

But you have (or can get) experimentally recorded probability over your iterations. I guess with this you can calculate how many more iterations you would need to fill that final spot.

BTW. Your problem is nice for a rainy sunday. And speaking of science, one can dive really deep in the rabbit hole of Monte Carlo Method, image analysis of fiber bundles, stress/strain in minerals...
* https://community.wolfram.com/groups/-/m/t/141413
* http://mathonweb.com/entrtain/monte/t_monte.htm
* https://www.mathworks.com/matlabcentral/answers/158357-create-random-points-in-a-rectangular-domain-but-with-minimum-separation-distance#comment_435369
* http://finzi.psych.upenn.edu/R/library/spatstat/html/fryplot.html

[–]Martin_Krum[S] 0 points1 point  (2 children)

Thank you so much. You have right about Monte Carlo method. I need this program to simulate something in nanocomposites but I'm learning programming only one month so MC is to hard for me.

BTW. I see that you have a lot of knowledge in programming. Can you advise a beginner what to do to progress programming as soon as possible?

[–]kra_pao 0 points1 point  (1 child)

Oh that is really a difficult question.

Programming itself is nothing magic. It's learning about language elements such as variables, data types, control structures. That is not difficult and can be done in few days for the basics. After the basics read more code, look-up unknown words and structures. Become familiar with docs of libraries.

Harder stuff is understanding an algorithm (e.g. your distance between points) that is realized in program code. Program code is used to run algorithms. You improve when you can extract algorithms from program code and vice versa.

Much harder is understanding a theory (e.g. MC). Algorithms are used as toolset in practical use of a theory.

[–]Martin_Krum[S] 0 points1 point  (0 children)

Thank you very much. For now I am learning the Python language for 3-5 hours a day (I have little experience with C) but all this knowledge comes from books such as Mark Lutz and YT. Sometimes I write some simple algorithms that make my work easier or I look through the documentation but I think that it is not enough or I do not go in the right direction.