View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.optim.nonlinear.vector.leastsquares;
23  
24  import org.hipparchus.geometry.euclidean.twod.Vector2D;
25  import org.hipparchus.random.RandomDataGenerator;
26  import org.hipparchus.util.FastMath;
27  import org.hipparchus.util.MathUtils;
28  
29  /**
30   * Factory for generating a cloud of points that approximate a circle.
31   */
32  public class RandomCirclePointGenerator {
33      /** Radius of the circle. */
34      private final double radius;
35      /** Random data generator */
36      private final RandomDataGenerator randomDataGenerator;
37      /** Error on the x-coordinate of the circumference points. */
38      private final double xSigma;
39      /** Error on the y-coordinate of the circumference points. */
40      private final double ySigma;
41      /** Abscissa of the circle center. */
42      private final double x;
43      /** Ordinate of the circle center. */
44      private final double y;
45  
46  
47      /**
48       * @param x Abscissa of the circle center.
49       * @param y Ordinate of the circle center.
50       * @param radius Radius of the circle.
51       * @param xSigma Error on the x-coordinate of the circumference points.
52       * @param ySigma Error on the y-coordinate of the circumference points.
53       * @param seed RNG seed.
54       */
55      public RandomCirclePointGenerator(double x,
56                                        double y,
57                                        double radius,
58                                        double xSigma,
59                                        double ySigma,
60                                        long seed) {
61          randomDataGenerator = new RandomDataGenerator(seed);
62          this.radius = radius;
63          this.xSigma = xSigma;
64          this.ySigma = ySigma;
65          this.x = x;
66          this.y = y;
67      }
68  
69      /**
70       * Point generator.
71       *
72       * @param n Number of points to create.
73       * @return the cloud of {@code n} points.
74       */
75      public Vector2D[] generate(int n) {
76          final Vector2D[] cloud = new Vector2D[n];
77          for (int i = 0; i < n; i++) {
78              cloud[i] = create();
79          }
80          return cloud;
81      }
82  
83      /**
84       * Create one point.
85       *
86       * @return a point.
87       */
88      private Vector2D create() {
89          final double t = randomDataGenerator.nextUniform(0,  MathUtils.TWO_PI);
90          final double pX = randomDataGenerator.nextNormal(x, xSigma) + radius * FastMath.cos(t);
91          final double pY = randomDataGenerator.nextNormal(y, ySigma) + radius * FastMath.sin(t);
92  
93          return new Vector2D(pX, pY);
94      }
95  }