Changeset 30345 in osm for applications


Ignore:
Timestamp:
2014-03-24T21:33:27+01:00 (10 years ago)
Author:
donvip
Message:

[josm_alignways] code cleanup

Location:
applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways/AlignWaysAction.java

    r28624 r30345  
    3232                tr("Makes a pair of selected way segments parallel by rotating one of them "
    3333                        + "around a chosen pivot."),
    34                                 Shortcut.registerShortcut("tools:alignways", tr("Tool: {0}", tr("Align Ways")),
     34                Shortcut.registerShortcut("tools:alignways", tr("Tool: {0}", tr("Align Ways")),
    3535                                KeyEvent.VK_SPACE, Shortcut.SHIFT)
    36                                 , true);
     36                , true);
    3737        setEnabled(false);
    3838    }
  • applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways/AlignWaysAlgnSegment.java

    r27348 r30345  
    6464    }
    6565
    66     /* (non-Javadoc)
    67      * @see com.tilusnet.josm.plugins.alignways.AlignWaysSegment#setSegmentEndpoints(org.openstreetmap.josm.data.osm.WaySegment)
    68      */
    6966    @Override
    7067    void setSegmentEndpoints(WaySegment segment) {
     
    7572            adjWaySegs.put(nA, new ArrayList<WaySegment>(determineAdjacentWaysegments(nA)));
    7673        }
    77 
    7874    }
    7975
     
    223219    }
    224220
    225     /*
    226      * (non-Javadoc)
    227      *
    228      * @see
    229      * com.tilusnet.josm.plugins.alignways.AlignWaysRefSegment#paint(java
    230      * .awt.Graphics2D, org.openstreetmap.josm.gui.MapView,
    231      * org.openstreetmap.josm.data.Bounds)
    232      */
    233221    @Override
    234222    public void paint(Graphics2D g, MapView mv, Bounds bbox) {
     
    248236        // Highlight active pivot
    249237        highlightPivot(g, mv, getPivotCoord(currPivot));
    250 
    251238    }
    252239
  • applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways/AlignWaysCmdKeepAngles.java

    r29854 r30345  
    5555
    5656        if (endpoint.getEastNorth().equals(pivot)) {
    57                 // endpoint is pivot: the coordinates won't change
    58                 return;
     57            // endpoint is pivot: the coordinates won't change
     58            return;
    5959        }
    6060       
     
    6464            //  - the alignee following the keep length rule
    6565            //  - the adjacent way segment
    66                
    67                 Node adjOther1 = getNonEqualNode(alws.get(0), endpoint);
    68                 EastNorth enAdjOther1 = adjOther1.getEastNorth();
    69                 Node adjOther2 = null;
    70                 EastNorth enAdjOther2 = null;
     66           
     67            Node adjOther1 = getNonEqualNode(alws.get(0), endpoint);
     68            EastNorth enAdjOther1 = adjOther1.getEastNorth();
     69            Node adjOther2 = null;
     70            EastNorth enAdjOther2 = null;
    7171
    7272            if (alws.size() == 2) {
    73                 adjOther2 = getNonEqualNode(alws.get(1), endpoint);
    74                 enAdjOther2 = adjOther2.getEastNorth();
    75                
    76                 // In order have a chance to align, (enAdjOther1, enAdjOther2 and endpoint) must be collinear
    77                 ArrayList<EastNorth> enAdjPts = new ArrayList<EastNorth>(3);
    78                 enAdjPts.add(enAdjOther1);
    79                 enAdjPts.add(endpoint.getEastNorth());
    80                 enAdjPts.add(enAdjOther2);
    81                 if (!isEnSetCollinear(enAdjPts)) {
    82                         // Not collinear, no point to proceed
     73                adjOther2 = getNonEqualNode(alws.get(1), endpoint);
     74                enAdjOther2 = adjOther2.getEastNorth();
     75               
     76                // In order have a chance to align, (enAdjOther1, enAdjOther2 and endpoint) must be collinear
     77                ArrayList<EastNorth> enAdjPts = new ArrayList<EastNorth>(3);
     78                enAdjPts.add(enAdjOther1);
     79                enAdjPts.add(endpoint.getEastNorth());
     80                enAdjPts.add(enAdjOther2);
     81                if (!isEnSetCollinear(enAdjPts)) {
     82                    // Not collinear, no point to proceed
    8383                    alignableStatKeepAngles = AlignableStatus.ALGN_INV_ANGLE_PRESERVING_CONFLICT;
    8484                    return;
    85                 }
    86                
     85                }
     86               
    8787            }
    8888
    8989            // Update the calculated node for angle preserving alignment
    9090            AlignWaysGeomPoint isectPnt = alignedLineKeepLength.getIntersection(new AlignWaysGeomLine(enAdjOther1.getX(), enAdjOther1.getY(),
    91                                                                                                                                                                                           endpoint.getEastNorth().getX(), endpoint.getEastNorth().getY()));
     91                                                                                                      endpoint.getEastNorth().getX(), endpoint.getEastNorth().getY()));
    9292            EastNorth enIsectPt = null;
    9393            // If the intersection is null, the adjacent and the alignee are parallel already:
    9494            // there's no need to update this node
    9595            if (isectPnt != null) {
    96                 enIsectPt = new EastNorth(isectPnt.getX(), isectPnt.getY());
    97                 // Don't "record" it yet as it may not be valid
     96                enIsectPt = new EastNorth(isectPnt.getX(), isectPnt.getY());
     97                // Don't "record" it yet as it may not be valid
    9898            } else if (alignedLineKeepLength.getIntersectionStatus() == IntersectionStatus.LINES_PARALLEL) {
    9999                alignableStatKeepAngles = AlignableStatus.ALGN_INV_ANGLE_PRESERVING_CONFLICT;
     
    107107            // TODO - find a solution
    108108            if (alws.size() == 2 && enIsectPt != null) {
    109                 int middlePtIdx = AlignWaysGeomPoint.getMiddleOf3(
    110                                 new AlignWaysGeomPoint(enIsectPt),
    111                                 new AlignWaysGeomPoint(enAdjOther1),
    112                                 new AlignWaysGeomPoint(enAdjOther2));
    113                 if (middlePtIdx != 0) {
    114                         EastNorth middlePt = null;
    115                         switch(middlePtIdx) {
    116                                 case 1:
    117                                         middlePt = enIsectPt;
    118                                         break;
    119                                 case 2:
    120                                         middlePt = enAdjOther1;
    121                                         break;
    122                                 case 3:
    123                                         middlePt = enAdjOther2;
    124                                         break;
    125                         }
    126                         if (middlePt != null) {
    127                                 double eps = 1E-6;
    128                                 if (!middlePt.equalsEpsilon(enIsectPt, eps)) {
    129                                         // Intersection point didn't fall between the two adjacent points; not allowed
    130                                 alignableStatKeepAngles = AlignableStatus.ALGN_INV_XPOINT_FALLSOUT;
    131                                 return;
    132                                        
    133                                 /*
    134                                         if (middlePt.equalsEpsilon(enAdjOther1, eps)) {
    135                                                 // Delete adjOther1
    136                                                 // adjOther1.setDeleted(true);
    137                                         } else
    138                                                 if (true);
    139                                                 // Delete adjOther2
    140                                                 // adjOther2.setDeleted(true);
    141                                         */
    142                                 }
    143                         }
    144                 }
     109                int middlePtIdx = AlignWaysGeomPoint.getMiddleOf3(
     110                        new AlignWaysGeomPoint(enIsectPt),
     111                        new AlignWaysGeomPoint(enAdjOther1),
     112                        new AlignWaysGeomPoint(enAdjOther2));
     113                if (middlePtIdx != 0) {
     114                    EastNorth middlePt = null;
     115                    switch(middlePtIdx) {
     116                        case 1:
     117                            middlePt = enIsectPt;
     118                            break;
     119                        case 2:
     120                            middlePt = enAdjOther1;
     121                            break;
     122                        case 3:
     123                            middlePt = enAdjOther2;
     124                            break;
     125                    }
     126                    if (middlePt != null) {
     127                        double eps = 1E-6;
     128                        if (!middlePt.equalsEpsilon(enIsectPt, eps)) {
     129                            // Intersection point didn't fall between the two adjacent points; not allowed
     130                            alignableStatKeepAngles = AlignableStatus.ALGN_INV_XPOINT_FALLSOUT;
     131                            return;
     132                           
     133                            /*
     134                            if (middlePt.equalsEpsilon(enAdjOther1, eps)) {
     135                                // Delete adjOther1
     136                                // adjOther1.setDeleted(true);
     137                            } else
     138                                if (true);
     139                                // Delete adjOther2
     140                                // adjOther2.setDeleted(true);
     141                            */
     142                        }
     143                    }
     144                }
    145145            }
    146146           
    147147            if (isectPnt != null) {
    148148                // Angle preserving alignment passed all verification tests: record it.
    149                 calculatedNodes.put(endpoint, enIsectPt);
     149                calculatedNodes.put(endpoint, enIsectPt);
    150150            }
    151151           
    152152           
    153153        } else {
    154                 // angle preserving alignment not possible
     154            // angle preserving alignment not possible
    155155            alignableStatKeepAngles = AlignableStatus.ALGN_INV_TOOMANY_CONNECTED_WS;
    156156        }
     
    160160
    161161    private boolean isEnSetCollinear(ArrayList<EastNorth> enAdjPts) {
    162         ArrayList<AlignWaysGeomPoint> awAdjPts = new ArrayList<AlignWaysGeomPoint>();
    163        
    164         for (EastNorth en : enAdjPts) {
    165                 AlignWaysGeomPoint pt = new AlignWaysGeomPoint(en.getX(), en.getY());
    166                 awAdjPts.add(pt);
    167         }
    168        
    169                 return AlignWaysGeomPoint.isSetCollinear(awAdjPts);
    170         }
    171 
    172 
    173         private Node getNonEqualNode(WaySegment waySegment, Node endpoint) {
    174         if (waySegment.getFirstNode().equals(endpoint)) {
    175                 return waySegment.getSecondNode();
    176         } else if (waySegment.getSecondNode().equals(endpoint)) {
    177                 return waySegment.getFirstNode();
    178         } else
    179                 return null;
    180         }
    181 
    182 
    183         /**
     162        ArrayList<AlignWaysGeomPoint> awAdjPts = new ArrayList<AlignWaysGeomPoint>();
     163       
     164        for (EastNorth en : enAdjPts) {
     165            AlignWaysGeomPoint pt = new AlignWaysGeomPoint(en.getX(), en.getY());
     166            awAdjPts.add(pt);
     167        }
     168       
     169        return AlignWaysGeomPoint.isSetCollinear(awAdjPts);
     170    }
     171
     172
     173    private Node getNonEqualNode(WaySegment waySegment, Node endpoint) {
     174        if (waySegment.getFirstNode().equals(endpoint)) {
     175            return waySegment.getSecondNode();
     176        } else if (waySegment.getSecondNode().equals(endpoint)) {
     177            return waySegment.getFirstNode();
     178        } else
     179            return null;
     180    }
     181
     182
     183    /**
    184184     * Reports invalid alignable statuses on screen in dialog boxes.
    185185     *
  • applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways/AlignWaysCmdKeepLength.java

    r29854 r30345  
    197197    }
    198198
    199     /*
    200      * (non-Javadoc)
    201      *
    202      * @see
    203      * org.openstreetmap.josm.command.Command#fillModifiedData(java.util.Collection
    204      * , java.util.Collection, java.util.Collection)
    205      */
    206199    @Override
    207200    public void fillModifiedData(Collection<OsmPrimitive> modified,
     
    212205    }
    213206
    214     /*
    215      * (non-Javadoc)
    216      *
    217      * @see org.openstreetmap.josm.command.Command#executeCommand()
    218      */
    219207    @Override
    220208    public boolean executeCommand() {
     
    223211    }
    224212
    225     /*
    226      * (non-Javadoc)
    227      *
    228      * @see org.openstreetmap.josm.command.Command#undoCommand()
    229      */
    230213    @Override
    231214    public void undoCommand() {
  • applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways/AlignWaysDialog.java

    r29854 r30345  
    7474        modesPanel.setLayout(new BoxLayout(modesPanel, BoxLayout.PAGE_AXIS));
    7575        /*
    76                 modesPanel.setBorder(BorderFactory.createCompoundBorder(
    77                                 BorderFactory.createEmptyBorder(10, 10, 10, 10),
    78                                 BorderFactory.createTitledBorder(tr("Align with:")))
    79                                 );
     76        modesPanel.setBorder(BorderFactory.createCompoundBorder(
     77                BorderFactory.createEmptyBorder(10, 10, 10, 10),
     78                BorderFactory.createTitledBorder(tr("Align with:")))
     79                );
    8080         */
    8181        modesPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
  • applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways/AlignWaysMode.java

    r27852 r30345  
    228228    }
    229229
    230     /* (non-Javadoc)
    231      * @see org.openstreetmap.josm.actions.mapmode.MapMode#layerIsSupported(org.openstreetmap.josm.gui.layer.Layer)
    232      */
    233230    @Override
    234231    public boolean layerIsSupported(Layer l) {
  • applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways/AlignWaysSegment.java

    r27348 r30345  
    2727public class AlignWaysSegment implements MapViewPaintable {
    2828
    29         protected WaySegment segment;
    30         protected MapView mapview;
    31         protected Color segmentColor = Color.WHITE;
    32         protected Collection<Node> segmentEndPoints;
     29    protected WaySegment segment;
     30    protected MapView mapview;
     31    protected Color segmentColor = Color.WHITE;
     32    protected Collection<Node> segmentEndPoints;
    3333
    34         public AlignWaysSegment(MapView mapview, Point p) throws IllegalArgumentException {
    35                 if (mapview == null)
    36                         throw new IllegalArgumentException(tr(
    37                                         "Parameter ''{0}'' must not be null", "mapview"));
    38                 if (p == null)
    39                         throw new IllegalArgumentException(tr(
    40                                         "Parameter ''{0}'' must not be null", "p"));
     34    public AlignWaysSegment(MapView mapview, Point p) throws IllegalArgumentException {
     35        if (mapview == null)
     36            throw new IllegalArgumentException(tr(
     37                    "Parameter ''{0}'' must not be null", "mapview"));
     38        if (p == null)
     39            throw new IllegalArgumentException(tr(
     40                    "Parameter ''{0}'' must not be null", "p"));
    4141
    42                 this.mapview = mapview;
    43         }
     42        this.mapview = mapview;
     43    }
    4444
    45         void setSegment(WaySegment segment) {
    46                 this.segment = segment;
    47                 if (segment != null) {
    48                         setSegmentEndpoints(segment);
    49                         mapview.addTemporaryLayer(this);
    50                 }
    51         }
     45    void setSegment(WaySegment segment) {
     46        this.segment = segment;
     47        if (segment != null) {
     48            setSegmentEndpoints(segment);
     49            mapview.addTemporaryLayer(this);
     50        }
     51    }
    5252
    5353
    54         void setSegmentEndpoints(WaySegment segment) {
    55                 if (segment != null) {
    56                         Node node1 = segment.way.getNode(segment.lowerIndex);
    57                         Node node2 = segment.way.getNode(segment.lowerIndex + 1);
     54    void setSegmentEndpoints(WaySegment segment) {
     55        if (segment != null) {
     56            Node node1 = segment.way.getNode(segment.lowerIndex);
     57            Node node2 = segment.way.getNode(segment.lowerIndex + 1);
    5858
    59                         segmentEndPoints = new HashSet<Node>();
    60                         segmentEndPoints.add(node1);
    61                         segmentEndPoints.add(node2);
     59            segmentEndPoints = new HashSet<Node>();
     60            segmentEndPoints.add(node1);
     61            segmentEndPoints.add(node2);
    6262
    63                 }
    64         }
     63        }
     64    }
    6565
    66         protected WaySegment getNearestWaySegment(Point p) {
     66    protected WaySegment getNearestWaySegment(Point p) {
    6767
    68                 return mapview.getNearestWaySegment(p, OsmPrimitive.isUsablePredicate);
     68        return mapview.getNearestWaySegment(p, OsmPrimitive.isUsablePredicate);
    6969
    70         }
     70    }
    7171
    72         public void destroy() {
    73                 if (segment != null) {
    74                         mapview.removeTemporaryLayer(this);
    75                 }
    76         }
     72    public void destroy() {
     73        if (segment != null) {
     74            mapview.removeTemporaryLayer(this);
     75        }
     76    }
    7777
    78         public WaySegment getSegment() {
    79                 return segment;
    80         }
     78    public WaySegment getSegment() {
     79        return segment;
     80    }
    8181
    82         public Collection<Node> getSegmentEndPoints() {
    83                 return segmentEndPoints;
    84         }
     82    public Collection<Node> getSegmentEndPoints() {
     83        return segmentEndPoints;
     84    }
    8585
    86         @Override
    87         public void paint(Graphics2D g, MapView mv, Bounds bbox) {
    88                 highlightSegment(segmentColor, g, mv);
    89         }
     86    @Override
     87    public void paint(Graphics2D g, MapView mv, Bounds bbox) {
     88        highlightSegment(segmentColor, g, mv);
     89    }
    9090
    91         protected void highlightSegment(Color c, Graphics2D g, MapView mv) {
     91    protected void highlightSegment(Color c, Graphics2D g, MapView mv) {
    9292
    93                 g.setColor(c);
    94                 g.setStroke(new BasicStroke(6, BasicStroke.CAP_ROUND,
    95                                 BasicStroke.JOIN_ROUND));
    96                 drawSegment(g, mv);
     93        g.setColor(c);
     94        g.setStroke(new BasicStroke(6, BasicStroke.CAP_ROUND,
     95                BasicStroke.JOIN_ROUND));
     96        drawSegment(g, mv);
    9797
    98         }
     98    }
    9999
    100         protected void drawSegment(Graphics2D g, MapView mv) {
    101                 Node n1 = segment.way.getNode(segment.lowerIndex);
    102                 Node n2 = segment.way.getNode(segment.lowerIndex + 1);
     100    protected void drawSegment(Graphics2D g, MapView mv) {
     101        Node n1 = segment.way.getNode(segment.lowerIndex);
     102        Node n2 = segment.way.getNode(segment.lowerIndex + 1);
    103103
    104                 Line2D newline = new Line2D.Double(mv.getPoint(n1), mv.getPoint(n2));
    105                 g.draw(newline);
    106         }
     104        Line2D newline = new Line2D.Double(mv.getPoint(n1), mv.getPoint(n2));
     105        g.draw(newline);
     106    }
    107107
    108         /*
    109          * (non-Javadoc)
    110          *
    111          * @see java.lang.Object#hashCode()
    112          */
    113          @Override
    114          public int hashCode() {
    115                 final int prime = 31;
    116                 int result = 1;
    117                 result = prime * result + ((segment == null) ? 0 : segment.hashCode());
    118                 result = prime * result
    119                                 + ((segmentColor == null) ? 0 : segmentColor.hashCode());
    120                 return result;
    121          }
     108     @Override
     109     public int hashCode() {
     110        final int prime = 31;
     111        int result = 1;
     112        result = prime * result + ((segment == null) ? 0 : segment.hashCode());
     113        result = prime * result
     114                + ((segmentColor == null) ? 0 : segmentColor.hashCode());
     115        return result;
     116     }
    122117
    123          /*
    124           * (non-Javadoc)
    125           *
    126           * @see java.lang.Object#equals(java.lang.Object)
    127           */
    128          @Override
    129          public boolean equals(Object obj) {
    130                  if (this == obj)
    131                          return true;
    132                  if (obj == null)
    133                          return false;
    134                  if (!(obj instanceof AlignWaysSegment))
    135                          return false;
    136                  AlignWaysSegment other = (AlignWaysSegment) obj;
    137                  if (segment == null) {
    138                          if (other.segment != null)
    139                                  return false;
    140                  } else if (!segment.equals(other.segment))
    141                          return false;
    142                  /* Segment colour is ignored in comparison
     118     @Override
     119     public boolean equals(Object obj) {
     120         if (this == obj)
     121             return true;
     122         if (obj == null)
     123             return false;
     124         if (!(obj instanceof AlignWaysSegment))
     125             return false;
     126         AlignWaysSegment other = (AlignWaysSegment) obj;
     127         if (segment == null) {
     128             if (other.segment != null)
     129                 return false;
     130         } else if (!segment.equals(other.segment))
     131             return false;
     132         /* Segment colour is ignored in comparison
    143133        if (segmentColor == null) {
    144134            if (other.segmentColor != null)
     
    146136        } else if (!segmentColor.equals(other.segmentColor))
    147137            return false;
    148                   */
    149                 return true;
    150         }
     138          */
     139        return true;
     140    }
    151141}
  • applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways/AlignWaysWhatsNewPanel.java

    r29854 r30345  
    4747
    4848        lblWhatsNew.setText("<html><div style=\"font-family: sans-serif; font-weight: bold; font-style: italic;\"><span style=\"font-size: large;\"><span style=\"font-size: x-large;\">"
    49                                             + tr("What''s new...")
    50                                             + "</span></div></html>");
     49                            + tr("What''s new...")
     50                            + "</span></div></html>");
    5151
    5252        icnLogo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/wndialog/alignways64.png"))); // NOI18N
    5353
    5454        newItem1.setText("<html><div style=\"font-family: sans-serif;\"><ul style=\"margin-left: 20px;\"><li>"
    55                                         + tr("Added <b>angle preserving</b> aligning mode")
    56                                         + "</li></ul></div></html>");
     55                        + tr("Added <b>angle preserving</b> aligning mode")
     56                        + "</li></ul></div></html>");
    5757
    5858        btnHelpItem1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/wndialog/extlink10.png"))); // NOI18N
     
    7373
    7474        newItem2.setText("<html><div style=\"font-family: sans-serif;\"><ul style=\"margin-left: 20px;\"><li>"
    75                                         + tr("Various improvements and bugfixes")
    76                                         + "</li></ul></div></html>");
     75                        + tr("Various improvements and bugfixes")
     76                        + "</li></ul></div></html>");
    7777
    7878        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
     
    115115    }// </editor-fold>
    116116
    117         private void btnHelpItem1ActionPerformed(java.awt.event.ActionEvent evt) {
    118            openURI();
    119         }
     117    private void btnHelpItem1ActionPerformed(java.awt.event.ActionEvent evt) {
     118       openURI();
     119    }
    120120
    121121    // Variables declaration - do not modify
  • applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways/geometry/AlignWaysGeomLine.java

    r27588 r30345  
    88public class AlignWaysGeomLine {
    99
    10         double coef_a, coef_b, coef_c;
     10    double coef_a, coef_b, coef_c;
    1111
    1212    public enum IntersectionStatus {
     
    7676
    7777    public AlignWaysGeomLine(AlignWaysGeomPoint awPt1, AlignWaysGeomPoint awPt2) {
    78         this(awPt1.getX(), awPt1.getY(), awPt2.getX(), awPt2.getY());
    79         }
    80 
    81         /**
     78        this(awPt1.getX(), awPt1.getY(), awPt2.getX(), awPt2.getY());
     79    }
     80
     81    /**
    8282     * Returns the intersection point of the line with another line.
    8383     * If the lines are parallel or overlap, returns null.
     
    9595
    9696        // See: http://www.mathwizz.com/algebra/help/help21.htm
    97         //  and http://en.wikipedia.org/wiki/Cramers_rule
     97        //  and https://en.wikipedia.org/wiki/Cramer%27s_rule
    9898
    9999
     
    184184    }
    185185
    186         public boolean isPointOnLine(AlignWaysGeomPoint awPt) {
    187                 // Method:
    188                 // 1. create a new line from awPt and one point of 'this'
    189                 // 2. check getIntersectionStatus of the two lines
    190                 // 3. if status is LINES_OVERLAP, the point os one the line, otherwise not
    191                
    192                 // Need an arbitrary point on this line; let it be (x, y)
    193                 Double x = 0.0;
    194                 Double y = getYonLine(x);
    195                 if (y.isNaN()) y = 0.0;
    196                
    197                 AlignWaysGeomLine line2 = new AlignWaysGeomLine(awPt, new AlignWaysGeomPoint(x, y));
    198                 getIntersection(line2);
    199                 if (getIntersectionStatus() == IntersectionStatus.LINES_OVERLAP)
    200                         return true;
    201                 else
    202                         return false;
    203                
    204         }
     186    public boolean isPointOnLine(AlignWaysGeomPoint awPt) {
     187        // Method:
     188        // 1. create a new line from awPt and one point of 'this'
     189        // 2. check getIntersectionStatus of the two lines
     190        // 3. if status is LINES_OVERLAP, the point os one the line, otherwise not
     191       
     192        // Need an arbitrary point on this line; let it be (x, y)
     193        Double x = 0.0;
     194        Double y = getYonLine(x);
     195        if (y.isNaN()) y = 0.0;
     196       
     197        AlignWaysGeomLine line2 = new AlignWaysGeomLine(awPt, new AlignWaysGeomPoint(x, y));
     198        getIntersection(line2);
     199        if (getIntersectionStatus() == IntersectionStatus.LINES_OVERLAP)
     200            return true;
     201        else
     202            return false;
     203       
     204    }
    205205
    206206}
  • applications/editors/josm/plugins/alignways/src/com/tilusnet/josm/plugins/alignways/geometry/AlignWaysGeomPoint.java

    r27588 r30345  
    1717
    1818    public AlignWaysGeomPoint(EastNorth eastNorth) {
    19         this.x = eastNorth.getX();
    20         this.y = eastNorth.getY();
    21         }
     19        this.x = eastNorth.getX();
     20        this.y = eastNorth.getY();
     21    }
    2222
    23         public double getX() {
     23    public double getX() {
    2424        return x;
    2525    }
     
    3737    }
    3838
    39         public static boolean isSetCollinear(ArrayList<AlignWaysGeomPoint> awPts) {
    40                 if (awPts.size() <= 1)
    41                         return false;
    42                
    43                 if (awPts.size() == 2)
    44                         return true;
    45                 else {
    46                         // at least 3 points
    47                         // First create a line of the first two points in the set
    48                         AlignWaysGeomLine line = new AlignWaysGeomLine(awPts.get(0), awPts.get(1));
    49                         // ...then check the subsequent points whether they are on the line
    50                         for (int i = 2; i < awPts.size(); i++) {
    51                                 if (!line.isPointOnLine(awPts.get(i))) {
    52                                         return false;
    53                                 }
    54                         }
    55                         return true;
    56                 }
    57         }
     39    public static boolean isSetCollinear(ArrayList<AlignWaysGeomPoint> awPts) {
     40        if (awPts.size() <= 1)
     41            return false;
     42       
     43        if (awPts.size() == 2)
     44            return true;
     45        else {
     46            // at least 3 points
     47            // First create a line of the first two points in the set
     48            AlignWaysGeomLine line = new AlignWaysGeomLine(awPts.get(0), awPts.get(1));
     49            // ...then check the subsequent points whether they are on the line
     50            for (int i = 2; i < awPts.size(); i++) {
     51                if (!line.isPointOnLine(awPts.get(i))) {
     52                    return false;
     53                }
     54            }
     55            return true;
     56        }
     57    }
    5858
    59         /**
    60         * Determines which (EastNorth) point falls between the other two.
    61         * Ideally to be used with collinear points.
    62         *
    63         * @return 1, 2 or 3 for pt1, pt2 and pt3, respectively.
    64         * 0 if middle value cannot be determined (i.e. some values are equal).
    65         */
    66         public static int getMiddleOf3(
    67                         AlignWaysGeomPoint pt1,
    68                         AlignWaysGeomPoint pt2,
    69                         AlignWaysGeomPoint pt3) {
    70                
    71                 int midPtXIdx = getMiddleOf3(pt1.x, pt2.x, pt3.x);
    72                 int midPtYIdx = getMiddleOf3(pt1.y, pt2.y, pt3.y);
    73                
    74                 if ((midPtXIdx == 0) && (midPtYIdx == 0))
    75                         // All 3 points overlap:
    76                         // Design decision: return the middle point (could be any other or none)
    77                         return 2;
    78                
    79                 if (midPtXIdx == 0) return midPtYIdx;
    80                 if (midPtYIdx == 0) return midPtXIdx;
    81                
    82                 // Both x and y middle points could be determined;
    83                 // their indexes must coincide
    84                 if (midPtXIdx == midPtYIdx)
    85                         // Success
    86                         return midPtXIdx; // (or midPtYIdx)
    87                 else
    88                         // Fail
    89                         return 0;
    90                
    91         }
     59    /**
     60    * Determines which (EastNorth) point falls between the other two.
     61    * Ideally to be used with collinear points.
     62    *
     63    * @return 1, 2 or 3 for pt1, pt2 and pt3, respectively.
     64    * 0 if middle value cannot be determined (i.e. some values are equal).
     65    */
     66    public static int getMiddleOf3(
     67            AlignWaysGeomPoint pt1,
     68            AlignWaysGeomPoint pt2,
     69            AlignWaysGeomPoint pt3) {
     70       
     71        int midPtXIdx = getMiddleOf3(pt1.x, pt2.x, pt3.x);
     72        int midPtYIdx = getMiddleOf3(pt1.y, pt2.y, pt3.y);
     73       
     74        if ((midPtXIdx == 0) && (midPtYIdx == 0))
     75            // All 3 points overlap:
     76            // Design decision: return the middle point (could be any other or none)
     77            return 2;
     78       
     79        if (midPtXIdx == 0) return midPtYIdx;
     80        if (midPtYIdx == 0) return midPtXIdx;
     81       
     82        // Both x and y middle points could be determined;
     83        // their indexes must coincide
     84        if (midPtXIdx == midPtYIdx)
     85            // Success
     86            return midPtXIdx; // (or midPtYIdx)
     87        else
     88            // Fail
     89            return 0;
     90       
     91    }
    9292
    93         /**
    94         * Determine which value, d1, d2 or d3 falls in the middle of the other two.
    95         * @return 1, 2 or 3 for d1, d2 and d3, respectively.
    96         * 0 if middle value cannot be determined (i.e. some values are equal).
    97         */
    98         private static int getMiddleOf3(double d1, double d2, double d3) {
    99                
    100                 Double[] dValues = {d1, d2, d3};
    101                 ArrayList<Double> alValues = new ArrayList<Double>(Arrays.asList(dValues));
    102                 Collections.sort(alValues);
    103                
    104                 if ((Math.abs(alValues.get(1) - alValues.get(0)) < 0.01) ||
    105                     (Math.abs(alValues.get(1) - alValues.get(2)) < 0.01))
    106                         // Cannot determine absolute middle value
    107                         return 0;
    108                 else {
    109                         if (Math.abs(alValues.get(1) - d1) < 0.01) return 1;
    110                         if (Math.abs(alValues.get(1) - d2) < 0.01) return 2;
    111                         if (Math.abs(alValues.get(1) - d3) < 0.01) return 3;
    112                 }
    113                
    114                 // Should never happen
    115                 return 0;
    116         }
     93    /**
     94    * Determine which value, d1, d2 or d3 falls in the middle of the other two.
     95    * @return 1, 2 or 3 for d1, d2 and d3, respectively.
     96    * 0 if middle value cannot be determined (i.e. some values are equal).
     97    */
     98    private static int getMiddleOf3(double d1, double d2, double d3) {
     99       
     100        Double[] dValues = {d1, d2, d3};
     101        ArrayList<Double> alValues = new ArrayList<Double>(Arrays.asList(dValues));
     102        Collections.sort(alValues);
     103       
     104        if ((Math.abs(alValues.get(1) - alValues.get(0)) < 0.01) ||
     105            (Math.abs(alValues.get(1) - alValues.get(2)) < 0.01))
     106            // Cannot determine absolute middle value
     107            return 0;
     108        else {
     109            if (Math.abs(alValues.get(1) - d1) < 0.01) return 1;
     110            if (Math.abs(alValues.get(1) - d2) < 0.01) return 2;
     111            if (Math.abs(alValues.get(1) - d3) < 0.01) return 3;
     112        }
     113       
     114        // Should never happen
     115        return 0;
     116    }
    117117}
Note: See TracChangeset for help on using the changeset viewer.