V
V
VasG2011-03-27 21:34:53
Java
VasG, 2011-03-27 21:34:53

Cut off all intersecting circles. The most trivial task, but misunderstandings arise during implementation.

Most likely this is due to an incomplete understanding (by me) of the specifics of the JAVA language.
In general: there is a two-dimensional array array[][], where each coordinate corresponds to either zero or non-zero. Not zero means at this point the center of the circle, and the radius is set from the outside and it is constant throughout the cycle. The value of the array element means the approximate probability that the center of the circle should actually be at this point. There is a certain parameter ( kriteria) that cuts off all unlikely options.
There are many circles whose centers are very close to each other, and there are also circles that intersect. This is harmful, because. the array data is used for subsequent statistical processing.
Actually how I do it:

  1. // Analysis of the original array.
  2.      for  ( int  x1  =  0 ;  x1  <  width ;  x1 ++ )  {
  3.          for  ( int  y1  =  0 ;  y1  <  height ;  y1 ++ )  {
  4.              // If the counter is less than the criterion, then reset it to hell.
  5.              if  ( array [ x1 ] [ y1 ]  <  kriteria )  {
  6.                  array [ x1 ] [ y1 ]  =  0 ;
  7.              }  else  {  // If not, continue the comparison.
  8.                  for  ( int  x2  =  x1  +  1 ;  x2  <  width ;  x2 ++ )  {
  9.                      for  ( int  y2  =  y1  +  1 ;  y2  <  height ;  y2 ++ )  {
  10.                          // If the counter is less than the criterion, then reset it to hell.
  11.                          if  ( array [ x2 ] [ y2 ]  <  kriteria )  {
  12.                              array [ x2 ] [ y2 ]  =  0 ;
  13.                          }  else  {  // If not, continue the comparison.
  14.                              // It's time to calculate the distance between the circles.
  15.                              long  rasst  =  Math . round ( Math . sqrt ( Math . pow ( y2  -  y1,  2 )  +  Math . pow ( x2  -  x1,  2 ) ) ) ;
  16.                              if  ( rasst  <  rasstKriteria )  {
  17.                                  array [ x2 ] [ y2 ]  =  array [ x1 ] [ y1 ]  +  array [ x2 ] [ y2 ] ;
  18.                                  array [ x1 ] [ y1 ]  =  0 ;
  19.                              }
  20.                          }
  21.                      }
  22.                  }
  23.              }
  24.          }
  25.      }


At the same time, a huge part (an array of 3000x2000) of unnecessary points is cut off, but a part still remains! CHADNT? Am I wrong in the code? Please let me know if you have some time!

Answer the question

In order to leave comments, you need to log in

5 answer(s)
X
xappymah, 2011-03-27
@VasG

It seems to me that the problem may be due to the fact that the inner loop (c x2 and y2) goes over too limited an area - over a rectangle from (x1, y1) to (width, height), although it should go through all the elements of the rectangle ( x1 + 1, 0) - (width, height) and element by element (x1, y1 +1) - (x1, height).
In your case, there will never be a comparison of such points, for example, as (x1, y1) and (x1 + 1, y1 -1), because the second is not included in the inner loop.

E
ENargit, 2011-03-28
@ENargit

I have another idea for you.
Why are you using such a huge nested second loop? Indeed, for the point (0,0), the second cycle will run through essentially the entire array (how many do you have there - 3000x2000?). It would be possible to run only [x1,x1+rasstKriteria],[y1,y1+rasstKriteria]. I think it's more efficient.

A
Anatoly, 2011-03-27
@taliban

Sorry for being off topic, but I just couldn't get past the rasst and rasstKriteria variables silently >.<
why didn't you name the width and height variables shirin and visot respectively?

E
ENargit, 2011-03-27
@ENargit

It doesn't seem to me that the question here is really only a misunderstanding of the specifics of Java. A few points stand out to me:
- I would do the zeroing "to hell" in one place with a full double cycle at the beginning (less side effects to track).
- Math.round rounding confuses. Is this exactly what you need? Simply, in principle, this can be the reason for the loss of several options.
- well, check if you forgot to take into account the doubling of the radius in rasstKriteria.
In general, it is really difficult to make assumptions without seeing at least some example.

A
Alexander, 2011-03-27
@Awake

well so take a point which is not cut off and look what happens to it. Cap is hinting.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question