Ignore:
Timestamp:
2017-11-10T21:06:30+01:00 (6 years ago)
Author:
Don-vip
Message:

extract PolarCoor from AlignInCircleAction

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/AlignInCircleAction.java

    r13106 r13107  
    2121import org.openstreetmap.josm.command.SequenceCommand;
    2222import org.openstreetmap.josm.data.coor.EastNorth;
     23import org.openstreetmap.josm.data.coor.PolarCoor;
    2324import org.openstreetmap.josm.data.osm.DataSet;
    2425import org.openstreetmap.josm.data.osm.Node;
     
    5253    }
    5354
    54     private static double distance(EastNorth n, EastNorth m) {
    55         double easd, nord;
    56         easd = n.east() - m.east();
    57         nord = n.north() - m.north();
    58         return Math.sqrt(easd * easd + nord * nord);
    59     }
    60 
    61     public static class PolarCoor {
    62         private double radius;
    63         private double angle;
    64         private EastNorth origin = new EastNorth(0, 0);
    65         private double azimuth;
    66 
    67         PolarCoor(double radius, double angle) {
    68             this(radius, angle, new EastNorth(0, 0), 0);
    69         }
    70 
    71         PolarCoor(double radius, double angle, EastNorth origin, double azimuth) {
    72             this.radius = radius;
    73             this.angle = angle;
    74             this.origin = origin;
    75             this.azimuth = azimuth;
    76         }
    77 
    78         PolarCoor(EastNorth en) {
    79             this(en, new EastNorth(0, 0), 0);
    80         }
    81 
    82         PolarCoor(EastNorth en, EastNorth origin, double azimuth) {
    83             radius = distance(en, origin);
    84             angle = Math.atan2(en.north() - origin.north(), en.east() - origin.east());
    85             this.origin = origin;
    86             this.azimuth = azimuth;
    87         }
    88 
    89         /**
    90          * Converts this {@code PolarCoor} to an {@link EastNorth} instance.
    91          * @return a new {@code EastNorth} instance
    92          */
    93         public EastNorth toEastNorth() {
    94             return new EastNorth(radius * Math.cos(angle - azimuth) + origin.east(), radius * Math.sin(angle - azimuth)
    95                     + origin.north());
    96         }
    97 
    98         /**
    99          * Create a MoveCommand to move a node to this PolarCoor.
    100          * @param n Node to move
    101          * @return new MoveCommand
    102          */
    103         public MoveCommand createMoveCommand(Node n) {
    104             EastNorth en = toEastNorth();
    105             return new MoveCommand(n, en.east() - n.getEastNorth().east(), en.north() - n.getEastNorth().north());
    106         }
    107     }
    108 
     55    /**
     56     * Create a {@link MoveCommand} to move a node to a PolarCoor.
     57     * @param n Node to move
     58     * @param coor polar coordinate where to move the node
     59     * @return new MoveCommand
     60     * @since 13107
     61     */
     62    public static MoveCommand createMoveCommand(Node n, PolarCoor coor) {
     63        EastNorth en = coor.toEastNorth();
     64        return new MoveCommand(n, en.east() - n.getEastNorth().east(), en.north() - n.getEastNorth().north());
     65    }
    10966
    11067    /**
     
    188145            } else if (outside.size() == 1 && inside.size() == 1) {
    189146                center = outside.get(0).getEastNorth();
    190                 radius = distance(center, inside.get(0).getEastNorth());
     147                radius = center.distance(inside.get(0).getEastNorth());
    191148            } else if (inside.size() == 2 && outside.isEmpty()) {
    192149                // 2 nodes inside, define diameter
     
    194151                EastNorth en1 = inside.get(1).getEastNorth();
    195152                center = new EastNorth((en0.east() + en1.east()) / 2, (en0.north() + en1.north()) / 2);
    196                 radius = distance(en0, en1) / 2;
     153                radius = en0.distance(en1) / 2;
    197154            }
    198155
     
    239196        if (radius == 0) {
    240197            for (Node n : nodes) {
    241                 radius += distance(center, n.getEastNorth());
     198                radius += center.distance(n.getEastNorth());
    242199            }
    243200            radius = radius / nodes.size();
     
    265222            }
    266223            Node first = nodes.get(i % nodeCount);
    267             PolarCoor pcFirst = new PolarCoor(first.getEastNorth(), center, 0);
    268             pcFirst.radius = radius;
    269             cmds.add(pcFirst.createMoveCommand(first));
     224            PolarCoor pcFirst = new PolarCoor(radius, PolarCoor.computeAngle(first.getEastNorth(), center), center);
     225            cmds.add(createMoveCommand(first, pcFirst));
    270226            if (j > i + 1) {
    271227                double delta;
     
    273229                    delta = 2 * Math.PI / nodeCount;
    274230                } else {
    275                     PolarCoor pcLast = new PolarCoor(nodes.get(j % nodeCount).getEastNorth(), center, 0);
     231                    PolarCoor pcLast = new PolarCoor(nodes.get(j % nodeCount).getEastNorth(), center);
    276232                    delta = pcLast.angle - pcFirst.angle;
    277233                    if (delta < 0) // Assume each PolarCoor.angle is in range ]-pi; pi]
     
    280236                }
    281237                for (int k = i+1; k < j; k++) {
    282                     PolarCoor p = new PolarCoor(radius, pcFirst.angle + (k-i)*delta, center, 0);
    283                     cmds.add(p.createMoveCommand(nodes.get(k % nodeCount)));
     238                    PolarCoor p = new PolarCoor(radius, pcFirst.angle + (k-i)*delta, center);
     239                    cmds.add(createMoveCommand(nodes.get(k % nodeCount), p));
    284240                }
    285241            }
Note: See TracChangeset for help on using the changeset viewer.