Java Geometry Algorithm slopeOfLineBetween(Point2D.Double p1, Point2D.Double p2)

Here you can find the source of slopeOfLineBetween(Point2D.Double p1, Point2D.Double p2)

Description

Computes slope between two points.

License

Apache License

Parameter

Parameter Description
p1 the first point
p2 the second point

Return

a double with the slope, which may be infinite (if the points are spaced vertically) or NaN (if they're the same)

Declaration

public static double slopeOfLineBetween(Point2D.Double p1, Point2D.Double p2) 

Method Source Code


//package com.java2s;
/*//from   www . j ava  2 s . co  m
 * #%L
 * BlaiseMath
 * --
 * Copyright (C) 2009 - 2015 Elisha Peterson
 * --
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.awt.geom.Point2D;

public class Main {
    /**
     * Computes slope between two points.
     * @param p1 the first point
     * @param p2 the second point
     * @return a <code>double</code> with the slope,
     *    which may be infinite (if the points are spaced vertically) or NaN (if they're the same)
     */
    public static double slopeOfLineBetween(Point2D.Double p1, Point2D.Double p2) {
        if (p1.equals(p2)) {
            return Double.NaN;
        } else if (p1.x == p2.x) {
            return Double.POSITIVE_INFINITY;
        } else {
            return (p2.y - p1.y) / (p2.x - p1.x);
        }
    }
}

Related

  1. relTo(final Point2D from, final Point2D to, final Point2D rel)
  2. resample(Vector points, int n, Vector newPoints)
  3. setPathAnchor(Shape s, Point2D pt)
  4. setPathControlPoint(Shape s, int i, Point2D pt)
  5. setRevisePoint(Point2D.Double revisePoint, Point2D.Double startPoint)
  6. smallestBoundingBox( java.awt.Point.Double[] points)
  7. snapToGrid(Point original, double gridSpacing)
  8. starRunner(int[][] screen, int x, int y, List blueList)
  9. te static double t(Point2D.Double original, Point2D.Double endpt1, Point2D.Double endpt2)