[ 
https://issues.apache.org/jira/browse/SIS-49?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13408364#comment-13408364
 ] 

Ross Laidlaw edited comment on SIS-49 at 7/6/12 10:46 PM:
----------------------------------------------------------

I've modified the loop and surrounding code as shown below to make it a bit 
easier to read and to prevent the ArrayIndexOutOfBoundsException:

{code}
LatLon[] points = new LatLon[numberOfPoints + 1];

double bearingIncrement = 0;
if (numberOfPoints > 0) { bearingIncrement = 360/numberOfPoints; }

for (int i = 0; i < numberOfPoints; i++) 
{
  points[i] = DistanceUtils.getPointOnGreatCircle(this.center.getLat(),
      this.center.getLon(), radius, i * bearingIncrement);
}

points[numberOfPoints] = points[0];
{code}

                
      was (Author: rlaidlaw):
    I've modified the loop and surrounding code as shown below to make it a bit 
easier to read and to prevent the ArrayIndexOutOfBoundsException:

{code}
LatLon[] points = new LatLon[numberOfPoints + 1];
int bearingIncrement = 360/numberOfPoints;
for (int i = 0; i < numberOfPoints; i++) 
{
  points[i] = DistanceUtils.getPointOnGreatCircle(this.center.getLat(),
      this.center.getLon(), radius, i * bearingIncrement);
}
points[numberOfPoints] = points[0];
{code}

                  
> ArrayIndexOutOfBoundsException caused by method 
> getCircularRegionApproximation(int numberOfPoints) in LatLonPointRadius class
> -----------------------------------------------------------------------------------------------------------------------------
>
>                 Key: SIS-49
>                 URL: https://issues.apache.org/jira/browse/SIS-49
>             Project: Spatial Information Systems
>          Issue Type: Bug
>          Components: geometry objects
>            Reporter: Ross Laidlaw
>            Assignee: Ross Laidlaw
>            Priority: Minor
>              Labels: gsoc2012
>             Fix For: 0.3-incubating
>
>         Attachments: SIS-49.rlaidlaw.2012-07-06.patch.txt
>
>
> The method public LatLon[] getCircularRegionApproximation(int numberOfPoints) 
> throws an ArrayIndexOutOfBoundsException under certain conditions.  This is 
> because of an error in the following loop:
> {code}
> LatLon[] points = new LatLon[numberOfPoints + 1];
> for (int i = 0; i < 360; i += (360 / numberOfPoints)) 
> {
>   points[i] = DistanceUtils.getPointOnGreatCircle(this.center.getLat(),
>       this.center.getLon(), radius, i);
> }
> {code}
> The exception arises because integer i is used as the array index for the 
> points array, but the value of i jumps up by (360/numberOfPoints) on every 
> iteration of the loop.  For example, if numberOfPoints is 10, then i will 
> increase by 36 each time, resulting in the exception.  The points array size 
> is set before the loop to 'numberOfPoints + 1'.
> As an experiment, I changed the loop as shown below and this appeared to fix 
> the problem.  There may be a more elegant solution:
> {code}
> LatLon[] points = new LatLon[numberOfPoints + 1];
> for (int i = 0, j = 0; i < 360; i += (360 / numberOfPoints), j++) 
> {
>   points[j] = DistanceUtils.getPointOnGreatCircle(this.center.getLat(),
>       this.center.getLon(), radius, i);
> }
> {code}

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: 
https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

Reply via email to