Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help in coding - sub-diving area into circular cells.

Status
Not open for further replies.

denisedede

Programmer
Jul 31, 2013
4
GB
Hi, I am still new to fortran programming and would like some help in starting my code. I was given the task to subdivide a rectangular area into circular cells, one next to the other. I then have to be able to change the radii of the circles to be able to overlap these cells. The area I am looking at has a number of different species. If a species is found in a particular circle, then I will have an output saying cell = i,j species = 2. I hope I made it clear.
Can someone help me at least get started?
 
I don't quite understand.

1) The rectangular area is divided into circular cells. This means that there are areas not covered by the cells.
2) Are all the cells the same size. If you change the radius, do all of them change?
3) If they overlap, what kind of overlap is it? Is it square or hexagonal or some other polygon shape?
4) Can the sizes be random, as long as the whole rectangular area is covered?
 
Hi,

As to your questions, this is the information:

1) What I have is a rectangular area and I need to subdivide it into circular cells. I am not entirely sure how to go about that.
2) The cells need to be varied in size and I need to be able to change the same radius for all the cells, but also change one radius one size and another another size.
3) If they overlap the overlap is that of a circle.
4) The sizes should be specified by the radius.
 
For example, for storing N circles you can use 2 allocatable arrays:
for circle middlepoints: M of size N*2
and for circle radiuses: R of size N.
On the beginning for a given rectangular area and a given N (number of circles) you can compute coordinates of the middlepoints of the circles and for all circles the same radius and store these values into the arrays M and R.
Later you can modify each of the arrays.
 
Mikrom, could you guide me as to how to start the programming? As I mentioned previously the logic is ok, but I am struggling with writing any piece of code.
 
OK, here is something to start:
Code:
[COLOR=#a020f0]program[/color] denisedede
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
[COLOR=#2e8b57][b]  real[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color](:,:), [COLOR=#2e8b57][b]allocatable[/b][/color] :: centre_point
[COLOR=#2e8b57][b]  real[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color](:), [COLOR=#2e8b57][b]allocatable[/b][/color] :: radius
[COLOR=#2e8b57][b]  real[/b][/color] :: x, y, r, d, centre_x, centre_y
  [COLOR=#2e8b57][b]integer[/b][/color] :: n, n1, n2, i, j, k

  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]'(A)'[/color]) [COLOR=#ff00ff]'Coordinates of rectangle area 0 --> X, 0 --> Y: '[/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]'(A)'[/color], [COLOR=#804040][b]advance[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'no'[/color]) [COLOR=#ff00ff]' X = '[/color]
  [COLOR=#804040][b]read[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) x
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]'(A)'[/color], [COLOR=#804040][b]advance[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'no'[/color]) [COLOR=#ff00ff]' Y = '[/color]
  [COLOR=#804040][b]read[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) y
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]'(A)'[/color]) [COLOR=#ff00ff]'Initial Radius of the circles'[/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]'(A)'[/color], [COLOR=#804040][b]advance[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'no'[/color]) [COLOR=#ff00ff]' R = '[/color]
  [COLOR=#804040][b]read[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) r

  [COLOR=#0000ff]! initial diameter[/color]
  d [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]2[/color][COLOR=#804040][b]*[/b][/color]r

  [COLOR=#0000ff]! number of circles in direction X[/color]
  n1 [COLOR=#804040][b]=[/b][/color] x[COLOR=#804040][b]/[/b][/color]d

  [COLOR=#0000ff]! number of circles in direction Y[/color]
  n2 [COLOR=#804040][b]=[/b][/color] y[COLOR=#804040][b]/[/b][/color]d

  [COLOR=#0000ff]! number of all circles[/color]
  n [COLOR=#804040][b]=[/b][/color] n1[COLOR=#804040][b]*[/b][/color]n2

  [COLOR=#0000ff]! allocate arrays[/color]
  [COLOR=#804040][b]allocate[/b][/color](centre_point(n,[COLOR=#ff00ff]2[/color]))
  [COLOR=#804040][b]allocate[/b][/color](radius(n))

  [COLOR=#0000ff]! compute centre points for circles[/color]
  k [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/color]
  [COLOR=#804040][b]do[/b][/color] j[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color], n2
    centre_y [COLOR=#804040][b]=[/b][/color] ([COLOR=#ff00ff]2[/color][COLOR=#804040][b]*[/b][/color]j[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color])[COLOR=#804040][b]*[/b][/color]r
    [COLOR=#804040][b]do[/b][/color] i[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color], n1
      k [COLOR=#804040][b]=[/b][/color] k [COLOR=#804040][b]+[/b][/color] [COLOR=#ff00ff]1[/color]
      centre_x [COLOR=#804040][b]=[/b][/color] ([COLOR=#ff00ff]2[/color][COLOR=#804040][b]*[/b][/color]i[COLOR=#804040][b]-[/b][/color][COLOR=#ff00ff]1[/color])[COLOR=#804040][b]*[/b][/color]r
      centre_point(k,[COLOR=#ff00ff]1[/color]) [COLOR=#804040][b]=[/b][/color] centre_x
      centre_point(k,[COLOR=#ff00ff]2[/color]) [COLOR=#804040][b]=[/b][/color] centre_y
    [COLOR=#804040][b]end do[/b][/color]
  [COLOR=#804040][b]end do[/b][/color]  

  [COLOR=#0000ff]! compute radiuses  [/color]
  [COLOR=#804040][b]do[/b][/color] i[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color], n
    radius(i) [COLOR=#804040][b]=[/b][/color] r
  [COLOR=#804040][b]end do[/b][/color]

  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]'(A)'[/color]) [COLOR=#ff00ff]'Circles computed:'[/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]100[/color]) n1, n2, n
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])
  [COLOR=#804040][b]do[/b][/color] k[COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]1[/color], n
    [COLOR=#0000ff]! print k-th circle[/color]
    [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]200[/color]) k, centre_point(k,[COLOR=#ff00ff]1[/color]), centre_point(k,[COLOR=#ff00ff]2[/color]), radius(k)
  [COLOR=#804040][b]end do[/b][/color]

  [COLOR=#0000ff]! at end deallocate all arrays[/color]
  [COLOR=#804040][b]deallocate[/b][/color](centre_point)
  [COLOR=#804040][b]deallocate[/b][/color](radius)

  [COLOR=#0000ff]! formats[/color]
  [COLOR=#6a5acd]100[/color] [COLOR=#804040][b]format[/b][/color] ([COLOR=#ff00ff]'N1 = '[/color],i2,[COLOR=#ff00ff]', N2 = '[/color],i2,[COLOR=#ff00ff]', N = '[/color],i2)
  [COLOR=#6a5acd]200[/color] [COLOR=#804040][b]format[/b][/color] ([COLOR=#ff00ff]'Circle'[/color],i2,[COLOR=#ff00ff]': C = ('[/color],[COLOR=#008080]f5.2[/color],[COLOR=#ff00ff]','[/color],[COLOR=#008080]f5.2[/color],[COLOR=#ff00ff]'), R ='[/color],[COLOR=#008080]f5.2[/color])  
[COLOR=#a020f0]end program[/color] denisedede

Compile & run:
Code:
$ denisedede
Coordinates of rectangle area 0 --> X, 0 --> Y: 
 X = 2
 Y = 3
Initial Radius of the circles
 R = 0.5
Circles computed:
N1 =  2, N2 =  3, N =  6

Circle 1: C = ( 0.50, 0.50), R = 0.50
Circle 2: C = ( 1.50, 0.50), R = 0.50
Circle 3: C = ( 0.50, 1.50), R = 0.50
Circle 4: C = ( 1.50, 1.50), R = 0.50
Circle 5: C = ( 0.50, 2.50), R = 0.50
Circle 6: C = ( 1.50, 2.50), R = 0.50
 
For simplicity in the example above I read the value of the radius (instead of number of all circles) and compute the circle centre points only on rectangular grid. So all circles have constant radius and there are points in the area, not covered by the circles.
You will need an algorithm how to compute the circles (i.e. centre points and radiuses), so that every point in the area is covered by minimal one circle. I have no idea about such things ...

 
If you want to have your domain covered by circles that don't overlap, but each touch the circles next to them, then stop thinking about circles for a minute. Instead, think of a hexagonal tessellation, e.g.

hexagon.gif


Now, inscribe each of these circles with a circle. Out of all possible ways to cover the domain with non-overlapping circles, this would cover the most area. I.E., it would leave the least space not covered by circles. As for the data structures, that is a bit (1/8 of a byte) more complicated. The easiest way would probably be to look at the picture I attached, then tilt your head either way by 30[sup]o[/sup]. Each row of hexagons that you see could be represented by a row in a two-dimensional array. Keep in mind that each even row of this array would have the same horizontal offset, but each odd row would be offset by half of the circle's radius.

If that sounded complicated, then forget that last paragraph and consider instead a square tessellation. You haven't really provided any information about how the circles are patterned or how they overlap or not, so this should be just as valid and much easier. Inscribe each square in this grid with a circle. Now, each row has the same offset, and you don't have to worry about all that stuff I mentioned in the last paragraph.

Yet another way to solve this problem would be to scatter a random set of points across your two-dimensional area. Now all you have to do is create an array of size n, where n is the number of points. Fill each element of this array with the distance from your point (x,y) to the random point. Now, find the minimum of that array. The distance is the radius of your circle, and index of the minimum is the center of the circle that contains your point (x,y).
 
Thanks for the help. What I now trying to do is to have for example a 50% overlap of the circles, that is I would be moving the centre of the circles. I am not entirely sure that if I pre-specify the N value for overlapping circles, this is doing a correct job!
 
denisedede said:
... I would be moving the centre of the circles. I am not entirely sure that if I pre-specify the N value for overlapping circles, this is doing a correct job!
If you change the value of N (number of circles) you have to re-compute centre points and radiuses.
 
denisedede said:
I am not entirely sure that if I pre-specify the N value for overlapping circles, this is doing a correct job!
If you only need that the circles overap then entlarge the radius of the circles.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top